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
2 changes: 2 additions & 0 deletions e2e/cli-build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("nf build (TypeScript, no server)", () => {
"--no-server",
"--no-init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -81,6 +82,7 @@ describe("nf build (TypeScript, with server)", () => {
"--server",
"--no-init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down
2 changes: 2 additions & 0 deletions e2e/cli-generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("nf generate (TypeScript, no server)", () => {
"--no-server",
"--init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -228,6 +229,7 @@ describe("nf generate (TypeScript, with server)", () => {
"--server",
"--init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down
1 change: 1 addition & 0 deletions e2e/cli-help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe("nf new --help", () => {
expect(stdout).toContain("--init-functions");
expect(stdout).toContain("--skip-install");
expect(stdout).toContain("--directory");
expect(stdout).toContain("--docker");
});
});

Expand Down
2 changes: 2 additions & 0 deletions e2e/cli-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("nf install (with existing project)", () => {
"--no-server",
"--no-init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -117,6 +118,7 @@ describe("nf install (without library name)", () => {
"--no-server",
"--no-init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down
3 changes: 3 additions & 0 deletions e2e/cli-new-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("nf new config output (no server)", () => {
"--no-server",
"--no-init-functions",
"--skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -75,6 +76,7 @@ describe("nf new config output (with server)", () => {
"--server",
"--no-init-functions",
"--skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -122,6 +124,7 @@ describe("nf new package.json output", () => {
"--no-server",
"--no-init-functions",
"--skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down
81 changes: 81 additions & 0 deletions e2e/cli-new.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("nf new (TypeScript, no server)", () => {
"--no-server",
"--no-init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -103,6 +104,7 @@ describe("nf new (JavaScript, with server)", () => {
"--server",
"--init-functions",
"--no-skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand Down Expand Up @@ -172,6 +174,7 @@ describe("nf new (with --path option)", () => {
"--no-server",
"--no-init-functions",
"--skip-install",
"--no-docker",
"-d",
projectDir,
]);
Expand All @@ -184,3 +187,81 @@ describe("nf new (with --path option)", () => {
expect(existsSync(resolve(projectDir, "custom/subdir"))).toBe(true);
});
});

describe("nf new (with typescript with docker option)", () => {
const projectDir = resolve(tmpDir, "ts-with-docker");

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

it("should create a project successfully", async () => {
const { stdout, exitCode } = await runCli([
"new",
"--name",
"ts-app",
"--language",
"ts",
"--package-manager",
"npm",
"--strict",
"--no-server",
"--no-init-functions",
"--no-skip-install",
"--docker",
"-d",
projectDir,
]);

expect(exitCode).toBe(0);
expect(stdout).toContain("NanoForge Project Creation");
expect(stdout).toContain("Project successfully created");
});

it("should create the project directory", () => {
expect(existsSync(resolve(projectDir, "ts-app"))).toBe(true);
});

it("should generate nanoforge.config.json", () => {
const configPath = resolve(projectDir, "ts-app/nanoforge.config.json");
expect(existsSync(configPath)).toBe(true);

const config = JSON.parse(readFileSync(configPath, "utf-8"));
expect(config.client).toBeDefined();
expect(config.client.build.entryFile).toBe("client/main.ts");
});

it("should generate package.json", () => {
expect(existsSync(resolve(projectDir, "ts-app/package.json"))).toBe(true);
});

it("should generate tsconfig.json for TypeScript", () => {
expect(existsSync(resolve(projectDir, "ts-app/tsconfig.json"))).toBe(true);
});

it("should generate client directory", () => {
expect(existsSync(resolve(projectDir, "ts-app/client"))).toBe(true);
});

it("should generate client main file", () => {
expect(existsSync(resolve(projectDir, "ts-app/client/main.ts"))).toBe(true);
});

it("should not generate server directory", () => {
expect(existsSync(resolve(projectDir, "ts-app/server"))).toBe(false);
});

it("should not have server enabled in config", () => {
const configPath = resolve(projectDir, "ts-app/nanoforge.config.json");
const config = JSON.parse(readFileSync(configPath, "utf-8"));
expect(config.server?.enable).not.toBe(true);
});

it("should generate Dockerfile", () => {
expect(existsSync(resolve(projectDir, "ts-app/Dockerfile"))).toBe(true);
});

it("should generate .dockerignore", () => {
expect(existsSync(resolve(projectDir, "ts-app/.dockerignore"))).toBe(true);
});
});
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ catalogs:
schematics:
'@angular-devkit/schematics': ^21.1.4
'@angular-devkit/schematics-cli': ^21.1.4
'@nanoforge-dev/schematics': ^1.1.0
'@nanoforge-dev/schematics': ^1.2.0
tests:
'@vitest/coverage-v8': ^4.0.18
vitest: ^4.0.18
Expand Down
15 changes: 15 additions & 0 deletions src/action/actions/new.action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from "node:path";

import { type Input, getDirectoryInput } from "@lib/input";
import { getDockerOrAsk } from "@lib/input/inputs/new/docker.input";
import { getNewInitFunctionsWithDefault } from "@lib/input/inputs/new/init-functions.input";
import { getNewLanguageInputOrAsk } from "@lib/input/inputs/new/language.input";
import { getNewNameInputOrAsk } from "@lib/input/inputs/new/name.input";
Expand All @@ -25,6 +26,7 @@ interface NewValues {
server: boolean;
initFunctions: boolean;
skipInstall: boolean;
docker: boolean;
}

export class NewAction extends AbstractAction {
Expand Down Expand Up @@ -57,6 +59,7 @@ export class NewAction extends AbstractAction {
server: await getNewServerOrAsk(inputs),
initFunctions: getNewInitFunctionsWithDefault(inputs),
skipInstall: await getNewSkipInstallOrAsk(inputs),
docker: await getDockerOrAsk(inputs),
};
}

Expand All @@ -69,6 +72,7 @@ export class NewAction extends AbstractAction {
await this.generateApplication(collection, values);
await this.generateConfiguration(collection, values);
await this.generateClientParts(collection, values);
await this.generateDocker(collection, values);

if (values.server) {
await this.generateServerParts(collection, values);
Expand Down Expand Up @@ -126,6 +130,17 @@ export class NewAction extends AbstractAction {
await executeSchematic("Server main file", collection, "part-main", partOptions);
}

private async generateDocker(
collection: ReturnType<typeof CollectionFactory.create>,
values: NewValues,
) {
await executeSchematic("Docker", collection, "docker", {
name: values.name,
directory: values.directory,
packageManager: values.packageManager,
});
}

private partOptions(values: NewValues, part: "client" | "server") {
return {
name: values.name,
Expand Down
4 changes: 4 additions & 0 deletions src/command/commands/new.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface NewOptions {
server?: boolean;
initFunctions?: boolean;
skipInstall?: boolean;
docker?: boolean;
}

export class NewCommand extends AbstractCommand {
Expand All @@ -32,6 +33,8 @@ export class NewCommand extends AbstractCommand {
.option("--no-init-functions", "do not initialize functions")
.option("--skip-install", "skip installing dependencies")
.option("--no-skip-install", "do not skip installing dependencies")
.option("--docker", "generate docker files")
.option("--no-docker", "do not generate docker files")
.action(async (rawOptions: NewOptions) => {
const options = AbstractCommand.mapToInput({
directory: rawOptions.directory,
Expand All @@ -43,6 +46,7 @@ export class NewCommand extends AbstractCommand {
server: rawOptions.server,
initFunctions: rawOptions.initFunctions,
skipInstall: rawOptions.skipInstall,
docker: rawOptions.docker,
});

await this.action.run(new Map(), options);
Expand Down
31 changes: 31 additions & 0 deletions src/lib/input/inputs/new/docker.input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it, vi } from "vitest";

import { askConfirm } from "@lib/question";

import { type Input } from "../../input.type";
import { getDockerOrAsk } from "./docker.input";

vi.mock("@lib/question", () => ({
askConfirm: vi.fn(),
}));

const createInput = (entries: [string, any][]): Input => {
return new Map(entries.map(([key, value]) => [key, { value }]));
};

describe("getDockerOrAsk", () => {
it("should return the docker input when provided", async () => {
const input = createInput([["docker", true]]);

expect(await getDockerOrAsk(input)).toBe(true);
expect(askConfirm).not.toHaveBeenCalled();
});

it("should call askConfirm when docker is not provided", async () => {
vi.mocked(askConfirm).mockResolvedValue(false);
const input = createInput([]);

expect(await getDockerOrAsk(input)).toBe(false);
expect(askConfirm).toHaveBeenCalledOnce();
});
});
16 changes: 16 additions & 0 deletions src/lib/input/inputs/new/docker.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { askConfirm } from "@lib/question";
import { Messages } from "@lib/ui";

import { getInputOrAsk } from "../../ask-inputs";
import { getBooleanInput } from "../../base-inputs";
import { type Input } from "../../input.type";

const getDockerInput = (inputs: Input) => {
return getBooleanInput(inputs, "docker");
};

export const getDockerOrAsk = (inputs: Input) => {
return getInputOrAsk(getDockerInput(inputs), () =>
askConfirm(Messages.NEW_DOCKER_QUESTION, { default: true }),
);
};
5 changes: 5 additions & 0 deletions src/lib/schematics/nanoforge.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class NanoforgeCollection extends AbstractCollection {
alias: "main",
description: "Generate a NanoForge Part Main file",
},
{
name: "docker",
alias: "docker",
description: "Generate a Dockerfile for the application",
},
];

constructor(runner: Runner, cwd?: string) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/ui/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Messages = {
NEW_STRICT_QUESTION: "Do you want to use strict type checking?",
NEW_SERVER_QUESTION: "Do you want to generate a server for multiplayer?",
NEW_SKIP_INSTALL_QUESTION: "Do you want to skip dependency installation?",
NEW_DOCKER_QUESTION: "Do you want to add a Dockerfile for containerization?",

// --- Generate ---
GENERATE_START: "NanoForge Generate",
Expand Down