Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions e2e/cli-build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

import { runCli } from "./helpers/run-cli";

const tmpDir = resolve(__dirname, "../.tmp-e2e-build");

beforeAll(async () => {
mkdirSync(tmpDir, { recursive: true });
});

afterAll(() => {
rmSync(tmpDir, { recursive: true, force: true });
});

describe("nf build (TypeScript, no server)", () => {
const projectDir = resolve(tmpDir, "build-ts-no-server");
const appDir = resolve(projectDir, "build-app");

beforeAll(async () => {
mkdirSync(projectDir, { recursive: true });

await runCli([
"new",
"--name",
"build-app",
"--language",
"ts",
"--package-manager",
"npm",
"--strict",
"--no-server",
"--no-init-functions",
"--no-skip-install",
"-d",
projectDir,
]);
});

it("should run the build command", async () => {
const { exitCode } = await runCli(["build", "-d", appDir]);

expect(exitCode).toBe(0);
expect(existsSync(resolve(appDir, ".nanoforge", "client"))).toBe(true);
expect(existsSync(resolve(appDir, ".nanoforge", "client", "main.js"))).toBe(true);
});

it("should accept --config option", async () => {
const { exitCode } = await runCli(["build", "-d", appDir, "--config", "nanoforge.config.json"]);

expect(exitCode).toBe(0);
expect(existsSync(resolve(appDir, ".nanoforge", "client", "main.js"))).toBe(true);
});

it("should accept --client-outDir option", async () => {
const { exitCode } = await runCli(["build", "-d", appDir, "--client-outDir", "custom-out"]);

expect(exitCode).toBe(0);
expect(existsSync(resolve(appDir, "custom-out"))).toBe(true);
expect(existsSync(resolve(appDir, "custom-out", "main.js"))).toBe(true);
});
});

describe("nf build (TypeScript, with server)", () => {
const projectDir = resolve(tmpDir, "build-ts-with-server");
const appDir = resolve(projectDir, "build-server-app");

beforeAll(async () => {
mkdirSync(projectDir, { recursive: true });

await runCli([
"new",
"--name",
"build-server-app",
"--language",
"ts",
"--package-manager",
"npm",
"--no-strict",
"--server",
"--no-init-functions",
"--no-skip-install",
"-d",
projectDir,
]);
});

it("should run the build command with server enabled", async () => {
const { exitCode } = await runCli(["build", "-d", appDir]);

expect(exitCode).toBe(0);
expect(existsSync(resolve(appDir, ".nanoforge", "client", "main.js"))).toBe(true);
expect(existsSync(resolve(appDir, ".nanoforge", "server", "main.js"))).toBe(true);
});

it("should accept --server-outDir option", async () => {
const { exitCode } = await runCli([
"build",
"-d",
appDir,
"--server-outDir",
"custom-server-out",
]);

expect(exitCode).toBe(0);
expect(existsSync(resolve(appDir, "custom-server-out"))).toBe(true);
expect(existsSync(resolve(appDir, "custom-server-out", "main.js"))).toBe(true);
});
});

describe("nf build (with invalid directory)", () => {
it("should fail when directory does not exist", async () => {
const { exitCode } = await runCli(["build", "-d", resolve(tmpDir, "nonexistent")]);

expect(exitCode).not.toBe(0);
});

it("should fail when no config file is found", async () => {
const emptyDir = resolve(tmpDir, "empty-dir");
mkdirSync(emptyDir, { recursive: true });

const { exitCode } = await runCli(["build", "-d", emptyDir]);

expect(exitCode).not.toBe(0);
});
});
132 changes: 132 additions & 0 deletions e2e/cli-install.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
import { resolve } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

import { runCli } from "./helpers/run-cli";

const tmpDir = resolve(__dirname, "../.tmp-e2e-install");

beforeAll(async () => {
mkdirSync(tmpDir, { recursive: true });
});

afterAll(() => {
rmSync(tmpDir, { recursive: true, force: true });
});

describe("nf install (with existing project)", () => {
const projectDir = resolve(tmpDir, "install-project");
const appDir = resolve(projectDir, "install-app");

beforeAll(async () => {
mkdirSync(projectDir, { recursive: true });

await runCli([
"new",
"--name",
"install-app",
"--language",
"ts",
"--package-manager",
"npm",
"--strict",
"--no-server",
"--no-init-functions",
"--no-skip-install",
"-d",
projectDir,
]);
});

it("should run the install command with a library name", async () => {
const { exitCode } = await runCli(["install", "@nanoforge-dev/network-client", "-d", appDir]);

expect(exitCode).toBe(0);

const pkgJson = JSON.parse(readFileSync(resolve(appDir, "package.json"), "utf-8"));
expect(pkgJson.dependencies).toHaveProperty("@nanoforge-dev/network-client");
expect(existsSync(resolve(appDir, "node_modules", "@nanoforge-dev", "network-client"))).toBe(
true,
);
});

it("should run the install command with multiple library names", async () => {
const { exitCode } = await runCli([
"install",
"@nanoforge-dev/network-client",
"@nanoforge-dev/network-server",
"-d",
appDir,
]);

expect(exitCode).toBe(0);

const pkgJson = JSON.parse(readFileSync(resolve(appDir, "package.json"), "utf-8"));
expect(pkgJson.dependencies).toHaveProperty("@nanoforge-dev/network-client");
expect(pkgJson.dependencies).toHaveProperty("@nanoforge-dev/network-server");
expect(existsSync(resolve(appDir, "node_modules", "@nanoforge-dev", "network-client"))).toBe(
true,
);
expect(existsSync(resolve(appDir, "node_modules", "@nanoforge-dev", "network-server"))).toBe(
true,
);
});

it("should work with the add alias", async () => {
const { exitCode } = await runCli(["add", "@nanoforge-dev/network-client", "-d", appDir]);

expect(exitCode).toBe(0);

const pkgJson = JSON.parse(readFileSync(resolve(appDir, "package.json"), "utf-8"));
expect(pkgJson.dependencies).toHaveProperty("@nanoforge-dev/network-client");
expect(existsSync(resolve(appDir, "node_modules", "@nanoforge-dev", "network-client"))).toBe(
true,
);
});
});

describe("nf install (with invalid directory)", () => {
it("should fail when directory does not exist", async () => {
const { exitCode } = await runCli([
"install",
"some-lib",
"-d",
resolve(tmpDir, "nonexistent"),
]);

expect(exitCode).not.toBe(0);
});
});

describe("nf install (without library name)", () => {
const projectDir = resolve(tmpDir, "install-no-name");
const appDir = resolve(projectDir, "install-no-name-app");

beforeAll(async () => {
mkdirSync(projectDir, { recursive: true });

await runCli([
"new",
"--name",
"install-no-name-app",
"--language",
"ts",
"--package-manager",
"npm",
"--strict",
"--no-server",
"--no-init-functions",
"--no-skip-install",
"-d",
projectDir,
]);
});

it("should prompt or fail when no library name is provided", async () => {
const { stdout, stderr, killed } = await runCli(["install", "-d", appDir], {
timeout: 3_000,
});

expect(killed || (stdout + stderr).length > 0).toBe(true);
});
});
52 changes: 51 additions & 1 deletion src/action/abstract.action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
import { type Input } from "@lib/input";
import { Prefixes } from "@lib/ui";

import { handleActionError } from "@utils/errors";

export interface HandleResult {
success?: boolean;
keepAlive?: boolean;
}

export abstract class AbstractAction {
public abstract handle(args?: Input, options?: Input, extraFlags?: string[]): Promise<void>;
protected abstract startMessage: string;
protected abstract successMessage: string;
protected abstract failureMessage: string;

public abstract handle(
args?: Input,
options?: Input,
extraFlags?: string[],
): Promise<HandleResult>;

public async run(args?: Input, options?: Input, extraFlags?: string[]): Promise<void> {
this.logStart();

try {
const result = await this.handle(args, options, extraFlags);
this.resolveResult(result);
} catch (error: unknown) {
handleActionError(this.failureMessage, error);
}
}

private logStart(): void {
console.info();
console.info(`${Prefixes.INFO} ${this.startMessage}`);
console.info();
}

private resolveResult(result: HandleResult): void {
const success = result?.success !== false;
const keepAlive = result?.keepAlive === true;

if (keepAlive) return;

console.info();

if (!success) {
if (this.failureMessage) console.error(this.failureMessage);
process.exit(1);
}

if (this.successMessage) console.info(this.successMessage);
process.exit(0);
}
}
Loading