create-claudecraft 1.0.0 → 2.0.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.
- package/README.md +43 -178
- package/bin/cli.js +186 -1
- package/dist/answers.d.ts +269 -0
- package/dist/answers.js +215 -0
- package/dist/ink-prompts.d.ts +90 -12
- package/dist/ink-prompts.js +371 -330
- package/dist/lettering.d.ts +2 -0
- package/dist/lettering.js +28 -0
- package/lib/apps.mjs +141 -0
- package/lib/github.mjs +24 -0
- package/lib/init.mjs +153 -0
- package/lib/ports.mjs +56 -0
- package/lib/scaffold.mjs +218 -0
- package/lib/upgrade.mjs +164 -0
- package/package.json +39 -33
- package/templates/astro/.editorconfig +15 -0
- package/templates/astro/.env.example +2 -0
- package/templates/astro/.github/workflows/ci.yml +37 -0
- package/templates/astro/.github/workflows/deploy.yml +46 -0
- package/templates/astro/.nvmrc +1 -0
- package/templates/astro/.prettierignore +10 -0
- package/templates/astro/.prettierrc.json +7 -0
- package/templates/astro/.vscode/extensions.json +3 -0
- package/templates/astro/.vscode/settings.json +12 -0
- package/templates/astro/AGENTS.md +100 -0
- package/templates/astro/README.md +42 -0
- package/templates/astro/_CLAUDE.md +1 -0
- package/templates/astro/_claude/launch.json +13 -0
- package/templates/astro/_claude/settings.json +22 -0
- package/templates/astro/_codex/environments/environment.toml +20 -0
- package/templates/astro/_gitignore +31 -0
- package/templates/astro/_npmrc +8 -0
- package/templates/astro/_super.engineering/config.json +8 -0
- package/templates/astro/_zed/tasks.json +5 -0
- package/templates/astro/astro.config.mjs +111 -0
- package/templates/astro/docs/README.md +7 -0
- package/templates/astro/docs/decisions.md +3 -0
- package/templates/astro/docs/inbox.md +7 -0
- package/templates/astro/docs/kit/agent-apps.md +15 -0
- package/templates/astro/docs/kit/analytics.md +13 -0
- package/templates/astro/docs/kit/deploy.md +9 -0
- package/templates/astro/docs/kit/images.md +10 -0
- package/templates/astro/docs/kit/languages.md +8 -0
- package/templates/astro/docs/kit/local.md +16 -0
- package/templates/astro/docs/kit/seo.md +15 -0
- package/templates/astro/docs/kit/structure.md +40 -0
- package/templates/astro/docs/kit/upgrade.md +19 -0
- package/templates/astro/functions/_middleware.js +25 -0
- package/templates/astro/intent.md +40 -0
- package/templates/astro/package.json +46 -0
- package/templates/astro/public/_headers +17 -0
- package/templates/astro/public/_routes.json +5 -0
- package/templates/astro/public/apple-touch-icon.png +0 -0
- package/templates/astro/public/favicon-16.png +0 -0
- package/templates/astro/public/favicon-32.png +0 -0
- package/templates/astro/public/favicon.ico +0 -0
- package/templates/astro/public/favicon.svg +4 -0
- package/templates/astro/public/icon-192.png +0 -0
- package/templates/astro/public/icon-512.png +0 -0
- package/templates/astro/public/og-image.png +0 -0
- package/templates/astro/scripts/chrome.mjs +138 -0
- package/templates/astro/scripts/deploy.sh +69 -0
- package/templates/astro/scripts/generate-icons.mjs +73 -0
- package/templates/astro/scripts/generate-og.mjs +66 -0
- package/templates/astro/scripts/lint-copy.mjs +67 -0
- package/templates/astro/scripts/local-domain.mjs +115 -0
- package/templates/astro/scripts/markdown-pages.mjs +142 -0
- package/templates/astro/scripts/og-template.html +73 -0
- package/templates/astro/scripts/shared.mjs +33 -0
- package/templates/astro/scripts/verify-build.mjs +283 -0
- package/templates/astro/scripts/verify.mjs +19 -0
- package/templates/astro/site.config.mjs +85 -0
- package/templates/astro/src/components/Analytics.astro +49 -0
- package/templates/astro/src/components/Consent.astro +52 -0
- package/templates/astro/src/components/Counter.tsx +20 -0
- package/templates/astro/src/data/README.md +18 -0
- package/templates/astro/src/dev/styleguide.astro +43 -0
- package/templates/astro/src/env.d.ts +1 -0
- package/templates/astro/src/i18n/ar.json +29 -0
- package/templates/astro/src/i18n/de.json +29 -0
- package/templates/astro/src/i18n/en.json +29 -0
- package/templates/astro/src/i18n/es.json +29 -0
- package/templates/astro/src/i18n/fr.json +29 -0
- package/templates/astro/src/i18n/he.json +29 -0
- package/templates/astro/src/i18n/hi.json +29 -0
- package/templates/astro/src/i18n/index.ts +80 -0
- package/templates/astro/src/i18n/it.json +29 -0
- package/templates/astro/src/i18n/ja.json +29 -0
- package/templates/astro/src/i18n/ko.json +29 -0
- package/templates/astro/src/i18n/nl.json +29 -0
- package/templates/astro/src/i18n/pl.json +29 -0
- package/templates/astro/src/i18n/pt-br.json +29 -0
- package/templates/astro/src/i18n/ro.json +29 -0
- package/templates/astro/src/i18n/ru.json +29 -0
- package/templates/astro/src/i18n/sv.json +29 -0
- package/templates/astro/src/i18n/tr.json +29 -0
- package/templates/astro/src/i18n/uk.json +29 -0
- package/templates/astro/src/i18n/zh-cn.json +29 -0
- package/templates/astro/src/layouts/BaseLayout.astro +205 -0
- package/templates/astro/src/lib/cn.ts +7 -0
- package/templates/astro/src/lib/consent.ts +32 -0
- package/templates/astro/src/lib/seo.ts +18 -0
- package/templates/astro/src/lib/theme.ts +5 -0
- package/templates/astro/src/pages/404.astro +7 -0
- package/templates/astro/src/pages/[...locale]/404.astro +25 -0
- package/templates/astro/src/pages/[...locale]/about.astro +25 -0
- package/templates/astro/src/pages/[...locale]/index.astro +30 -0
- package/templates/astro/src/pages/[...locale]/privacy.astro +28 -0
- package/templates/astro/src/pages/robots.txt.ts +16 -0
- package/templates/astro/src/pages/site.webmanifest.ts +26 -0
- package/templates/astro/src/styles/global.css +163 -0
- package/templates/astro/t3.json +22 -0
- package/templates/astro/tsconfig.json +13 -0
- package/templates/astro/wrangler.toml +8 -0
- package/templates/react/.editorconfig +15 -0
- package/templates/react/.env.example +2 -0
- package/templates/react/.nvmrc +1 -0
- package/templates/react/.prettierignore +11 -0
- package/templates/react/.prettierrc.json +5 -0
- package/templates/react/.vscode/extensions.json +3 -0
- package/templates/react/.vscode/settings.json +4 -0
- package/templates/react/AGENTS.md +64 -0
- package/templates/react/README.md +31 -0
- package/templates/react/_CLAUDE.md +1 -0
- package/templates/react/_claude/launch.json +13 -0
- package/templates/react/_claude/settings.json +21 -0
- package/templates/react/_codex/environments/environment.toml +20 -0
- package/templates/react/_gitignore +17 -0
- package/templates/react/_npmrc +8 -0
- package/templates/react/_super.engineering/config.json +8 -0
- package/templates/react/_zed/tasks.json +5 -0
- package/templates/react/docs/decisions.md +3 -0
- package/templates/react/docs/inbox.md +7 -0
- package/templates/react/index.html +14 -0
- package/templates/react/package.json +47 -0
- package/templates/react/public/favicon.svg +5 -0
- package/templates/react/scripts/lint-copy.mjs +46 -0
- package/templates/react/scripts/lint-tokens.mjs +44 -0
- package/templates/react/scripts/local-domain.mjs +115 -0
- package/templates/react/scripts/verify.mjs +20 -0
- package/templates/react/site.config.mjs +23 -0
- package/templates/react/src/App.test.tsx +15 -0
- package/templates/react/src/App.tsx +21 -0
- package/templates/react/src/components/ErrorBoundary.tsx +42 -0
- package/templates/react/src/components/SkipLink.tsx +10 -0
- package/templates/react/src/config/site.ts +5 -0
- package/templates/react/src/lib/cn.ts +7 -0
- package/templates/react/src/main.tsx +13 -0
- package/templates/react/src/pages/HomePage.tsx +71 -0
- package/templates/react/src/pages/NotFoundPage.tsx +21 -0
- package/templates/react/src/styles/global.css +96 -0
- package/templates/react/src/test-setup.ts +1 -0
- package/templates/react/t3.json +22 -0
- package/templates/{base → react}/tsconfig.json +3 -3
- package/templates/react/vite.config.ts +26 -0
- package/templates/shared/_claude/hooks/design-lint.mjs +67 -0
- package/templates/shared/_claude/skills/design-quality/SKILL.md +113 -0
- package/templates/shared/_claude/skills/design-quality/references/composition-palette.md +87 -0
- package/templates/shared/_claude/skills/design-quality/references/copy-tells.md +49 -0
- package/templates/shared/_claude/skills/design-quality/references/visual-critic.md +75 -0
- package/templates/shared/design.md +148 -0
- package/templates/shared/intent.md +28 -0
- package/dist/constants.d.ts +0 -71
- package/dist/constants.js +0 -128
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -229
- package/dist/prompts.d.ts +0 -16
- package/dist/prompts.js +0 -434
- package/dist/scaffold.d.ts +0 -19
- package/dist/scaffold.js +0 -303
- package/dist/ui.d.ts +0 -27
- package/dist/ui.js +0 -254
- package/templates/app/App.tsx +0 -21
- package/templates/base/CLAUDE.md +0 -332
- package/templates/base/eslint.config.js +0 -28
- package/templates/base/index.html +0 -17
- package/templates/base/package.json +0 -43
- package/templates/base/postcss.config.js +0 -6
- package/templates/base/tailwind.config.js +0 -81
- package/templates/base/vite.config.ts +0 -16
- package/templates/commands/brainstorm.md +0 -6
- package/templates/commands/build.md +0 -41
- package/templates/commands/execute-plan.md +0 -6
- package/templates/commands/lint.md +0 -41
- package/templates/commands/ralph.md +0 -113
- package/templates/commands/typecheck.md +0 -44
- package/templates/commands/write-plan.md +0 -6
- package/templates/components/ErrorBoundary.tsx +0 -49
- package/templates/components/ui/Button.tsx +0 -60
- package/templates/components/ui/CodeBlock.tsx +0 -46
- package/templates/components/ui/CopyCommand.tsx +0 -38
- package/templates/components/ui/FilePreview.tsx +0 -46
- package/templates/components/ui/SkipLink.tsx +0 -7
- package/templates/components/ui/ThemeSelector.tsx +0 -41
- package/templates/components/ui/UICarousel.tsx +0 -309
- package/templates/context/ThemeContext.tsx +0 -61
- package/templates/homepage/HomePage.tsx +0 -534
- package/templates/homepage/NotFoundPage.tsx +0 -17
- package/templates/hooks/README.md +0 -82
- package/templates/hooks/check-branch.js +0 -27
- package/templates/hooks/typecheck-after-edit.js +0 -51
- package/templates/index.css +0 -67
- package/templates/lib/utils.ts +0 -9
- package/templates/main.tsx +0 -16
- package/templates/settings/MCP_SETUP.md +0 -76
- package/templates/settings/settings.json +0 -16
- package/templates/settings/settings.local.json +0 -37
- package/templates/skills/design/a11y-audit/SKILL.md +0 -173
- package/templates/skills/design/design-polish/SKILL.md +0 -75
- package/templates/skills/design/figma-to-code/SKILL.md +0 -157
- package/templates/skills/design/json-ld/SKILL.md +0 -125
- package/templates/skills/design/microcopy/SKILL.md +0 -197
- package/templates/skills/design/og-image/SKILL.md +0 -157
- package/templates/skills/design/ralph-wiggum-loops/SKILL.md +0 -299
- package/templates/skills/design/react-best-practices/SKILL.md +0 -106
- package/templates/skills/design/react-best-practices/references/react-performance-guidelines.md +0 -143
- package/templates/skills/design/seo-review/SKILL.md +0 -96
- package/templates/skills/design/sitemap-generator/SKILL.md +0 -66
- package/templates/skills/design/testing-patterns/SKILL.md +0 -276
- package/templates/skills/design/ui-skills/SKILL.md +0 -85
- package/templates/skills/design/visual-iteration/SKILL.md +0 -88
- package/templates/skills/workflow/brainstorming/SKILL.md +0 -54
- package/templates/skills/workflow/dispatching-parallel-agents/SKILL.md +0 -180
- package/templates/skills/workflow/executing-plans/SKILL.md +0 -76
- package/templates/skills/workflow/finishing-a-development-branch/SKILL.md +0 -200
- package/templates/skills/workflow/receiving-code-review/SKILL.md +0 -213
- package/templates/skills/workflow/requesting-code-review/SKILL.md +0 -105
- package/templates/skills/workflow/requesting-code-review/code-reviewer.md +0 -146
- package/templates/skills/workflow/subagent-driven-development/SKILL.md +0 -240
- package/templates/skills/workflow/subagent-driven-development/code-quality-reviewer-prompt.md +0 -20
- package/templates/skills/workflow/subagent-driven-development/implementer-prompt.md +0 -78
- package/templates/skills/workflow/subagent-driven-development/spec-reviewer-prompt.md +0 -61
- package/templates/skills/workflow/systematic-debugging/CREATION-LOG.md +0 -119
- package/templates/skills/workflow/systematic-debugging/SKILL.md +0 -296
- package/templates/skills/workflow/systematic-debugging/condition-based-waiting-example.ts +0 -158
- package/templates/skills/workflow/systematic-debugging/condition-based-waiting.md +0 -115
- package/templates/skills/workflow/systematic-debugging/defense-in-depth.md +0 -122
- package/templates/skills/workflow/systematic-debugging/find-polluter.sh +0 -63
- package/templates/skills/workflow/systematic-debugging/root-cause-tracing.md +0 -169
- package/templates/skills/workflow/systematic-debugging/test-academic.md +0 -14
- package/templates/skills/workflow/systematic-debugging/test-pressure-1.md +0 -58
- package/templates/skills/workflow/systematic-debugging/test-pressure-2.md +0 -68
- package/templates/skills/workflow/systematic-debugging/test-pressure-3.md +0 -69
- package/templates/skills/workflow/test-driven-development/SKILL.md +0 -371
- package/templates/skills/workflow/test-driven-development/testing-anti-patterns.md +0 -299
- package/templates/skills/workflow/using-git-worktrees/SKILL.md +0 -217
- package/templates/skills/workflow/using-superpowers/SKILL.md +0 -87
- package/templates/skills/workflow/verification-before-completion/SKILL.md +0 -139
- package/templates/skills/workflow/writing-plans/SKILL.md +0 -116
- package/templates/skills/workflow/writing-skills/SKILL.md +0 -655
- package/templates/skills/workflow/writing-skills/anthropic-best-practices.md +0 -1150
- package/templates/skills/workflow/writing-skills/examples/CLAUDE_MD_TESTING.md +0 -189
- package/templates/skills/workflow/writing-skills/graphviz-conventions.dot +0 -172
- package/templates/skills/workflow/writing-skills/persuasion-principles.md +0 -187
- package/templates/skills/workflow/writing-skills/render-graphs.js +0 -168
- package/templates/skills/workflow/writing-skills/testing-skills-with-subagents.md +0 -384
- package/templates/types/index.ts +0 -17
- /package/templates/{vite-env.d.ts → react/src/vite-env.d.ts} +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Header lettering: tall block face drawn for the 76-column frame.
|
|
2
|
+
// Stems are two columns wide and bars one row tall so strokes look even in ~1:2 terminal cells.
|
|
3
|
+
// Odd rows use a lighter shade (scanlines).
|
|
4
|
+
const GLYPHS = {
|
|
5
|
+
C: [' █████', '██ ', '██ ', '██ ', '██ ', '██ ', ' █████'],
|
|
6
|
+
L: ['██ ', '██ ', '██ ', '██ ', '██ ', '██ ', '█████'],
|
|
7
|
+
A: [' ████ ', '██ ██', '██ ██', '██████', '██ ██', '██ ██', '██ ██'],
|
|
8
|
+
U: ['██ ██', '██ ██', '██ ██', '██ ██', '██ ██', '██ ██', ' ████ '],
|
|
9
|
+
D: ['█████ ', '██ ██', '██ ██', '██ ██', '██ ██', '██ ██', '█████ '],
|
|
10
|
+
E: ['██████', '██ ', '██ ', '█████ ', '██ ', '██ ', '██████'],
|
|
11
|
+
R: ['█████ ', '██ ██', '██ ██', '█████ ', '██ ██ ', '██ ██', '██ ██'],
|
|
12
|
+
F: ['██████', '██ ', '██ ', '█████ ', '██ ', '██ ', '██ '],
|
|
13
|
+
T: ['██████', ' ██ ', ' ██ ', ' ██ ', ' ██ ', ' ██ ', ' ██ '],
|
|
14
|
+
' ': [' ', ' ', ' ', ' ', ' ', ' ', ' '],
|
|
15
|
+
};
|
|
16
|
+
/** `text` in the header face: 7 rows, one column between letters, odd rows lighter. */
|
|
17
|
+
export function lettering(text) {
|
|
18
|
+
const glyphs = [...text].map((ch) => {
|
|
19
|
+
const glyph = GLYPHS[ch];
|
|
20
|
+
if (!glyph)
|
|
21
|
+
throw new Error(`No header glyph for "${ch}".`);
|
|
22
|
+
return glyph;
|
|
23
|
+
});
|
|
24
|
+
return glyphs[0].map((_, row) => {
|
|
25
|
+
const line = glyphs.map((glyph) => glyph[row]).join(' ');
|
|
26
|
+
return (row % 2 ? line.replaceAll('█', '▓') : line).trimEnd();
|
|
27
|
+
});
|
|
28
|
+
}
|
package/lib/apps.mjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Agent apps a new project can be added to. Each row names the template files that belong to the
|
|
2
|
+
// app (copied only when picked) and how to open or register the project in it.
|
|
3
|
+
// With CLAUDECRAFT_DRY_RUN=1 every command is printed instead of run.
|
|
4
|
+
import { spawnSync } from 'node:child_process';
|
|
5
|
+
import { accessSync, constants, existsSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
const appPaths = (name) =>
|
|
10
|
+
['/Applications', path.join(homedir(), 'Applications')]
|
|
11
|
+
.map((dir) => path.join(dir, `${name}.app`))
|
|
12
|
+
.filter((p) => existsSync(p));
|
|
13
|
+
const app = (...names) => names.flatMap(appPaths)[0];
|
|
14
|
+
|
|
15
|
+
function which(bin) {
|
|
16
|
+
for (const dir of (process.env.PATH ?? '').split(path.delimiter).filter(Boolean)) {
|
|
17
|
+
try {
|
|
18
|
+
accessSync(path.join(dir, bin), constants.X_OK);
|
|
19
|
+
return path.join(dir, bin);
|
|
20
|
+
} catch {}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const bundleId = (appPath) =>
|
|
25
|
+
spawnSync('plutil', ['-extract', 'CFBundleIdentifier', 'raw', '-o', '-', `${appPath}/Contents/Info.plist`], {
|
|
26
|
+
encoding: 'utf8',
|
|
27
|
+
}).stdout?.trim();
|
|
28
|
+
|
|
29
|
+
const codexApp = () =>
|
|
30
|
+
['ChatGPT', 'Codex'].flatMap(appPaths).find((p) => bundleId(p) === 'com.openai.codex');
|
|
31
|
+
|
|
32
|
+
const quote = (arg) => (/^([\w@%+=:,./-]+|<[\w-]+>)$/.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`);
|
|
33
|
+
export const show = (cmd, args) => [cmd, ...args].map(quote).join(' ');
|
|
34
|
+
|
|
35
|
+
export function run(cmd, args) {
|
|
36
|
+
if (process.env.CLAUDECRAFT_DRY_RUN === '1') {
|
|
37
|
+
console.log(`[dry run] ${show(cmd, args)}`);
|
|
38
|
+
return { ok: true, dry: true, stdout: '' };
|
|
39
|
+
}
|
|
40
|
+
if (process.env.NODE_TEST_CONTEXT) throw new Error('tests must set CLAUDECRAFT_DRY_RUN=1, so nothing ran');
|
|
41
|
+
const r = spawnSync(cmd, args, { encoding: 'utf8', timeout: 30_000, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
42
|
+
return { ok: r.status === 0, dry: false, stdout: r.stdout ?? '' };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const attempt = (cmd, args, done) => (run(cmd, args).ok ? done : `could not open it. Run ${show(cmd, args)}`);
|
|
46
|
+
|
|
47
|
+
function json(text) {
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(text);
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const APPS = [
|
|
56
|
+
{
|
|
57
|
+
id: 'super',
|
|
58
|
+
label: 'super.engineering',
|
|
59
|
+
files: ['_super.engineering'],
|
|
60
|
+
detect: () => Boolean(which('sc') && app('super.engineering')),
|
|
61
|
+
open(dir) {
|
|
62
|
+
const list = run('sc', ['workspace', 'list', '--json']);
|
|
63
|
+
const workspace = list.dry ? '<workspace-id>' : json(list.stdout)?.response?.active_workspace_id;
|
|
64
|
+
const add = ['workspace', 'add', workspace ?? '<workspace-id>', '--project', dir];
|
|
65
|
+
if (!workspace) return `open the app, then run ${show('sc', add)}`;
|
|
66
|
+
if (!existsSync(path.join(dir, '.git'))) return `needs git. Run git init, then ${show('sc', add)}`;
|
|
67
|
+
const r = run('sc', [...add, '--json']);
|
|
68
|
+
return r.ok && !r.stdout.includes('confirmation_required')
|
|
69
|
+
? 'added to your active workspace.'
|
|
70
|
+
: `run ${show('sc', add)}`;
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: 't3',
|
|
75
|
+
label: 'T3 Code',
|
|
76
|
+
files: ['t3.json'],
|
|
77
|
+
detect: () => Boolean(app('T3 Code (Alpha)', 'T3 Code') || which('t3')),
|
|
78
|
+
open(dir, { siteName }) {
|
|
79
|
+
const manual = `add ${dir} in the app, then import its t3.json actions.`;
|
|
80
|
+
if (!which('t3')) return manual;
|
|
81
|
+
return run('t3', ['project', 'add', dir, '--title', siteName]).ok
|
|
82
|
+
? 'added. Import its t3.json actions in Settings > Projects.'
|
|
83
|
+
: manual;
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
id: 'claude',
|
|
88
|
+
label: 'Claude desktop',
|
|
89
|
+
files: ['_claude/launch.json'],
|
|
90
|
+
detect: () => Boolean(app('Claude')),
|
|
91
|
+
open: (dir) =>
|
|
92
|
+
attempt(
|
|
93
|
+
'open',
|
|
94
|
+
[`claude://code/new?folder=${encodeURIComponent(dir)}`],
|
|
95
|
+
'opened a Code session. Confirm the folder in its dialog.',
|
|
96
|
+
),
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 'codex',
|
|
100
|
+
label: 'Codex app',
|
|
101
|
+
files: ['_codex'],
|
|
102
|
+
detect: () => Boolean(codexApp()),
|
|
103
|
+
open(dir) {
|
|
104
|
+
const url = `codex://threads/new?path=${encodeURIComponent(dir)}`;
|
|
105
|
+
return attempt('open', ['-a', codexApp() ?? '/Applications/ChatGPT.app', url], 'opened a new thread.');
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
id: 'cursor',
|
|
110
|
+
label: 'Cursor',
|
|
111
|
+
files: [],
|
|
112
|
+
detect: () => Boolean(app('Cursor')),
|
|
113
|
+
open: (dir) =>
|
|
114
|
+
attempt(
|
|
115
|
+
which('cursor') ?? `${app('Cursor') ?? '/Applications/Cursor.app'}/Contents/Resources/app/bin/cursor`,
|
|
116
|
+
[dir],
|
|
117
|
+
'opened.',
|
|
118
|
+
),
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: 'zed',
|
|
122
|
+
label: 'Zed',
|
|
123
|
+
files: ['_zed'],
|
|
124
|
+
detect: () => Boolean(app('Zed')),
|
|
125
|
+
open: (dir) => attempt(`${app('Zed') ?? '/Applications/Zed.app'}/Contents/MacOS/cli`, [dir], 'opened.'),
|
|
126
|
+
},
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
export const detectApps = () => APPS.filter((a) => a.detect()).map(({ id, label }) => ({ id, label }));
|
|
130
|
+
|
|
131
|
+
export function openApps(ids, dir, ctx) {
|
|
132
|
+
return APPS.filter((a) => ids.includes(a.id)).map((a) => {
|
|
133
|
+
let line;
|
|
134
|
+
try {
|
|
135
|
+
line = a.open(dir, ctx);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
line = `could not open it (${err instanceof Error ? err.message : err}).`;
|
|
138
|
+
}
|
|
139
|
+
return { id: a.id, line: `${a.label}: ${line}` };
|
|
140
|
+
});
|
|
141
|
+
}
|
package/lib/github.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Optional GitHub repo creation via gh CLI.
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { run, show } from './apps.mjs';
|
|
4
|
+
|
|
5
|
+
export const githubReady = () => spawnSync('gh', ['auth', 'token'], { stdio: 'ignore' }).status === 0;
|
|
6
|
+
|
|
7
|
+
export function createRepo(dir, name, visibility) {
|
|
8
|
+
const git = (...args) => spawnSync('git', ['-C', dir, ...args], { encoding: 'utf8' });
|
|
9
|
+
const commit = git('add', '-A').status === 0 && git('commit', '-q', '-m', `chore: scaffold ${name} with claudecraft`);
|
|
10
|
+
if (!commit || commit.status !== 0) {
|
|
11
|
+
const why = (commit && commit.stderr.trim().split('\n')[0]) || 'git add failed';
|
|
12
|
+
return `GitHub: skipped, the first commit failed (${why}).`;
|
|
13
|
+
}
|
|
14
|
+
const args = ['repo', 'create', name, `--${visibility}`, '--source', dir, '--remote', 'origin', '--push'];
|
|
15
|
+
try {
|
|
16
|
+
const r = run('gh', args);
|
|
17
|
+
if (!r.ok) return `GitHub: could not create the repo. Run ${show('gh', args)}`;
|
|
18
|
+
const url = r.stdout.match(/https:\/\/github\.com\/\S+/)?.[0];
|
|
19
|
+
if (r.dry) return 'GitHub: dry run, no repo created.';
|
|
20
|
+
return `GitHub: ${visibility} repo ${url ?? 'created'}, main pushed.`;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return `GitHub: could not create the repo (${error.message}).`;
|
|
23
|
+
}
|
|
24
|
+
}
|
package/lib/init.mjs
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// `create-claudecraft --init`: add the design-quality skill, design.md (G1-G20), intent.md,
|
|
2
|
+
// design-lint PostToolUse hook, and agent entrypoints into an existing project without
|
|
3
|
+
// clobbering user code.
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { SHARED_TEMPLATE_DIR, prettifyName } from '../dist/answers.js';
|
|
8
|
+
import { RENAME, replaceTokens } from './scaffold.mjs';
|
|
9
|
+
|
|
10
|
+
const HOOK_ENTRY = {
|
|
11
|
+
matcher: 'Write|Edit|MultiEdit',
|
|
12
|
+
hooks: [
|
|
13
|
+
{
|
|
14
|
+
type: 'command',
|
|
15
|
+
command: 'node .claude/hooks/design-lint.mjs',
|
|
16
|
+
timeout: 5000,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function detectTokensFile(dir) {
|
|
22
|
+
const candidates = [
|
|
23
|
+
'src/styles/global.css',
|
|
24
|
+
'src/index.css',
|
|
25
|
+
'src/globals.css',
|
|
26
|
+
'app/globals.css',
|
|
27
|
+
'src/app/globals.css',
|
|
28
|
+
];
|
|
29
|
+
return candidates.find((c) => existsSync(path.join(dir, c))) ?? 'src/styles/global.css';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function detectSiteName(dir) {
|
|
33
|
+
const pkgFile = path.join(dir, 'package.json');
|
|
34
|
+
if (existsSync(pkgFile)) {
|
|
35
|
+
try {
|
|
36
|
+
const pkg = JSON.parse(readFileSync(pkgFile, 'utf8'));
|
|
37
|
+
if (typeof pkg.name === 'string' && pkg.name.trim()) {
|
|
38
|
+
return prettifyName(pkg.name.replace(/^@[^/]+\//, ''));
|
|
39
|
+
}
|
|
40
|
+
} catch {}
|
|
41
|
+
}
|
|
42
|
+
return prettifyName(path.basename(dir));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function copyShared(from, to, vars, { force, added, updated, skipped }) {
|
|
46
|
+
const entries = await readdir(from, { withFileTypes: true });
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
const src = path.join(from, entry.name);
|
|
49
|
+
const dest = path.join(to, RENAME.get(entry.name) ?? entry.name);
|
|
50
|
+
const rel = path.relative(vars._ROOT, dest);
|
|
51
|
+
if (entry.isDirectory()) {
|
|
52
|
+
await mkdir(dest, { recursive: true });
|
|
53
|
+
await copyShared(src, dest, vars, { force, added, updated, skipped });
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const existed = existsSync(dest);
|
|
57
|
+
// Skill files and hook update cleanly on --force; user-authored design.md / intent.md stay safe unless --force.
|
|
58
|
+
const isSkillOrHook = rel.startsWith(`.claude${path.sep}`);
|
|
59
|
+
if (existed && !force) {
|
|
60
|
+
skipped.push(rel);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const raw = await readFile(src, 'utf8');
|
|
64
|
+
await mkdir(path.dirname(dest), { recursive: true });
|
|
65
|
+
await writeFile(dest, replaceTokens(raw, vars));
|
|
66
|
+
if (existed) updated.push(rel);
|
|
67
|
+
else added.push(rel);
|
|
68
|
+
void isSkillOrHook;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function ensureSettingsHook(dir, { added, updated, skipped }) {
|
|
73
|
+
const file = path.join(dir, '.claude', 'settings.json');
|
|
74
|
+
const rel = path.relative(dir, file);
|
|
75
|
+
let settings = {};
|
|
76
|
+
const existed = existsSync(file);
|
|
77
|
+
if (existed) {
|
|
78
|
+
try {
|
|
79
|
+
settings = JSON.parse(await readFile(file, 'utf8'));
|
|
80
|
+
} catch {
|
|
81
|
+
skipped.push(`${rel} (invalid JSON, add hook manually)`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const post = settings.hooks?.PostToolUse ?? [];
|
|
86
|
+
const hasHook = post.some((entry) =>
|
|
87
|
+
entry?.hooks?.some((h) => typeof h?.command === 'string' && h.command.includes('design-lint.mjs')),
|
|
88
|
+
);
|
|
89
|
+
if (hasHook) {
|
|
90
|
+
skipped.push(rel);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const next = {
|
|
94
|
+
$schema: 'https://json.schemastore.org/claude-code-settings.json',
|
|
95
|
+
...settings,
|
|
96
|
+
hooks: {
|
|
97
|
+
...(settings.hooks ?? {}),
|
|
98
|
+
PostToolUse: [...post, HOOK_ENTRY],
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
102
|
+
await writeFile(file, `${JSON.stringify(next, null, 2)}\n`);
|
|
103
|
+
(existed ? updated : added).push(rel);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function ensureAgentEntrypoints(dir, siteName, { added, skipped }) {
|
|
107
|
+
const agentsPath = path.join(dir, 'AGENTS.md');
|
|
108
|
+
const claudePath = path.join(dir, 'CLAUDE.md');
|
|
109
|
+
|
|
110
|
+
if (!existsSync(agentsPath)) {
|
|
111
|
+
await writeFile(
|
|
112
|
+
agentsPath,
|
|
113
|
+
`# AGENTS.md: ${siteName}
|
|
114
|
+
|
|
115
|
+
Read \`design.md\` and \`intent.md\` before laying out or reviewing UI.
|
|
116
|
+
|
|
117
|
+
- Run \`/design-quality plan\` before building a new page or surface.
|
|
118
|
+
- Run \`/design-quality\` (or read \`.claude/skills/design-quality/SKILL.md\`) as a separate critique pass grounded in rendered pixels before claiming UI done.
|
|
119
|
+
`,
|
|
120
|
+
);
|
|
121
|
+
added.push('AGENTS.md');
|
|
122
|
+
} else {
|
|
123
|
+
skipped.push('AGENTS.md');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!existsSync(claudePath)) {
|
|
127
|
+
await writeFile(claudePath, '@AGENTS.md\n');
|
|
128
|
+
added.push('CLAUDE.md');
|
|
129
|
+
} else {
|
|
130
|
+
skipped.push('CLAUDE.md');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function initExisting(dir = process.cwd(), { force = false } = {}) {
|
|
135
|
+
const siteName = detectSiteName(dir);
|
|
136
|
+
const tokensFile = detectTokensFile(dir);
|
|
137
|
+
const vars = {
|
|
138
|
+
_ROOT: dir,
|
|
139
|
+
SITE_NAME: siteName,
|
|
140
|
+
ONE_SENTENCE: `${siteName}: fill the one-sentence intent with your agent.`,
|
|
141
|
+
TOKENS_FILE: tokensFile,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const added = [];
|
|
145
|
+
const updated = [];
|
|
146
|
+
const skipped = [];
|
|
147
|
+
|
|
148
|
+
await copyShared(SHARED_TEMPLATE_DIR, dir, vars, { force, added, updated, skipped });
|
|
149
|
+
await ensureSettingsHook(dir, { added, updated, skipped });
|
|
150
|
+
await ensureAgentEntrypoints(dir, siteName, { added, skipped });
|
|
151
|
+
|
|
152
|
+
return { dir, siteName, tokensFile, added, updated, skipped };
|
|
153
|
+
}
|
package/lib/ports.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// One stable dev port per project name, so every project keeps the same local URL.
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import net from 'node:net';
|
|
5
|
+
import { homedir } from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
const FIRST = 4300;
|
|
9
|
+
const COUNT = 700; // 4300-4999
|
|
10
|
+
const RESERVED = new Set([4321, 4173, 4747]);
|
|
11
|
+
|
|
12
|
+
export async function claimDevPort(name) {
|
|
13
|
+
if (!process.env.CLAUDECRAFT_PORTS_FILE && process.env.NODE_TEST_CONTEXT) {
|
|
14
|
+
throw new Error('Tests must set CLAUDECRAFT_PORTS_FILE. ~/.config/claudecraft/ports.json was not touched.');
|
|
15
|
+
}
|
|
16
|
+
const file = process.env.CLAUDECRAFT_PORTS_FILE || path.join(homedir(), '.config', 'claudecraft', 'ports.json');
|
|
17
|
+
const claimed = await readClaims(file);
|
|
18
|
+
if (Number.isInteger(claimed[name])) return claimed[name];
|
|
19
|
+
|
|
20
|
+
const taken = new Set(Object.values(claimed));
|
|
21
|
+
const start = createHash('sha256').update(name).digest().readUInt32BE(0) % COUNT;
|
|
22
|
+
for (let i = 0; i < COUNT; i++) {
|
|
23
|
+
const port = FIRST + ((start + i) % COUNT);
|
|
24
|
+
if (RESERVED.has(port) || taken.has(port) || (await inUse(port))) continue;
|
|
25
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
26
|
+
await writeFile(file, `${JSON.stringify({ ...claimed, [name]: port }, null, 2)}\n`);
|
|
27
|
+
return port;
|
|
28
|
+
}
|
|
29
|
+
throw new Error(`No free dev port between ${FIRST} and ${FIRST + COUNT - 1}.`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function readClaims(file) {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(await readFile(file, 'utf8'));
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error.code === 'ENOENT') return {};
|
|
37
|
+
throw new Error(`Cannot read ${file}: ${error.message}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function inUse(port) {
|
|
42
|
+
const hits = await Promise.all(['::1', '127.0.0.1'].map((host) => listening(host, port)));
|
|
43
|
+
return hits.includes(true);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const listening = (host, port) =>
|
|
47
|
+
new Promise((resolve) => {
|
|
48
|
+
const socket = net.connect({ host, port });
|
|
49
|
+
const done = (result) => {
|
|
50
|
+
socket.destroy();
|
|
51
|
+
resolve(result);
|
|
52
|
+
};
|
|
53
|
+
socket.setTimeout(1000, () => done(true));
|
|
54
|
+
socket.once('connect', () => done(true));
|
|
55
|
+
socket.once('error', () => done(false));
|
|
56
|
+
});
|
package/lib/scaffold.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { cp, mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { SHARED_TEMPLATE_DIR, templateDirFor, templateVars } from '../dist/answers.js';
|
|
7
|
+
import { APPS, openApps } from './apps.mjs';
|
|
8
|
+
import { createRepo } from './github.mjs';
|
|
9
|
+
import { claimDevPort } from './ports.mjs';
|
|
10
|
+
|
|
11
|
+
export const KIT = fileURLToPath(new URL('..', import.meta.url));
|
|
12
|
+
export const STAMP = 'claudecraft.json';
|
|
13
|
+
|
|
14
|
+
// Stored without the dot so npm pack keeps them and agents working in this kit do not load them.
|
|
15
|
+
export const RENAME = new Map([
|
|
16
|
+
['_gitignore', '.gitignore'],
|
|
17
|
+
['_npmrc', '.npmrc'],
|
|
18
|
+
['_claude', '.claude'],
|
|
19
|
+
['_CLAUDE.md', 'CLAUDE.md'],
|
|
20
|
+
['_super.engineering', '.super.engineering'],
|
|
21
|
+
['_codex', '.codex'],
|
|
22
|
+
['_zed', '.zed'],
|
|
23
|
+
['_githooks', '.githooks'],
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
// '{{X}}' or "{{X}}" becomes a string or array literal; bare {{X}} is pasted raw.
|
|
27
|
+
const TOKEN = /(['"]?)\{\{\s*([A-Z0-9_]+)\s*\}\}\1/g;
|
|
28
|
+
|
|
29
|
+
const TEXT_EXT = new Set([
|
|
30
|
+
'.astro',
|
|
31
|
+
'.css',
|
|
32
|
+
'.env',
|
|
33
|
+
'.example',
|
|
34
|
+
'.html',
|
|
35
|
+
'.js',
|
|
36
|
+
'.json',
|
|
37
|
+
'.md',
|
|
38
|
+
'.mjs',
|
|
39
|
+
'.sh',
|
|
40
|
+
'.svg',
|
|
41
|
+
'.toml',
|
|
42
|
+
'.ts',
|
|
43
|
+
'.tsx',
|
|
44
|
+
'.txt',
|
|
45
|
+
'.webmanifest',
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const SKIP_NAMES = new Set(['node_modules', 'dist', '.astro', '.git', '.DS_Store', '.env', '.env.local', '.dev.vars', '.wrangler']);
|
|
49
|
+
|
|
50
|
+
export function refuseInsideGitRepo(cwd = process.cwd()) {
|
|
51
|
+
if (process.env.CLAUDECRAFT_ALLOW_NESTED === '1') return;
|
|
52
|
+
const git = spawnSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd, encoding: 'utf8' });
|
|
53
|
+
if (git.status === 0 && git.stdout.trim() === 'true') {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`${cwd} is inside a git repo. Run create-claudecraft from the folder that should hold the new project (or pass --init to add design-quality here).`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function scaffold(answers) {
|
|
61
|
+
const { template, projectName, git, install } = answers;
|
|
62
|
+
const picked = answers.apps ?? [];
|
|
63
|
+
refuseInsideGitRepo();
|
|
64
|
+
const targetDir = path.resolve(process.cwd(), projectName);
|
|
65
|
+
const templateDir = templateDirFor(template);
|
|
66
|
+
|
|
67
|
+
if (existsSync(targetDir)) {
|
|
68
|
+
throw new Error(`Directory already exists: ${targetDir}`);
|
|
69
|
+
}
|
|
70
|
+
if (!existsSync(templateDir)) {
|
|
71
|
+
throw new Error(`Template "${template}" missing at ${templateDir}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const devPort = await claimDevPort(projectName);
|
|
75
|
+
const stamp = {
|
|
76
|
+
...kitVersion(),
|
|
77
|
+
answers: stamped(answers),
|
|
78
|
+
devPort,
|
|
79
|
+
published: new Date().toISOString().slice(0, 10),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
await render({ shared: SHARED_TEMPLATE_DIR, template: templateDir }, targetDir, stamp);
|
|
83
|
+
await writeStamp(targetDir, stamp);
|
|
84
|
+
const localUrl = localDomain(targetDir) ?? `http://${projectName}.localhost:${devPort}/`;
|
|
85
|
+
|
|
86
|
+
let github = null;
|
|
87
|
+
if (git) {
|
|
88
|
+
const g = spawnSync('git', ['init', '-b', 'main'], { cwd: targetDir, encoding: 'utf8' });
|
|
89
|
+
if (g.status !== 0) {
|
|
90
|
+
console.warn('git init skipped:', g.stderr?.trim() || g.error?.message);
|
|
91
|
+
} else if (answers.repo) {
|
|
92
|
+
github = createRepo(targetDir, projectName, answers.repo);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (install) {
|
|
97
|
+
console.log('npm install…');
|
|
98
|
+
const n = spawnSync('npm', ['install'], {
|
|
99
|
+
cwd: targetDir,
|
|
100
|
+
encoding: 'utf8',
|
|
101
|
+
stdio: 'inherit',
|
|
102
|
+
shell: process.platform === 'win32',
|
|
103
|
+
});
|
|
104
|
+
if (n.status !== 0) {
|
|
105
|
+
throw new Error('npm install failed. Retry inside the project with: npm install');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { targetDir, localUrl, github, apps: openApps(picked, targetDir, { siteName: answers.siteName }) };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function kitVersion() {
|
|
113
|
+
const { version } = JSON.parse(readFileSync(path.join(KIT, 'package.json'), 'utf8'));
|
|
114
|
+
const head = spawnSync('git', ['rev-parse', 'HEAD'], { cwd: KIT, encoding: 'utf8' });
|
|
115
|
+
return { kit: version, commit: head.status === 0 ? head.stdout.trim() : null };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const stamped = ({
|
|
119
|
+
template,
|
|
120
|
+
projectName,
|
|
121
|
+
siteName,
|
|
122
|
+
oneSentence,
|
|
123
|
+
siteUrl,
|
|
124
|
+
accentId,
|
|
125
|
+
defaultLocale,
|
|
126
|
+
locales,
|
|
127
|
+
apps,
|
|
128
|
+
darkMode,
|
|
129
|
+
}) => ({
|
|
130
|
+
template,
|
|
131
|
+
projectName,
|
|
132
|
+
siteName,
|
|
133
|
+
oneSentence,
|
|
134
|
+
siteUrl,
|
|
135
|
+
accentId,
|
|
136
|
+
defaultLocale,
|
|
137
|
+
locales,
|
|
138
|
+
apps: apps ?? [],
|
|
139
|
+
darkMode,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
export const writeStamp = (dir, stamp) =>
|
|
143
|
+
writeFile(path.join(dir, STAMP), `${JSON.stringify(stamp, null, 2)}\n`);
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Renders shared files first, then template-specific files on top (so template overrides win).
|
|
147
|
+
*/
|
|
148
|
+
export async function render(sources, to, { answers, devPort, published }) {
|
|
149
|
+
const dirs = typeof sources === 'string' ? [sources] : [sources.shared, sources.template].filter(Boolean);
|
|
150
|
+
const vars = templateVars({ ...answers, devPort }, published);
|
|
151
|
+
const unpicked = APPS.filter((app) => !(answers.apps ?? []).includes(app.id));
|
|
152
|
+
await mkdir(to, { recursive: true });
|
|
153
|
+
|
|
154
|
+
for (const dir of dirs) {
|
|
155
|
+
if (!existsSync(dir)) continue;
|
|
156
|
+
const skip = new Set(unpicked.flatMap((app) => app.files).map((file) => path.join(dir, file)));
|
|
157
|
+
await copyAndTransform(dir, to, vars, answers.locales ?? ['en'], skip, path.join(dir, 'src', 'i18n'));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function localDomain(dir) {
|
|
162
|
+
const script = path.join(dir, 'scripts', 'local-domain.mjs');
|
|
163
|
+
if (!existsSync(script)) return undefined;
|
|
164
|
+
const r = spawnSync(process.execPath, [script], { cwd: dir, encoding: 'utf8' });
|
|
165
|
+
const out = (r.stdout ?? '').trim().split('\n');
|
|
166
|
+
const url = out.at(-1)?.trim();
|
|
167
|
+
for (const note of out.slice(0, -1)) console.log(note);
|
|
168
|
+
if (r.status !== 0) {
|
|
169
|
+
const reason = r.stderr?.match(/^\w*Error\b.*$/m)?.[0] ?? r.stderr?.trim().split('\n').at(-1);
|
|
170
|
+
console.warn(`Local domain skipped: ${reason || r.error?.message || 'failed'}`);
|
|
171
|
+
}
|
|
172
|
+
return url && /^https?:\/\//.test(url) ? url : undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function copyAndTransform(from, to, vars, locales, skip, i18n) {
|
|
176
|
+
const entries = await readdir(from, { withFileTypes: true });
|
|
177
|
+
for (const entry of entries) {
|
|
178
|
+
if (SKIP_NAMES.has(entry.name)) continue;
|
|
179
|
+
const unselectedLocale =
|
|
180
|
+
from === i18n && entry.name.endsWith('.json') && !locales.includes(path.basename(entry.name, '.json'));
|
|
181
|
+
if (unselectedLocale) continue;
|
|
182
|
+
const src = path.join(from, entry.name);
|
|
183
|
+
if (skip.has(src)) continue;
|
|
184
|
+
const dest = path.join(to, RENAME.get(entry.name) ?? entry.name);
|
|
185
|
+
if (entry.isDirectory()) {
|
|
186
|
+
await mkdir(dest, { recursive: true });
|
|
187
|
+
await copyAndTransform(src, dest, vars, locales, skip, i18n);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
191
|
+
const base = entry.name.toLowerCase();
|
|
192
|
+
const asText = TEXT_EXT.has(ext) || base === 'dockerfile' || base.endsWith('.example') || !ext;
|
|
193
|
+
if (asText) {
|
|
194
|
+
const body = await readFile(src, 'utf8');
|
|
195
|
+
await writeFile(dest, replaceTokens(body, vars));
|
|
196
|
+
} else {
|
|
197
|
+
await cp(src, dest);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function replaceTokens(body, vars) {
|
|
203
|
+
return body.replace(TOKEN, (match, quote, key) => splice(vars, key, quote) ?? match);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function splice(vars, key, quote) {
|
|
207
|
+
if (!Object.hasOwn(vars, key)) return undefined;
|
|
208
|
+
const value = vars[key];
|
|
209
|
+
if (!quote) return Array.isArray(value) ? value.join(', ') : value;
|
|
210
|
+
return Array.isArray(value) ? `[${value.map((v) => literal(v, quote)).join(', ')}]` : literal(value, quote);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function literal(text, quote) {
|
|
214
|
+
const json = JSON.stringify(text);
|
|
215
|
+
const count = (q) => text.split(q).length - 1;
|
|
216
|
+
if (quote === '"' || count("'") > count('"')) return json;
|
|
217
|
+
return `'${json.slice(1, -1).replace(/\\"|'/g, (m) => (m === "'" ? "\\'" : '"'))}'`;
|
|
218
|
+
}
|