create-creact 0.0.0-stage → 0.1.1
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.
- package/LICENSE +12 -0
- package/README.md +17 -2
- package/dist/create.js +263 -0
- package/dist/layer.js +633 -0
- package/dist/main.js +63 -0
- package/dist/options.js +262 -0
- package/dist/shell.js +128 -0
- package/dist/stacks.js +42 -0
- package/dist/ui.js +507 -0
- package/dist/version.js +7 -0
- package/layer/base/all/.oxfmtrc.json.layer +65 -0
- package/layer/base/all/AGENTS.md.layer +22 -0
- package/layer/base/all/CLAUDE.md.layer +1 -0
- package/layer/base/all/apps/web/src/styles/tokens.css.layer +15 -0
- package/layer/base/all/commitlint.config.ts.layer +12 -0
- package/layer/base/all/oxlint.config.ts.layer +556 -0
- package/layer/base/next/AGENTS.md.layer +4 -0
- package/layer/base/next/apps/web/CLAUDE.md.layer +1 -0
- package/layer/base/react-router/AGENTS.md.layer +3 -0
- package/layer/standalone/all/.husky/commit-msg.layer +2 -0
- package/layer/standalone/all/.husky/pre-commit.layer +2 -0
- package/layer/standalone/all/.lintstagedrc.json.layer +4 -0
- package/layer/standalone/all/AGENTS.md.layer +9 -0
- package/layer/standalone/all/vitest.config.ts.layer +14 -0
- package/layer/stylex/all/AGENTS.md.layer +4 -0
- package/layer/stylex/all/apps/web/src/styles/tokens.stylex.ts.layer +15 -0
- package/layer/stylex/all/apps/web/src/styles/tokens.test.ts.layer +32 -0
- package/layer/stylex/all/oxlint.styling.ts.layer +20 -0
- package/layer/stylex/all/vitest.styling.ts.layer +18 -0
- package/layer/stylex/next/apps/web/babel.config.js.layer +25 -0
- package/layer/stylex/next/apps/web/postcss.config.js.layer +16 -0
- package/layer/stylex/next/apps/web/src/app/layout.tsx.layer +20 -0
- package/layer/stylex/next/apps/web/src/app/page.tsx.layer +48 -0
- package/layer/stylex/next/apps/web/src/index.css.layer +45 -0
- package/layer/stylex/react-router/apps/web/src/index.css.layer +27 -0
- package/layer/stylex/react-router/apps/web/src/root.tsx.layer +94 -0
- package/layer/stylex/react-router/apps/web/src/routes/_index.tsx.layer +56 -0
- package/layer/stylex/react-router/apps/web/vite.config.ts.layer +22 -0
- package/layer/tailwind/all/AGENTS.md.layer +4 -0
- package/layer/tailwind/all/apps/web/src/index.css.layer +3 -0
- package/layer/tailwind/all/apps/web/src/styles/tokens.tailwind.css.layer +7 -0
- package/layer/tailwind/all/apps/web/src/styles/tokens.test.ts.layer +35 -0
- package/layer/tailwind/all/oxlint.styling.ts.layer +69 -0
- package/layer/tailwind/all/vitest.styling.ts.layer +5 -0
- package/layer/vite-plus/all/.vite-hooks/commit-msg.layer +2 -0
- package/layer/vite-plus/all/.vite-hooks/pre-commit.layer +2 -0
- package/layer/vite-plus/all/AGENTS.md.layer +11 -0
- package/layer/vite-plus/all/vite.config.ts.layer +25 -0
- package/package.json +37 -4
package/dist/layer.js
ADDED
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
/* oxlint-disable security/detect-non-literal-fs-filename -- every path is under the project dir or the CLI's own layer dir */
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { lstat, mkdir, readdir, readFile, readlink, rename, rm, symlink, writeFile, } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { EXTRAS, isBuilt, isProjectName, STYLINGS, TOOLCHAINS, UsageError, } from "./options.js";
|
|
7
|
+
import { MONOREPOS, STACKS } from "./stacks.js";
|
|
8
|
+
export const JSON_FILES = [
|
|
9
|
+
"package.json",
|
|
10
|
+
"tsconfig.json",
|
|
11
|
+
"packages/config/tsconfig.base.json",
|
|
12
|
+
"apps/web/package.json",
|
|
13
|
+
"apps/web/tsconfig.json",
|
|
14
|
+
];
|
|
15
|
+
const MANIFESTS = JSON_FILES.filter((file) => file.endsWith("package.json"));
|
|
16
|
+
export const CREACT_LOCK = {
|
|
17
|
+
"agent-browser": {
|
|
18
|
+
source: "vercel-labs/agent-browser",
|
|
19
|
+
skillPath: "skills/agent-browser/SKILL.md",
|
|
20
|
+
computedHash: "80161e6836b3b40f4e83730a290deb40ac14d877d38c97ad5f6d71be37ce74c3",
|
|
21
|
+
},
|
|
22
|
+
"find-skills": {
|
|
23
|
+
source: "vercel-labs/skills",
|
|
24
|
+
skillPath: "skills/find-skills/SKILL.md",
|
|
25
|
+
computedHash: "b146008599c31057cef1c145774cea5d5afb30e8f43fa802e47a4b461419aaaf",
|
|
26
|
+
},
|
|
27
|
+
"grill-me": {
|
|
28
|
+
source: "mattpocock/skills",
|
|
29
|
+
skillPath: "skills/productivity/grill-me/SKILL.md",
|
|
30
|
+
computedHash: "9cdbb4b8f7a3aeaef82e6230c9e823500d4a8a1214c726cf342cf9829d34fc7d",
|
|
31
|
+
},
|
|
32
|
+
grilling: {
|
|
33
|
+
source: "mattpocock/skills",
|
|
34
|
+
skillPath: "skills/productivity/grilling/SKILL.md",
|
|
35
|
+
computedHash: "e40cc1d348f6d935e28f1e24474302d7368f0a400429271a43b35fe154891dc5",
|
|
36
|
+
},
|
|
37
|
+
shadcn: {
|
|
38
|
+
source: "shadcn/ui",
|
|
39
|
+
skillPath: "skills/shadcn/SKILL.md",
|
|
40
|
+
computedHash: "b864a958765563321f485a96f4d59c0bb6539f4286ab6d8b8b66895b7ebd302d",
|
|
41
|
+
},
|
|
42
|
+
"systematic-debugging": {
|
|
43
|
+
source: "obra/superpowers",
|
|
44
|
+
skillPath: "skills/systematic-debugging/SKILL.md",
|
|
45
|
+
computedHash: "276fb0aadb002ab930cc2a56de89e16995a2e3e70b33587652bf0a5fb123148d",
|
|
46
|
+
},
|
|
47
|
+
tdd: {
|
|
48
|
+
source: "mattpocock/skills",
|
|
49
|
+
skillPath: "skills/engineering/tdd/SKILL.md",
|
|
50
|
+
computedHash: "daf55026f456af104a72fa43247f7bcec89e5127316dc5b1b8d675d6a9d8ce94",
|
|
51
|
+
},
|
|
52
|
+
"vercel-composition-patterns": {
|
|
53
|
+
source: "vercel-labs/agent-skills",
|
|
54
|
+
skillPath: "skills/composition-patterns/SKILL.md",
|
|
55
|
+
computedHash: "575757e3e25761c8c562d6e395d29f0b76c98b1273c0bd72d88e6ab1bc9c7d42",
|
|
56
|
+
},
|
|
57
|
+
"vercel-react-best-practices": {
|
|
58
|
+
source: "vercel-labs/agent-skills",
|
|
59
|
+
skillPath: "skills/react-best-practices/SKILL.md",
|
|
60
|
+
computedHash: "ca7b0c0c6e5f2750043f7f0cd72d16ac4e2abc48f9b5500d047a4b77a2506212",
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
export const SKILLS_CLI = "skills@1.7.4";
|
|
64
|
+
export const SKILLS_LOCK = "skills-lock.json";
|
|
65
|
+
export const SKILLS_DIR = ".agents/skills";
|
|
66
|
+
export const CLAUDE_SKILLS = ".claude/skills";
|
|
67
|
+
const GITIGNORE = ".gitignore";
|
|
68
|
+
const AGENT_GITIGNORE = [
|
|
69
|
+
"!/.claude/",
|
|
70
|
+
"/.claude/*",
|
|
71
|
+
"!/.claude/settings.json",
|
|
72
|
+
"!/.claude/rules/",
|
|
73
|
+
`!/${CLAUDE_SKILLS}/`,
|
|
74
|
+
"!/.claude/workflows/",
|
|
75
|
+
`/${SKILLS_DIR}/`,
|
|
76
|
+
];
|
|
77
|
+
function withoutDependencies(names) {
|
|
78
|
+
const gone = Object.fromEntries(names.map((name) => [name, null]));
|
|
79
|
+
return { dependencies: gone, devDependencies: gone };
|
|
80
|
+
}
|
|
81
|
+
const STRICT = {
|
|
82
|
+
strict: true,
|
|
83
|
+
noUncheckedIndexedAccess: true,
|
|
84
|
+
noImplicitOverride: true,
|
|
85
|
+
erasableSyntaxOnly: true,
|
|
86
|
+
verbatimModuleSyntax: true,
|
|
87
|
+
forceConsistentCasingInFileNames: true,
|
|
88
|
+
};
|
|
89
|
+
export const OVERLAYS = {
|
|
90
|
+
base: {
|
|
91
|
+
all: {
|
|
92
|
+
json: {
|
|
93
|
+
"package.json": {
|
|
94
|
+
type: "module",
|
|
95
|
+
scripts: { typecheck: "pnpm -r --if-present check-types && tsc" },
|
|
96
|
+
},
|
|
97
|
+
"tsconfig.json": {
|
|
98
|
+
include: ["*.ts"],
|
|
99
|
+
compilerOptions: { noEmit: true, allowImportingTsExtensions: true },
|
|
100
|
+
},
|
|
101
|
+
"packages/config/tsconfig.base.json": { compilerOptions: STRICT },
|
|
102
|
+
"apps/web/package.json": {
|
|
103
|
+
type: "module",
|
|
104
|
+
scripts: { "check-types": "pnpm run typegen && tsc --noEmit" },
|
|
105
|
+
},
|
|
106
|
+
"apps/web/tsconfig.json": { compilerOptions: STRICT },
|
|
107
|
+
},
|
|
108
|
+
packages: {
|
|
109
|
+
"package.json": {
|
|
110
|
+
devDependencies: [
|
|
111
|
+
"@commitlint/cli",
|
|
112
|
+
"@commitlint/config-conventional",
|
|
113
|
+
"@commitlint/types",
|
|
114
|
+
"@eslint-community/eslint-plugin-eslint-comments",
|
|
115
|
+
"@eslint-react/eslint-plugin",
|
|
116
|
+
"eslint-plugin-barrel-files",
|
|
117
|
+
"eslint-plugin-check-file",
|
|
118
|
+
"eslint-plugin-depend",
|
|
119
|
+
"eslint-plugin-jsdoc",
|
|
120
|
+
"eslint-plugin-perfectionist",
|
|
121
|
+
"eslint-plugin-react-you-might-not-need-an-effect",
|
|
122
|
+
"eslint-plugin-security",
|
|
123
|
+
"eslint-plugin-sonarjs",
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
skills: [
|
|
128
|
+
"agent-browser",
|
|
129
|
+
"find-skills",
|
|
130
|
+
"grill-me",
|
|
131
|
+
"grilling",
|
|
132
|
+
"systematic-debugging",
|
|
133
|
+
"tdd",
|
|
134
|
+
"vercel-composition-patterns",
|
|
135
|
+
"vercel-react-best-practices",
|
|
136
|
+
],
|
|
137
|
+
gitignore: AGENT_GITIGNORE,
|
|
138
|
+
prune: { paths: ["bunfig.toml", "apps/web/bunfig.toml"] },
|
|
139
|
+
},
|
|
140
|
+
"react-router": {
|
|
141
|
+
json: {
|
|
142
|
+
"apps/web/package.json": {
|
|
143
|
+
scripts: { typegen: "react-router typegen" },
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
next: {
|
|
148
|
+
json: {
|
|
149
|
+
"apps/web/package.json": { scripts: { typegen: "next typegen" } },
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
standalone: {
|
|
154
|
+
all: {
|
|
155
|
+
json: {
|
|
156
|
+
"package.json": {
|
|
157
|
+
scripts: {
|
|
158
|
+
lint: "pnpm -r --if-present typegen && oxlint --max-warnings 0",
|
|
159
|
+
format: "oxfmt",
|
|
160
|
+
"format:check": "oxfmt --check",
|
|
161
|
+
test: "vitest run",
|
|
162
|
+
prepare: "husky || true",
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
packages: {
|
|
167
|
+
"package.json": {
|
|
168
|
+
devDependencies: [
|
|
169
|
+
"husky",
|
|
170
|
+
"lint-staged",
|
|
171
|
+
"oxfmt",
|
|
172
|
+
"oxlint",
|
|
173
|
+
"oxlint-tsgolint",
|
|
174
|
+
"vitest",
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
"vite-plus": {
|
|
181
|
+
all: {
|
|
182
|
+
json: {
|
|
183
|
+
"package.json": {
|
|
184
|
+
scripts: {
|
|
185
|
+
lint: "pnpm -r --if-present typegen && vp lint --max-warnings 0",
|
|
186
|
+
format: "vp fmt",
|
|
187
|
+
"format:check": "vp fmt --check",
|
|
188
|
+
test: "vp test",
|
|
189
|
+
prepare: "vp config --no-agent || true",
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
packages: { "package.json": { devDependencies: ["vite-plus"] } },
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
tailwind: {
|
|
197
|
+
all: {
|
|
198
|
+
json: {
|
|
199
|
+
"package.json": {
|
|
200
|
+
scripts: {
|
|
201
|
+
"ui:add": "pnpm --dir packages/ui exec shadcn add",
|
|
202
|
+
"postui:add": "pnpm lint --fix || pnpm lint --fix && pnpm format",
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
packages: {
|
|
207
|
+
"package.json": {
|
|
208
|
+
devDependencies: ["eslint-plugin-better-tailwindcss"],
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
skills: ["shadcn"],
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
stylex: {
|
|
215
|
+
all: {
|
|
216
|
+
packages: {
|
|
217
|
+
"package.json": {
|
|
218
|
+
devDependencies: ["@stylexjs/eslint-plugin", "@stylexjs/unplugin"],
|
|
219
|
+
},
|
|
220
|
+
"apps/web/package.json": { dependencies: ["@stylexjs/stylex"] },
|
|
221
|
+
},
|
|
222
|
+
prune: {
|
|
223
|
+
paths: [
|
|
224
|
+
"packages/ui",
|
|
225
|
+
"apps/web/components.json",
|
|
226
|
+
"apps/web/src/components",
|
|
227
|
+
],
|
|
228
|
+
json: {
|
|
229
|
+
"apps/web/package.json": withoutDependencies([
|
|
230
|
+
"@{{name}}/ui",
|
|
231
|
+
"lucide-react",
|
|
232
|
+
"next-themes",
|
|
233
|
+
"sonner",
|
|
234
|
+
"tailwindcss",
|
|
235
|
+
"@tailwindcss/vite",
|
|
236
|
+
"@tailwindcss/postcss",
|
|
237
|
+
]),
|
|
238
|
+
"apps/web/tsconfig.json": {
|
|
239
|
+
compilerOptions: { paths: { "@{{name}}/ui/*": null } },
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
"react-router": {
|
|
245
|
+
packages: {
|
|
246
|
+
"apps/web/package.json": { devDependencies: ["@stylexjs/unplugin"] },
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
next: {
|
|
250
|
+
packages: {
|
|
251
|
+
"apps/web/package.json": {
|
|
252
|
+
devDependencies: [
|
|
253
|
+
"@stylexjs/babel-plugin",
|
|
254
|
+
"@stylexjs/postcss-plugin",
|
|
255
|
+
"postcss",
|
|
256
|
+
],
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
// Turbopack runs an .mjs PostCSS config in its own runtime, where
|
|
260
|
+
// import.meta.dirname in the babel config it imports is undefined.
|
|
261
|
+
prune: { paths: ["apps/web/postcss.config.mjs"] },
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
export const STATE_FILE = ".creact/state.json";
|
|
266
|
+
const DEPENDENCY_MAPS = ["dependencies", "devDependencies"];
|
|
267
|
+
function scopes(stack) {
|
|
268
|
+
return ["all", stack];
|
|
269
|
+
}
|
|
270
|
+
export async function loadLayer(layerDir, ids, stack) {
|
|
271
|
+
const files = new Map();
|
|
272
|
+
const roots = ids.flatMap((id) => scopes(stack).map((scope) => path.join(layerDir, id, scope)));
|
|
273
|
+
for (const root of roots.filter((dir) => existsSync(dir))) {
|
|
274
|
+
const entries = await readdir(root, {
|
|
275
|
+
recursive: true,
|
|
276
|
+
withFileTypes: true,
|
|
277
|
+
});
|
|
278
|
+
for (const entry of entries.filter((dirent) => dirent.isFile())) {
|
|
279
|
+
const source = path.join(entry.parentPath, entry.name);
|
|
280
|
+
const relative = path.relative(root, source).split(path.sep).join("/");
|
|
281
|
+
if (!relative.endsWith(".layer")) {
|
|
282
|
+
throw new Error(`${source} needs the .layer suffix`);
|
|
283
|
+
}
|
|
284
|
+
const file = relative.slice(0, -".layer".length);
|
|
285
|
+
const content = await readFile(source, "utf8");
|
|
286
|
+
const earlier = files.get(file);
|
|
287
|
+
if (earlier === undefined)
|
|
288
|
+
files.set(file, content);
|
|
289
|
+
else if (file.endsWith(".md")) {
|
|
290
|
+
files.set(file, `${earlier.trimEnd()}\n\n${content}`);
|
|
291
|
+
}
|
|
292
|
+
else
|
|
293
|
+
throw new Error(`${source} is already in an earlier overlay`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return files;
|
|
297
|
+
}
|
|
298
|
+
export function plan(files, ids, project) {
|
|
299
|
+
const vars = {
|
|
300
|
+
name: project.name,
|
|
301
|
+
monorepo: MONOREPOS[project.monorepo].summary,
|
|
302
|
+
...TOOLCHAINS[project.toolchain].imports,
|
|
303
|
+
};
|
|
304
|
+
const parts = ids.flatMap((id) => scopes(project.stack).flatMap((scope) => OVERLAYS[id][scope] ?? []));
|
|
305
|
+
const skills = [
|
|
306
|
+
...new Set(parts.flatMap((part) => part.skills ?? [])),
|
|
307
|
+
].toSorted();
|
|
308
|
+
const lock = {
|
|
309
|
+
version: 1,
|
|
310
|
+
skills: Object.fromEntries(skills.map((id) => [
|
|
311
|
+
id,
|
|
312
|
+
{
|
|
313
|
+
source: CREACT_LOCK[id].source,
|
|
314
|
+
sourceType: "github",
|
|
315
|
+
skillPath: CREACT_LOCK[id].skillPath,
|
|
316
|
+
computedHash: CREACT_LOCK[id].computedHash,
|
|
317
|
+
},
|
|
318
|
+
])),
|
|
319
|
+
};
|
|
320
|
+
const seeds = skills.length > 0
|
|
321
|
+
? [
|
|
322
|
+
{
|
|
323
|
+
kind: "file",
|
|
324
|
+
path: SKILLS_LOCK,
|
|
325
|
+
content: `${JSON.stringify(lock, null, 2)}\n`,
|
|
326
|
+
seed: true,
|
|
327
|
+
},
|
|
328
|
+
]
|
|
329
|
+
: [];
|
|
330
|
+
const linkOps = skills.map((id) => ({
|
|
331
|
+
kind: "link",
|
|
332
|
+
skill: id,
|
|
333
|
+
path: `${CLAUDE_SKILLS}/${id}`,
|
|
334
|
+
target: `../../${SKILLS_DIR}/${id}`,
|
|
335
|
+
}));
|
|
336
|
+
const ignored = parts.flatMap((part) => part.gitignore ?? []);
|
|
337
|
+
const ignoreOps = ignored.length > 0 ? [{ kind: "gitignore", lines: ignored }] : [];
|
|
338
|
+
const paths = parts
|
|
339
|
+
.flatMap((part) => part.prune?.paths ?? [])
|
|
340
|
+
.map((entry) => render(entry, entry, vars));
|
|
341
|
+
const removals = parts.flatMap((part) => JSON_FILES.flatMap((file) => {
|
|
342
|
+
const removal = part.prune?.json?.[file];
|
|
343
|
+
return removal === undefined
|
|
344
|
+
? []
|
|
345
|
+
: [{ file, removal: renderKeys(removal, vars) }];
|
|
346
|
+
}));
|
|
347
|
+
const fileOps = [
|
|
348
|
+
...[...files].map(([file, content]) => ({
|
|
349
|
+
kind: "file",
|
|
350
|
+
path: render(file, file, vars),
|
|
351
|
+
content: render(file, content, vars),
|
|
352
|
+
})),
|
|
353
|
+
...seeds,
|
|
354
|
+
].toSorted((a, b) => a.path.localeCompare(b.path));
|
|
355
|
+
const jsonOps = JSON_FILES.flatMap((file) => {
|
|
356
|
+
let patch = {};
|
|
357
|
+
for (const part of parts)
|
|
358
|
+
patch = merge(patch, part.json?.[file] ?? {});
|
|
359
|
+
return Object.keys(patch).length > 0
|
|
360
|
+
? [{ kind: "json", path: file, patch }]
|
|
361
|
+
: [];
|
|
362
|
+
});
|
|
363
|
+
const depsOps = MANIFESTS.flatMap((manifest) => {
|
|
364
|
+
const listed = (key) => parts.flatMap((part) => part.packages?.[manifest]?.[key] ?? []);
|
|
365
|
+
const dependencies = listed("dependencies");
|
|
366
|
+
const devDependencies = listed("devDependencies");
|
|
367
|
+
return dependencies.length + devDependencies.length > 0
|
|
368
|
+
? [{ kind: "deps", manifest, dependencies, devDependencies }]
|
|
369
|
+
: [];
|
|
370
|
+
});
|
|
371
|
+
return [
|
|
372
|
+
...(paths.length + removals.length > 0
|
|
373
|
+
? [{ kind: "prune", paths, json: removals }]
|
|
374
|
+
: []),
|
|
375
|
+
...fileOps,
|
|
376
|
+
...linkOps,
|
|
377
|
+
...ignoreOps,
|
|
378
|
+
...jsonOps,
|
|
379
|
+
...depsOps,
|
|
380
|
+
];
|
|
381
|
+
}
|
|
382
|
+
export function render(file, content, vars) {
|
|
383
|
+
return content.replaceAll(/\{\{(\w+)\}\}/g, (_match, name) => {
|
|
384
|
+
const value = vars[name];
|
|
385
|
+
if (value === undefined)
|
|
386
|
+
throw new Error(`${file}: unknown {{${name}}}`);
|
|
387
|
+
return value;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
function renderKeys(removal, vars) {
|
|
391
|
+
return Object.fromEntries(Object.entries(removal).map(([key, value]) => [
|
|
392
|
+
render(key, key, vars),
|
|
393
|
+
value === null ? null : renderKeys(value, vars),
|
|
394
|
+
]));
|
|
395
|
+
}
|
|
396
|
+
export function merge(doc, patch) {
|
|
397
|
+
const result = { ...doc };
|
|
398
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
399
|
+
const current = result[key];
|
|
400
|
+
result[key] =
|
|
401
|
+
isObject(current) && isObject(value) ? merge(current, value) : value;
|
|
402
|
+
}
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
function remove(doc, removal) {
|
|
406
|
+
const removed = [];
|
|
407
|
+
const kept = Object.entries(doc).flatMap(([key, value]) => {
|
|
408
|
+
const rule = removal[key];
|
|
409
|
+
if (rule === null) {
|
|
410
|
+
removed.push(key);
|
|
411
|
+
return [];
|
|
412
|
+
}
|
|
413
|
+
if (rule === undefined || !isObject(value))
|
|
414
|
+
return [[key, value]];
|
|
415
|
+
const [inner, gone] = remove(value, rule);
|
|
416
|
+
removed.push(...gone);
|
|
417
|
+
return [[key, inner]];
|
|
418
|
+
});
|
|
419
|
+
return [Object.fromEntries(kept), removed];
|
|
420
|
+
}
|
|
421
|
+
export async function apply(dir, ops, prior) {
|
|
422
|
+
const files = {};
|
|
423
|
+
const hashes = {};
|
|
424
|
+
let pruned = [];
|
|
425
|
+
const missing = [];
|
|
426
|
+
let locked;
|
|
427
|
+
for (const op of ops) {
|
|
428
|
+
switch (op.kind) {
|
|
429
|
+
case "prune": {
|
|
430
|
+
if (prior.kind === "upstream-only")
|
|
431
|
+
pruned = await applyPrune(dir, op);
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case "file": {
|
|
435
|
+
const result = op.seed
|
|
436
|
+
? await seedFile(dir, op.path, op.content)
|
|
437
|
+
: await applyFile(dir, op.path, op.content, prior);
|
|
438
|
+
files[op.path] = result.outcome;
|
|
439
|
+
if (result.hash !== undefined)
|
|
440
|
+
hashes[op.path] = result.hash;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case "link": {
|
|
444
|
+
locked ??= await lockedSkills(dir);
|
|
445
|
+
if (locked.has(op.skill)) {
|
|
446
|
+
files[op.path] = await applyLink(dir, op.path, op.target, prior);
|
|
447
|
+
}
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
case "gitignore": {
|
|
451
|
+
files[GITIGNORE] = await applyIgnore(dir, op.lines);
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
case "json": {
|
|
455
|
+
files[op.path] = await applyJson(path.join(dir, op.path), op.patch);
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "deps": {
|
|
459
|
+
const pkg = await readJson(path.join(dir, op.manifest));
|
|
460
|
+
const present = new Set(DEPENDENCY_MAPS.flatMap((key) => Object.keys(objectAt(pkg, key))));
|
|
461
|
+
const absent = (names) => names.filter((name) => !present.has(name));
|
|
462
|
+
const addition = {
|
|
463
|
+
manifest: op.manifest,
|
|
464
|
+
dependencies: absent(op.dependencies),
|
|
465
|
+
devDependencies: absent(op.devDependencies),
|
|
466
|
+
};
|
|
467
|
+
if (addition.dependencies.length + addition.devDependencies.length >
|
|
468
|
+
0) {
|
|
469
|
+
missing.push(addition);
|
|
470
|
+
}
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return { files, hashes, pruned, missing };
|
|
476
|
+
}
|
|
477
|
+
async function applyPrune(dir, prune) {
|
|
478
|
+
const paths = prune.paths.filter((entry) => existsSync(path.join(dir, entry)));
|
|
479
|
+
for (const entry of paths) {
|
|
480
|
+
await rm(path.join(dir, entry), { recursive: true, force: true });
|
|
481
|
+
}
|
|
482
|
+
const keys = new Set();
|
|
483
|
+
for (const { file, removal } of prune.json) {
|
|
484
|
+
const target = path.join(dir, file);
|
|
485
|
+
const [doc, removed] = remove(await readJson(target), removal);
|
|
486
|
+
if (removed.length > 0) {
|
|
487
|
+
await writeAtomic(target, `${JSON.stringify(doc, null, 2)}\n`);
|
|
488
|
+
for (const key of removed)
|
|
489
|
+
keys.add(key);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return [...paths, ...keys];
|
|
493
|
+
}
|
|
494
|
+
async function applyFile(dir, file, content, prior) {
|
|
495
|
+
const target = path.join(dir, file);
|
|
496
|
+
const current = await readText(target);
|
|
497
|
+
const recorded = prior.kind === "recorded" ? prior.hashes[file] : undefined;
|
|
498
|
+
if (current === content)
|
|
499
|
+
return { outcome: "unchanged", hash: sha256(content) };
|
|
500
|
+
if (current === undefined ||
|
|
501
|
+
prior.kind === "upstream-only" ||
|
|
502
|
+
sha256(current) === recorded) {
|
|
503
|
+
await writeAtomic(target, content);
|
|
504
|
+
return { outcome: "written", hash: sha256(content) };
|
|
505
|
+
}
|
|
506
|
+
return { outcome: "kept", hash: recorded };
|
|
507
|
+
}
|
|
508
|
+
// The project's lock decides which links exist, so a skill it removed stays
|
|
509
|
+
// removed on a rerun.
|
|
510
|
+
export async function lockedSkills(dir) {
|
|
511
|
+
const lock = await readJson(path.join(dir, SKILLS_LOCK));
|
|
512
|
+
return new Set(Object.keys(isObject(lock.skills) ? lock.skills : {}));
|
|
513
|
+
}
|
|
514
|
+
// Written once. After that the file belongs to the tool that maintains it,
|
|
515
|
+
// so no hash is recorded and a rerun leaves it as it finds it.
|
|
516
|
+
async function seedFile(dir, file, content) {
|
|
517
|
+
const target = path.join(dir, file);
|
|
518
|
+
if (existsSync(target))
|
|
519
|
+
return { outcome: "unchanged", hash: undefined };
|
|
520
|
+
await writeAtomic(target, content);
|
|
521
|
+
return { outcome: "written", hash: undefined };
|
|
522
|
+
}
|
|
523
|
+
async function applyLink(dir, file, target, prior) {
|
|
524
|
+
const link = path.join(dir, file);
|
|
525
|
+
const current = await lstat(link).catch(() => null);
|
|
526
|
+
if (current?.isSymbolicLink() && (await readlink(link)) === target) {
|
|
527
|
+
return "unchanged";
|
|
528
|
+
}
|
|
529
|
+
if (current !== null && prior.kind === "recorded")
|
|
530
|
+
return "kept";
|
|
531
|
+
await rm(link, { recursive: true, force: true });
|
|
532
|
+
await mkdir(path.dirname(link), { recursive: true });
|
|
533
|
+
await symlink(target, link);
|
|
534
|
+
return "written";
|
|
535
|
+
}
|
|
536
|
+
async function applyIgnore(dir, block) {
|
|
537
|
+
const file = path.join(dir, GITIGNORE);
|
|
538
|
+
const current = (await readText(file)) ?? "";
|
|
539
|
+
if (containsBlock(current.split("\n"), block))
|
|
540
|
+
return "unchanged";
|
|
541
|
+
await writeAtomic(file, withBlock(current, block));
|
|
542
|
+
return "written";
|
|
543
|
+
}
|
|
544
|
+
function containsBlock(lines, block) {
|
|
545
|
+
return lines.some((_, at) => block.every((line, i) => lines[at + i] === line));
|
|
546
|
+
}
|
|
547
|
+
function withBlock(current, block) {
|
|
548
|
+
const lines = current.split("\n");
|
|
549
|
+
const own = new Set(block);
|
|
550
|
+
const first = lines.findIndex((line) => own.has(line));
|
|
551
|
+
if (first === -1) {
|
|
552
|
+
const head = current === "" ? "" : `${current.trimEnd()}\n\n`;
|
|
553
|
+
return `${head}${block.join("\n")}\n`;
|
|
554
|
+
}
|
|
555
|
+
const rest = lines.slice(first).filter((line) => !own.has(line));
|
|
556
|
+
return [...lines.slice(0, first), ...block, ...rest].join("\n");
|
|
557
|
+
}
|
|
558
|
+
async function applyJson(file, patch) {
|
|
559
|
+
const current = await readJson(file);
|
|
560
|
+
const next = merge(current, patch);
|
|
561
|
+
if (JSON.stringify(next) === JSON.stringify(current))
|
|
562
|
+
return "unchanged";
|
|
563
|
+
await writeAtomic(file, `${JSON.stringify(next, null, 2)}\n`);
|
|
564
|
+
return "written";
|
|
565
|
+
}
|
|
566
|
+
export async function readState(dir) {
|
|
567
|
+
const file = path.join(dir, STATE_FILE);
|
|
568
|
+
if (!existsSync(file))
|
|
569
|
+
return undefined;
|
|
570
|
+
const raw = await readJson(file);
|
|
571
|
+
const { schema, creact, name, stack, styling, monorepo, extras, hashes } = raw;
|
|
572
|
+
// Projects made before Vite+ was a choice have no toolchain recorded.
|
|
573
|
+
const toolchain = Object.hasOwn(raw, "toolchain")
|
|
574
|
+
? raw.toolchain
|
|
575
|
+
: "standalone";
|
|
576
|
+
if (schema === 1) {
|
|
577
|
+
throw new UsageError(`${file} comes from the first create-creact, which made single-package projects. This one makes monorepos and can't update it.`);
|
|
578
|
+
}
|
|
579
|
+
const valid = schema === 2 &&
|
|
580
|
+
typeof creact === "string" &&
|
|
581
|
+
typeof name === "string" &&
|
|
582
|
+
isProjectName(name) &&
|
|
583
|
+
typeof stack === "string" &&
|
|
584
|
+
Object.hasOwn(STACKS, stack) &&
|
|
585
|
+
typeof styling === "string" &&
|
|
586
|
+
isBuilt(STYLINGS, styling) &&
|
|
587
|
+
typeof monorepo === "string" &&
|
|
588
|
+
isBuilt(MONOREPOS, monorepo) &&
|
|
589
|
+
typeof toolchain === "string" &&
|
|
590
|
+
isBuilt(TOOLCHAINS, toolchain) &&
|
|
591
|
+
Array.isArray(extras) &&
|
|
592
|
+
extras.every((id) => typeof id === "string" && isBuilt(EXTRAS, id)) &&
|
|
593
|
+
isObject(hashes) &&
|
|
594
|
+
Object.values(hashes).every((hash) => typeof hash === "string");
|
|
595
|
+
if (!valid) {
|
|
596
|
+
throw new UsageError(`${file} isn't a create-creact state file`);
|
|
597
|
+
}
|
|
598
|
+
return { ...raw, toolchain };
|
|
599
|
+
}
|
|
600
|
+
export async function writeState(dir, state) {
|
|
601
|
+
const file = path.join(dir, STATE_FILE);
|
|
602
|
+
if (existsSync(file)) {
|
|
603
|
+
const current = await readJson(file);
|
|
604
|
+
if (JSON.stringify(current) === JSON.stringify(state))
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
await writeAtomic(file, `${JSON.stringify(state, null, 2)}\n`);
|
|
608
|
+
}
|
|
609
|
+
export function isObject(value) {
|
|
610
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
611
|
+
}
|
|
612
|
+
function objectAt(doc, key) {
|
|
613
|
+
const value = doc[key];
|
|
614
|
+
return isObject(value) ? value : {};
|
|
615
|
+
}
|
|
616
|
+
function sha256(content) {
|
|
617
|
+
return createHash("sha256").update(content).digest("hex");
|
|
618
|
+
}
|
|
619
|
+
async function readText(file) {
|
|
620
|
+
return existsSync(file) ? readFile(file, "utf8") : undefined;
|
|
621
|
+
}
|
|
622
|
+
export async function readJson(file) {
|
|
623
|
+
const parsed = JSON.parse(await readFile(file, "utf8"));
|
|
624
|
+
if (!isObject(parsed))
|
|
625
|
+
throw new UsageError(`${file} isn't a JSON object`);
|
|
626
|
+
return parsed;
|
|
627
|
+
}
|
|
628
|
+
async function writeAtomic(file, content) {
|
|
629
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
630
|
+
const temp = `${file}.creact-tmp`;
|
|
631
|
+
await writeFile(temp, content);
|
|
632
|
+
await rename(temp, file);
|
|
633
|
+
}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { tmpdir, userInfo } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { create } from "./create.js";
|
|
6
|
+
import { parseRequest, UsageError } from "./options.js";
|
|
7
|
+
import { CommandFailed, Log } from "./shell.js";
|
|
8
|
+
import { pickMode, renderer } from "./ui.js";
|
|
9
|
+
const EXIT = { commandFailed: 1, usage: 2 };
|
|
10
|
+
const LAYER_DIR = fileURLToPath(new URL("../layer", import.meta.url));
|
|
11
|
+
const argv = process.argv.slice(2);
|
|
12
|
+
const cwd = process.cwd();
|
|
13
|
+
// Read before parsing, so a usage error still answers in JSON.
|
|
14
|
+
const wantsJson = argv.includes("--json");
|
|
15
|
+
const parsed = parseRequest(argv, cwd);
|
|
16
|
+
switch (parsed.kind) {
|
|
17
|
+
case "help": {
|
|
18
|
+
process.stdout.write(parsed.text);
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
case "usage": {
|
|
22
|
+
usage(parsed.message);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case "run": {
|
|
26
|
+
const { request } = parsed;
|
|
27
|
+
const log = new Log(logFile(request.dir));
|
|
28
|
+
const ui = renderer(pickMode(wantsJson, process.stdout, process.env), log, cwd);
|
|
29
|
+
try {
|
|
30
|
+
ui.succeeded(await create(request, LAYER_DIR, { progress: ui, log }));
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error instanceof UsageError) {
|
|
34
|
+
ui.failed(error, "usage");
|
|
35
|
+
process.exitCode = EXIT.usage;
|
|
36
|
+
}
|
|
37
|
+
else if (error instanceof CommandFailed) {
|
|
38
|
+
ui.failed(error, "command");
|
|
39
|
+
process.exitCode = EXIT.commandFailed;
|
|
40
|
+
}
|
|
41
|
+
else
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function usage(message) {
|
|
48
|
+
if (wantsJson) {
|
|
49
|
+
process.stdout.write(`${JSON.stringify({ ok: false, kind: "usage", message, log: null }, null, 2)}\n`);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
process.stderr.write(`create-creact: ${message}\n`);
|
|
53
|
+
}
|
|
54
|
+
process.exitCode = EXIT.usage;
|
|
55
|
+
}
|
|
56
|
+
// One file per user and project name, in the OS temp dir, so a rerun
|
|
57
|
+
// replaces it, a failure before the project exists still has somewhere to
|
|
58
|
+
// write, and two users sharing /tmp don't collide.
|
|
59
|
+
function logFile(dir) {
|
|
60
|
+
const name = path.basename(dir).replaceAll(/[^\w.-]/g, "_");
|
|
61
|
+
const user = process.getuid?.() ?? userInfo().username;
|
|
62
|
+
return path.join(tmpdir(), `create-creact-${user}-${name}.log`);
|
|
63
|
+
}
|