@bitbybit-dev/create-app 1.1.0 → 1.2.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ import { describe, it, expect, beforeAll, afterAll } from "vitest";
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import path from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
9
+ const CLI = path.join(ROOT, "dist/index.js");
10
+ const RELEASE_VERSION = (JSON.parse(readFileSync(path.join(ROOT, "package.json"), "utf8")) as { version: string }).version;
11
+
12
+ const FRONTEND_FILES = ["package.json", "tsconfig.json", "index.html", "src/main.ts", "src/style.css"];
13
+ const ENGINES = ["threejs", "babylonjs", "playcanvas"] as const;
14
+ const CLOUD_BACKENDS = ["hono-sdk", "hono-rest", "nodejs-sdk", "nodejs-rest"] as const;
15
+ const OCCT_BITNESS = "32";
16
+ const FAILURE_EXIT = 1;
17
+
18
+ const ENGINE_TEMPLATES_DIR = path.join(ROOT, "templates", "vite");
19
+ const TEMPLATE_LANGUAGE = "typescript";
20
+ const CLI_SOURCE = path.join(ROOT, "src", "index.ts");
21
+ const OPTIONS_PATTERN_DECLARATION = /const\s+optionsPattern\s*=\s*\/(.+)\/([dgimsuvy]*);/;
22
+ const LAST_TWO_OPTION_PROPERTIES = /(^[ \t]*\w+:\s*true,\n)(^[ \t]*enableManifold:\s*true,\n)/m;
23
+
24
+ type EngineTemplate = { engine: string; mainTs: string };
25
+
26
+ const templateMainTs = (engine: string): string =>
27
+ path.join(ENGINE_TEMPLATES_DIR, engine, TEMPLATE_LANGUAGE, "src", "main.ts");
28
+
29
+ const engineTemplates = (): EngineTemplate[] =>
30
+ readdirSync(ENGINE_TEMPLATES_DIR, { withFileTypes: true })
31
+ .filter((entry) => entry.isDirectory())
32
+ .map((entry) => ({ engine: entry.name, mainTs: templateMainTs(entry.name) }))
33
+ .filter((template) => existsSync(template.mainTs))
34
+ .sort((left, right) => left.engine.localeCompare(right.engine));
35
+
36
+ const cliOptionsPattern = (): RegExp => {
37
+ const source = readFileSync(CLI_SOURCE, "utf8");
38
+ const declaration = OPTIONS_PATTERN_DECLARATION.exec(source);
39
+ const body = declaration?.[1];
40
+ if (!body) {
41
+ throw new Error(
42
+ `could not read "const optionsPattern = /.../;" out of ${CLI_SOURCE}. That regex is what rewrites a ` +
43
+ "scaffolded project's src/main.ts when 64-bit OCCT is chosen; if it was renamed or moved, point this " +
44
+ "test at it again rather than deleting the test. Check src/index.ts.");
45
+ }
46
+ return new RegExp(body, (declaration?.[2] ?? "").replace(/[gy]/g, ""));
47
+ };
48
+
49
+ type Manifest = { name: string; dependencies?: Record<string, string>; devDependencies?: Record<string, string> };
50
+
51
+ let work: string;
52
+
53
+ const scaffold = (args: string[]): { status: number | null; output: string } => {
54
+ const result = spawnSync("node", [CLI, ...args], {
55
+ cwd: work, encoding: "utf8", env: { ...process.env, CI: "1", NO_COLOR: "1" },
56
+ });
57
+ return { status: result.status, output: result.stdout + result.stderr };
58
+ };
59
+
60
+ const manifestOf = (...segments: string[]): Manifest =>
61
+ JSON.parse(readFileSync(path.join(work, ...segments, "package.json"), "utf8")) as Manifest;
62
+
63
+ describe("create-app", () => {
64
+ beforeAll(() => {
65
+ if (!existsSync(CLI)) {
66
+ const built = spawnSync("npm", ["run", "build"], { cwd: ROOT, encoding: "utf8" });
67
+ expect(built.status, `building the CLI failed:\n${built.stdout}${built.stderr}`).toBe(0);
68
+ }
69
+ work = mkdtempSync(path.join(tmpdir(), "create-app-test-"));
70
+ }, 120_000);
71
+
72
+ afterAll(() => {
73
+ if (work) rmSync(work, { recursive: true, force: true });
74
+ });
75
+
76
+ describe("a frontend project", () => {
77
+ it.each(ENGINES)("should scaffold a runnable %s project", (engine) => {
78
+ // Arrange
79
+ const name = `frontend-${engine}`;
80
+
81
+ // Act
82
+ const { status } = scaffold([name, "-t", "frontend", "-e", engine, "-o", OCCT_BITNESS]);
83
+
84
+ // Assert
85
+ expect(status).toBe(0);
86
+ for (const file of FRONTEND_FILES) {
87
+ expect(existsSync(path.join(work, name, file)), `${name}/${file} is missing`).toBe(true);
88
+ }
89
+ const manifest = manifestOf(name);
90
+ expect(manifest.name).toBe(name);
91
+ expect(manifest.dependencies?.[`@bitbybit-dev/${engine}`]).toBe(RELEASE_VERSION);
92
+ });
93
+
94
+ it("should pin the engine package at the version this release publishes", () => {
95
+ // Arrange
96
+ const name = "pinned";
97
+
98
+ // Act
99
+ scaffold([name, "-t", "frontend", "-e", "threejs", "-o", OCCT_BITNESS]);
100
+
101
+ // Assert
102
+ const pins = Object.entries(manifestOf(name).dependencies ?? {})
103
+ .filter(([dependency]) => dependency.startsWith("@bitbybit-dev/"));
104
+ expect(pins.length).toBeGreaterThan(0);
105
+ for (const [, pinned] of pins) expect(pinned).toBe(RELEASE_VERSION);
106
+ });
107
+ });
108
+
109
+ describe("a cloud project", () => {
110
+ it.each(CLOUD_BACKENDS)("should scaffold a frontend and a backend for %s", (backend) => {
111
+ // Arrange
112
+ const name = `cloud-${backend}`;
113
+
114
+ // Act
115
+ const { status } = scaffold([name, "-t", "cloud", "-b", backend]);
116
+
117
+ // Assert
118
+ expect(status).toBe(0);
119
+ expect(existsSync(path.join(work, name, "frontend", "package.json"))).toBe(true);
120
+ expect(existsSync(path.join(work, name, "backend", "package.json"))).toBe(true);
121
+ expect(existsSync(path.join(work, name, "README.md"))).toBe(true);
122
+ });
123
+
124
+ it.each(CLOUD_BACKENDS)("should give the %s backend a .gitignore that covers the secret file the CLI writes beside it", (backend) => {
125
+ // Arrange
126
+ const name = `ignored-${backend}`;
127
+ const secret = backend.startsWith("hono") ? ".dev.vars" : ".env";
128
+
129
+ // Act
130
+ const { status } = scaffold([name, "-t", "cloud", "-b", backend]);
131
+
132
+ // Assert
133
+ expect(status).toBe(0);
134
+ const backendDir = path.join(work, name, "backend");
135
+ expect(existsSync(path.join(backendDir, secret))).toBe(true);
136
+ expect(existsSync(path.join(backendDir, "_gitignore"))).toBe(false);
137
+ expect(readFileSync(path.join(backendDir, ".gitignore"), "utf8")).toContain(secret);
138
+ });
139
+ });
140
+
141
+ describe("when the arguments are wrong", () => {
142
+ it("should refuse an engine it does not have a template for", () => {
143
+ // Act
144
+ const { status, output } = scaffold(["bad-engine", "-t", "frontend", "-e", "unrealengine", "-o", OCCT_BITNESS]);
145
+
146
+ // Assert
147
+ expect(status).toBe(FAILURE_EXIT);
148
+ expect(output).toMatch(/unrealengine/);
149
+ expect(existsSync(path.join(work, "bad-engine"))).toBe(false);
150
+ });
151
+
152
+ it("should refuse a backend it does not have a template for", () => {
153
+ // Act
154
+ const { status } = scaffold(["bad-backend", "-t", "cloud", "-b", "cobol"]);
155
+
156
+ // Assert
157
+ expect(status).toBe(FAILURE_EXIT);
158
+ expect(existsSync(path.join(work, "bad-backend"))).toBe(false);
159
+ });
160
+ });
161
+
162
+ describe("when the target directory is already there", () => {
163
+ it("should refuse to write into a directory that has files in it", () => {
164
+ // Arrange
165
+ const name = "occupied";
166
+ mkdirSync(path.join(work, name), { recursive: true });
167
+ writeFileSync(path.join(work, name, "mine.txt"), "do not overwrite me");
168
+
169
+ // Act
170
+ const { status } = scaffold([name, "-t", "frontend", "-e", "threejs", "-o", OCCT_BITNESS]);
171
+
172
+ // Assert
173
+ expect(status).toBe(FAILURE_EXIT);
174
+ expect(readFileSync(path.join(work, name, "mine.txt"), "utf8")).toBe("do not overwrite me");
175
+ });
176
+ });
177
+
178
+ describe("the OCCT architecture patch", () => {
179
+ const mismatchHint = (engine: string): string =>
180
+ `templates/vite/${engine}/typescript/src/main.ts no longer matches the regex the CLI rewrites it with. ` +
181
+ "\"enableManifold: true,\" has to stay the last property of the InitBitByBitOptions object literal, " +
182
+ "or whoever picks 64-bit OCCT silently gets 32-bit. Check optionsPattern in src/index.ts.";
183
+
184
+ it("should ship a patchable template for every engine the CLI offers", () => {
185
+ // Act
186
+ const engines = engineTemplates().map((template) => template.engine);
187
+
188
+ // Assert
189
+ expect(engines).toEqual([...ENGINES].sort());
190
+ });
191
+
192
+ it.each(engineTemplates())("should match the options object $engine's main.ts declares", ({ engine, mainTs }) => {
193
+ // Arrange
194
+ const template = readFileSync(mainTs, "utf8");
195
+
196
+ // Act
197
+ const matched = cliOptionsPattern().test(template);
198
+
199
+ // Assert
200
+ expect(matched, mismatchHint(engine)).toBe(true);
201
+ });
202
+
203
+ it("should stop matching once that options object is reordered", () => {
204
+ const [first] = engineTemplates();
205
+ if (!first) throw new Error(`no engine templates under ${ENGINE_TEMPLATES_DIR} - there is nothing left to patch`);
206
+ const template = readFileSync(first.mainTs, "utf8");
207
+ const reordered = template.replace(LAST_TWO_OPTION_PROPERTIES, "$2$1");
208
+
209
+ // Act
210
+ const matched = cliOptionsPattern().test(reordered);
211
+
212
+ expect(reordered, "the swap changed nothing, so this case proves nothing").not.toBe(template);
213
+ expect(matched).toBe(false);
214
+ });
215
+
216
+ it.each(ENGINES)("should write the chosen 64-bit architecture into a scaffolded %s project", (engine) => {
217
+ // Arrange
218
+ const name = `arch-64-${engine}`;
219
+
220
+ // Act
221
+ const { status } = scaffold([name, "-t", "frontend", "-e", engine, "-o", "64"]);
222
+
223
+ // Assert
224
+ expect(status).toBe(0);
225
+ const generated = readFileSync(path.join(work, name, "src", "main.ts"), "utf8");
226
+ expect(generated, mismatchHint(engine)).toMatch(/const options: InitBitByBitOptions = \{[\s\S]*?occtArchitecture: "64"\s*\};/);
227
+ });
228
+
229
+ it("should write the multi-threaded architecture the same way", () => {
230
+ // Arrange
231
+ const name = "arch-64-mt";
232
+
233
+ // Act
234
+ const { status } = scaffold([name, "-t", "frontend", "-e", "threejs", "-o", "64-mt"]);
235
+
236
+ // Assert
237
+ expect(status).toBe(0);
238
+ const generated = readFileSync(path.join(work, name, "src", "main.ts"), "utf8");
239
+ expect(generated, mismatchHint("threejs")).toMatch(/const options: InitBitByBitOptions = \{[\s\S]*?occtArchitecture: "64-mt"\s*\};/);
240
+ });
241
+
242
+ it("should leave main.ts exactly as the template has it for the default 32-bit choice", () => {
243
+ // Arrange
244
+ const name = "arch-32";
245
+
246
+ // Act
247
+ const { status } = scaffold([name, "-t", "frontend", "-e", "threejs", "-o", OCCT_BITNESS]);
248
+
249
+ // Assert
250
+ expect(status).toBe(0);
251
+ const generated = readFileSync(path.join(work, name, "src", "main.ts"), "utf8");
252
+ expect(generated).toBe(readFileSync(templateMainTs("threejs"), "utf8"));
253
+ });
254
+ });
255
+ });