create-creact 0.0.0-stage → 0.1.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.
Files changed (49) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +17 -2
  3. package/dist/create.js +257 -0
  4. package/dist/layer.js +633 -0
  5. package/dist/main.js +61 -0
  6. package/dist/options.js +262 -0
  7. package/dist/shell.js +117 -0
  8. package/dist/stacks.js +42 -0
  9. package/dist/ui.js +506 -0
  10. package/dist/version.js +7 -0
  11. package/layer/base/all/.oxfmtrc.json.layer +65 -0
  12. package/layer/base/all/AGENTS.md.layer +22 -0
  13. package/layer/base/all/CLAUDE.md.layer +1 -0
  14. package/layer/base/all/apps/web/src/styles/tokens.css.layer +15 -0
  15. package/layer/base/all/commitlint.config.ts.layer +12 -0
  16. package/layer/base/all/oxlint.config.ts.layer +556 -0
  17. package/layer/base/next/AGENTS.md.layer +4 -0
  18. package/layer/base/next/apps/web/CLAUDE.md.layer +1 -0
  19. package/layer/base/react-router/AGENTS.md.layer +3 -0
  20. package/layer/standalone/all/.husky/commit-msg.layer +2 -0
  21. package/layer/standalone/all/.husky/pre-commit.layer +2 -0
  22. package/layer/standalone/all/.lintstagedrc.json.layer +4 -0
  23. package/layer/standalone/all/AGENTS.md.layer +9 -0
  24. package/layer/standalone/all/vitest.config.ts.layer +14 -0
  25. package/layer/stylex/all/AGENTS.md.layer +4 -0
  26. package/layer/stylex/all/apps/web/src/styles/tokens.stylex.ts.layer +15 -0
  27. package/layer/stylex/all/apps/web/src/styles/tokens.test.ts.layer +32 -0
  28. package/layer/stylex/all/oxlint.styling.ts.layer +20 -0
  29. package/layer/stylex/all/vitest.styling.ts.layer +18 -0
  30. package/layer/stylex/next/apps/web/babel.config.js.layer +25 -0
  31. package/layer/stylex/next/apps/web/postcss.config.js.layer +16 -0
  32. package/layer/stylex/next/apps/web/src/app/layout.tsx.layer +20 -0
  33. package/layer/stylex/next/apps/web/src/app/page.tsx.layer +48 -0
  34. package/layer/stylex/next/apps/web/src/index.css.layer +45 -0
  35. package/layer/stylex/react-router/apps/web/src/index.css.layer +27 -0
  36. package/layer/stylex/react-router/apps/web/src/root.tsx.layer +94 -0
  37. package/layer/stylex/react-router/apps/web/src/routes/_index.tsx.layer +56 -0
  38. package/layer/stylex/react-router/apps/web/vite.config.ts.layer +22 -0
  39. package/layer/tailwind/all/AGENTS.md.layer +4 -0
  40. package/layer/tailwind/all/apps/web/src/index.css.layer +3 -0
  41. package/layer/tailwind/all/apps/web/src/styles/tokens.tailwind.css.layer +7 -0
  42. package/layer/tailwind/all/apps/web/src/styles/tokens.test.ts.layer +35 -0
  43. package/layer/tailwind/all/oxlint.styling.ts.layer +69 -0
  44. package/layer/tailwind/all/vitest.styling.ts.layer +5 -0
  45. package/layer/vite-plus/all/.vite-hooks/commit-msg.layer +2 -0
  46. package/layer/vite-plus/all/.vite-hooks/pre-commit.layer +2 -0
  47. package/layer/vite-plus/all/AGENTS.md.layer +11 -0
  48. package/layer/vite-plus/all/vite.config.ts.layer +25 -0
  49. package/package.json +37 -4
@@ -0,0 +1,262 @@
1
+ import path from "node:path";
2
+ import { parseArgs } from "node:util";
3
+ import { MONOREPOS, STACKS } from "./stacks.js";
4
+ export const STYLINGS = {
5
+ tailwind: { status: "built", summary: "Tailwind CSS" },
6
+ stylex: { status: "built", summary: "StyleX" },
7
+ };
8
+ export const TOOLCHAINS = {
9
+ standalone: {
10
+ status: "built",
11
+ summary: "Oxlint, Oxfmt, Vitest and husky, each set up on its own",
12
+ imports: {
13
+ oxlint: "oxlint",
14
+ vitest: "vitest",
15
+ vitestConfig: "vitest/config",
16
+ },
17
+ },
18
+ "vite-plus": {
19
+ status: "built",
20
+ summary: "Vite+, whose vp runs lint, format, tests and the git hooks",
21
+ imports: {
22
+ oxlint: "vite-plus/lint",
23
+ vitest: "vite-plus/test",
24
+ vitestConfig: "vite-plus",
25
+ },
26
+ },
27
+ };
28
+ export const EXTRAS = {
29
+ shadcn: {
30
+ status: "planned",
31
+ summary: "shadcn/ui components from creact's registry",
32
+ onlyWith: ["tailwind"],
33
+ },
34
+ storybook: {
35
+ status: "planned",
36
+ summary: "Storybook, with stories run as tests",
37
+ },
38
+ msw: {
39
+ status: "planned",
40
+ summary: "MSW handlers shared by tests and Storybook",
41
+ },
42
+ playwright: { status: "planned", summary: "Playwright end-to-end tests" },
43
+ i18n: { status: "planned", summary: "i18next with typed resources" },
44
+ posthog: {
45
+ status: "planned",
46
+ summary: "PostHog, set up by its official skill",
47
+ },
48
+ sentry: {
49
+ status: "planned",
50
+ summary: "Sentry, set up by its official skill",
51
+ },
52
+ };
53
+ export const DEFAULTS = {
54
+ stack: "react-router",
55
+ styling: "tailwind",
56
+ monorepo: "pnpm",
57
+ toolchain: "standalone",
58
+ };
59
+ export class UsageError extends Error {
60
+ name = "UsageError";
61
+ }
62
+ const EXTRA_NAMES = Object.keys(EXTRAS);
63
+ const OPTIONS = {
64
+ stack: { type: "string" },
65
+ styling: { type: "string" },
66
+ monorepo: { type: "string" },
67
+ toolchain: { type: "string" },
68
+ install: { type: "boolean", default: true },
69
+ git: { type: "boolean", default: true },
70
+ yes: { type: "boolean", short: "y", default: false },
71
+ json: { type: "boolean", default: false },
72
+ help: { type: "boolean", short: "h", default: false },
73
+ ...Object.fromEntries(EXTRA_NAMES.map((name) => [name, { type: "boolean", default: false }])),
74
+ };
75
+ export function isBuilt(registry, key) {
76
+ return Object.hasOwn(registry, key) && registry[key]?.status === "built";
77
+ }
78
+ function lookup(registry, key, flag) {
79
+ if (isBuilt(registry, key))
80
+ return { kind: "built", id: key };
81
+ const feature = Object.hasOwn(registry, key) ? registry[key] : undefined;
82
+ if (feature === undefined)
83
+ return { kind: "unknown" };
84
+ return {
85
+ kind: "planned",
86
+ flag: feature.note === undefined ? flag : `${flag} (${feature.note})`,
87
+ };
88
+ }
89
+ export function parseRequest(argv, cwd) {
90
+ let parsed;
91
+ try {
92
+ parsed = parseArgs({
93
+ args: [...argv],
94
+ options: OPTIONS,
95
+ strict: true,
96
+ allowPositionals: true,
97
+ allowNegative: true,
98
+ });
99
+ }
100
+ catch (error) {
101
+ const reason = error instanceof Error ? error.message : String(error);
102
+ return usage(`${reason}. Run with --help to see the flags.`);
103
+ }
104
+ const { values, positionals } = parsed;
105
+ if (values.help)
106
+ return { kind: "help", text: helpText() };
107
+ const [dir, ...rest] = positionals;
108
+ if (dir === undefined || rest.length > 0) {
109
+ return usage("give one project directory, as in: create-creact ../my-app");
110
+ }
111
+ if (values.stack !== undefined && !Object.hasOwn(STACKS, values.stack)) {
112
+ return usage(`--stack must be one of ${Object.keys(STACKS).join(", ")}`);
113
+ }
114
+ const stack = values.stack;
115
+ const styling = values.styling === undefined
116
+ ? undefined
117
+ : lookup(STYLINGS, values.styling, `--styling ${values.styling}`);
118
+ if (styling?.kind === "unknown") {
119
+ return usage(`--styling must be one of ${Object.keys(STYLINGS).join(", ")}`);
120
+ }
121
+ const monorepo = values.monorepo === undefined
122
+ ? undefined
123
+ : lookup(MONOREPOS, values.monorepo, `--monorepo ${values.monorepo}`);
124
+ if (monorepo?.kind === "unknown") {
125
+ return usage(`--monorepo must be one of ${Object.keys(MONOREPOS).join(", ")}`);
126
+ }
127
+ const toolchain = values.toolchain === undefined
128
+ ? undefined
129
+ : lookup(TOOLCHAINS, values.toolchain, `--toolchain ${values.toolchain}`);
130
+ if (toolchain?.kind === "unknown") {
131
+ return usage(`--toolchain must be one of ${Object.keys(TOOLCHAINS).join(", ")}`);
132
+ }
133
+ const extras = EXTRA_NAMES.filter((extra) => values[extra]).map((name) => lookup(EXTRAS, name, `--${name}`));
134
+ const planned = [styling, monorepo, toolchain, ...extras].flatMap((found) => found?.kind === "planned" ? [found.flag] : []);
135
+ if (planned.length > 0) {
136
+ return usage(`not built yet: ${planned.join(", ")}. Rerun without these flags.`);
137
+ }
138
+ return {
139
+ kind: "run",
140
+ request: {
141
+ dir: path.resolve(cwd, dir),
142
+ stack,
143
+ styling: styling?.kind === "built" ? styling.id : undefined,
144
+ monorepo: monorepo?.kind === "built" ? monorepo.id : undefined,
145
+ toolchain: toolchain?.kind === "built" ? toolchain.id : undefined,
146
+ extras: extras.flatMap((found) => found.kind === "built" ? [found.id] : []),
147
+ yes: values.yes,
148
+ install: values.install,
149
+ git: values.git,
150
+ },
151
+ };
152
+ }
153
+ export function resolveChoices(request, recorded, registry = EXTRAS) {
154
+ const choices = choose(request, recorded);
155
+ const extras = choices.extras;
156
+ for (const extra of extras) {
157
+ const onlyWith = registry[extra]?.onlyWith;
158
+ if (onlyWith !== undefined && !onlyWith.includes(choices.styling)) {
159
+ throw new UsageError(`--${extra} works with --styling ${onlyWith.join("|")}, and ${request.dir} is ${choices.styling}`);
160
+ }
161
+ }
162
+ return choices;
163
+ }
164
+ function choose(request, recorded) {
165
+ if (recorded !== undefined) {
166
+ for (const key of ["stack", "styling", "monorepo", "toolchain"]) {
167
+ const asked = request[key];
168
+ if (asked !== undefined && asked !== recorded[key]) {
169
+ throw new UsageError(`${request.dir} is recorded as ${key} ${recorded[key]}, so --${key} ${asked} can't apply`);
170
+ }
171
+ }
172
+ return {
173
+ stack: recorded.stack,
174
+ styling: recorded.styling,
175
+ monorepo: recorded.monorepo,
176
+ toolchain: recorded.toolchain,
177
+ extras: [...new Set([...recorded.extras, ...request.extras])],
178
+ };
179
+ }
180
+ const stack = request.stack ?? (request.yes ? DEFAULTS.stack : undefined);
181
+ if (stack === undefined) {
182
+ throw new UsageError(`choose a stack with --stack ${Object.keys(STACKS).join("|")}, or pass --yes for ${DEFAULTS.stack}`);
183
+ }
184
+ return {
185
+ stack,
186
+ styling: request.styling ?? DEFAULTS.styling,
187
+ monorepo: request.monorepo ?? DEFAULTS.monorepo,
188
+ toolchain: request.toolchain ?? DEFAULTS.toolchain,
189
+ extras: request.extras,
190
+ };
191
+ }
192
+ export function isProjectName(name) {
193
+ return /^[a-z0-9][a-z0-9._-]*$/.test(name);
194
+ }
195
+ export function projectName(dir) {
196
+ const name = path.basename(dir);
197
+ if (!isProjectName(name)) {
198
+ throw new UsageError(`${name} would become the package scope @${name}, so it needs lowercase letters, digits, ".", "_" or "-", starting with a letter or digit. Pick another directory name.`);
199
+ }
200
+ return name;
201
+ }
202
+ function usage(message) {
203
+ return { kind: "usage", message };
204
+ }
205
+ function choicesLine(registry) {
206
+ const ids = Object.entries(registry);
207
+ const built = ids.filter(([, feature]) => feature.status === "built");
208
+ const planned = ids.filter(([, feature]) => feature.status === "planned");
209
+ const names = built.map(([id]) => id).join(", ");
210
+ return planned.length === 0
211
+ ? names
212
+ : `${names}; planned: ${planned.map(([id]) => id).join(", ")}`;
213
+ }
214
+ function helpText() {
215
+ const registry = EXTRAS;
216
+ const extras = Object.entries(registry).map(([id, extra]) => {
217
+ const planned = extra.status === "planned" ? "planned: " : "";
218
+ const only = extra.onlyWith === undefined
219
+ ? ""
220
+ : ` (${extra.onlyWith.join(", ")} only)`;
221
+ return ` --${id.padEnd(16)}${planned}${extra.summary}${only}`;
222
+ });
223
+ return [
224
+ "Usage: create-creact <dir> [flags]",
225
+ "",
226
+ "Creates a React monorepo with Better-T-Stack, then adds creact's lint,",
227
+ "format, tsconfig, git hooks and test setup.",
228
+ "",
229
+ ` --stack <id> ${Object.keys(STACKS).join(", ")} (--yes picks ${DEFAULTS.stack})`,
230
+ ` --styling <id> ${choicesLine(STYLINGS)} (default ${DEFAULTS.styling})`,
231
+ ` --monorepo <id> ${choicesLine(MONOREPOS)} (default ${DEFAULTS.monorepo})`,
232
+ ` --toolchain <id> ${choicesLine(TOOLCHAINS)} (default ${DEFAULTS.toolchain})`,
233
+ ...extras,
234
+ " --no-install skip pnpm install, the lint and format pass and the skills restore",
235
+ " --no-git skip git init",
236
+ " -y, --yes take the default for every choice not given",
237
+ " --json print one JSON object on stdout instead of the steps",
238
+ " -h, --help show this help",
239
+ "",
240
+ "The directory must sit outside any git repository, as the project gets",
241
+ "its own. Its name becomes the package scope, as in @my-app/ui, so it",
242
+ 'needs lowercase letters, digits, ".", "_" or "-".',
243
+ "",
244
+ "The toolchain picks what runs that setup. standalone installs Oxlint,",
245
+ "Oxfmt, Vitest, husky and lint-staged, each with its own config. vite-plus",
246
+ "installs Vite+, and vp runs them from the root vite.config.ts and",
247
+ ".vite-hooks. Both get the same lint rules, format options and hooks.",
248
+ "",
249
+ "Run it again on the same directory to finish an interrupted run or take a",
250
+ "newer layer. Layer files you edited are kept and reported. The",
251
+ "package.json and tsconfig files creact patches are merged again, so the",
252
+ "scripts and compiler options creact sets go back to creact's values.",
253
+ "",
254
+ "In a terminal it shows each step as it runs. Piped, in CI or in an",
255
+ "agent's shell, it prints one plain line per finished step. Either way the",
256
+ "output of the commands it runs goes to a log file, and a failure shows",
257
+ "that log's last lines and its path.",
258
+ "",
259
+ "Exit codes: 0 done, 1 a command failed, 2 bad usage or a planned feature.",
260
+ "",
261
+ ].join("\n");
262
+ }
package/dist/shell.js ADDED
@@ -0,0 +1,117 @@
1
+ import { spawn, } from "node:child_process";
2
+ import { appendFileSync, writeFileSync } from "node:fs";
3
+ import { stripVTControlCharacters } from "node:util";
4
+ export class CommandFailed extends Error {
5
+ name = "CommandFailed";
6
+ tail;
7
+ constructor(message, tail = []) {
8
+ super(message);
9
+ this.tail = tail;
10
+ }
11
+ }
12
+ const TAIL_LINES = 12;
13
+ // Where the commands' own output goes, so the terminal shows only
14
+ // create-creact's steps. A failure shows the last lines of the command that
15
+ // failed, and the file keeps the rest.
16
+ // The file starts empty on a run's first command, so it holds one run.
17
+ export class Log {
18
+ file;
19
+ #written = false;
20
+ constructor(file) {
21
+ this.file = file;
22
+ }
23
+ get written() {
24
+ return this.#written;
25
+ }
26
+ write(text) {
27
+ /* oxlint-disable security/detect-non-literal-fs-filename -- a file in the OS temp dir, named by main */
28
+ if (this.#written)
29
+ appendFileSync(this.file, text);
30
+ else
31
+ writeFileSync(this.file, text);
32
+ /* oxlint-enable security/detect-non-literal-fs-filename -- the write ends here */
33
+ this.#written = true;
34
+ }
35
+ }
36
+ const NO_PROMPTS_OUTPUT_TO_STDERR = ["ignore", 2, 2];
37
+ // Git exports GIT_DIR, GIT_INDEX_FILE and friends to its hooks. A child that
38
+ // inherits them acts on the caller's repository instead of the project's,
39
+ // and `git init` under a linked worktree's GIT_DIR rewrites that repository's
40
+ // shared config as bare. The list is what `git rev-parse --local-env-vars`
41
+ // prints, so the caller's own git settings still reach the child.
42
+ const REPOSITORY_ENV = new Set([
43
+ "GIT_ALTERNATE_OBJECT_DIRECTORIES",
44
+ "GIT_CONFIG",
45
+ "GIT_CONFIG_PARAMETERS",
46
+ "GIT_CONFIG_COUNT",
47
+ "GIT_OBJECT_DIRECTORY",
48
+ "GIT_DIR",
49
+ "GIT_WORK_TREE",
50
+ "GIT_IMPLICIT_WORK_TREE",
51
+ "GIT_GRAFT_FILE",
52
+ "GIT_INDEX_FILE",
53
+ "GIT_NO_REPLACE_OBJECTS",
54
+ "GIT_REPLACE_REF_BASE",
55
+ "GIT_PREFIX",
56
+ "GIT_SHALLOW_FILE",
57
+ "GIT_COMMON_DIR",
58
+ ]);
59
+ export function withoutGitContext() {
60
+ return Object.fromEntries(Object.entries(process.env).filter(([name]) => !REPOSITORY_ENV.has(name)));
61
+ }
62
+ export async function run(command, args, cwd, log) {
63
+ const line = [command, ...args].join(" ");
64
+ if (log === undefined) {
65
+ const code = await exitCode(command, args, cwd, NO_PROMPTS_OUTPUT_TO_STDERR);
66
+ if (code !== 0) {
67
+ throw new CommandFailed(`${line} exited with ${code ?? "a signal"}`);
68
+ }
69
+ return;
70
+ }
71
+ log.write(`\n$ ${line}\n in ${cwd}\n\n`);
72
+ const child = spawn(command, args, {
73
+ cwd,
74
+ env: withoutGitContext(),
75
+ stdio: ["ignore", "pipe", "pipe"],
76
+ });
77
+ let output = "";
78
+ const keep = (chunk) => {
79
+ const text = chunk.toString();
80
+ log.write(text);
81
+ output = (output + text).slice(-16_384);
82
+ };
83
+ child.stdout.on("data", keep);
84
+ child.stderr.on("data", keep);
85
+ const code = await closed(child, command);
86
+ if (code !== 0) {
87
+ throw new CommandFailed(`${line} exited with ${code ?? "a signal"}`, tailOf(output));
88
+ }
89
+ }
90
+ function tailOf(output) {
91
+ return stripVTControlCharacters(output)
92
+ .split(/\r?\n|\r/)
93
+ .map((text) => text.trimEnd())
94
+ .filter((text) => text.trim() !== "")
95
+ .slice(-TAIL_LINES);
96
+ }
97
+ export async function succeeds(command, args, cwd) {
98
+ try {
99
+ return (await exitCode(command, args, cwd, "ignore")) === 0;
100
+ }
101
+ catch (error) {
102
+ if (error instanceof CommandFailed)
103
+ return false;
104
+ throw error;
105
+ }
106
+ }
107
+ async function exitCode(command, args, cwd, stdio) {
108
+ return closed(spawn(command, args, { cwd, env: withoutGitContext(), stdio }), command);
109
+ }
110
+ async function closed(child, command) {
111
+ return new Promise((resolve, reject) => {
112
+ child.on("error", (error) => {
113
+ reject(new CommandFailed(`${command} couldn't start: ${error.message}`));
114
+ });
115
+ child.on("close", resolve);
116
+ });
117
+ }
package/dist/stacks.js ADDED
@@ -0,0 +1,42 @@
1
+ export const BTS = "create-better-t-stack@3.49.0";
2
+ export const STACKS = {
3
+ "react-router": {
4
+ label: "React Router",
5
+ signature: "apps/web/react-router.config.ts",
6
+ },
7
+ next: { label: "Next.js", signature: "apps/web/next.config.ts" },
8
+ };
9
+ export const MONOREPOS = {
10
+ pnpm: { status: "built", summary: "pnpm workspaces", addon: "none" },
11
+ turborepo: { status: "built", summary: "Turborepo", addon: "turborepo" },
12
+ nx: { status: "built", summary: "Nx", addon: "nx" },
13
+ };
14
+ const NONE = [
15
+ "backend",
16
+ "runtime",
17
+ "database",
18
+ "orm",
19
+ "api",
20
+ "auth",
21
+ "payments",
22
+ "examples",
23
+ "db-setup",
24
+ "web-deploy",
25
+ "server-deploy",
26
+ ];
27
+ export function btsArgs(name, stack, monorepo) {
28
+ return [
29
+ name,
30
+ "--frontend",
31
+ stack,
32
+ "--addons",
33
+ MONOREPOS[monorepo].addon,
34
+ ...NONE.flatMap((option) => [`--${option}`, "none"]),
35
+ "--no-install",
36
+ "--no-git",
37
+ "--package-manager",
38
+ "pnpm",
39
+ "--disable-analytics",
40
+ "--no-render-title",
41
+ ];
42
+ }