create-fs-app 2.0.3 → 3.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.
Files changed (60) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +255 -151
  3. package/dist/cli/output.js +223 -0
  4. package/dist/cli/program.js +403 -0
  5. package/dist/cli/resolve-config.js +264 -0
  6. package/dist/cli/ui/errors.js +27 -0
  7. package/dist/cli/ui/help.js +43 -0
  8. package/dist/cli/ui/hints.js +22 -0
  9. package/dist/cli/ui/layer-log.js +109 -0
  10. package/dist/cli/ui/list.js +115 -0
  11. package/dist/cli/ui/text.js +56 -0
  12. package/dist/cli/wizard.js +275 -0
  13. package/dist/core/auth/jwt.js +478 -0
  14. package/dist/core/download.js +109 -0
  15. package/dist/core/health-check.js +42 -44
  16. package/dist/core/presets.js +75 -117
  17. package/dist/core/registry-fetch.js +101 -81
  18. package/dist/core/registry.fallback.json +892 -0
  19. package/dist/core/scaffold.js +121 -75
  20. package/dist/core/stack.js +109 -0
  21. package/dist/core/template-cache.js +95 -154
  22. package/dist/core/template-clone.js +351 -655
  23. package/dist/core/template-list.js +19 -141
  24. package/dist/core/template-registry.js +116 -216
  25. package/dist/index.js +13 -963
  26. package/dist/types/index.js +30 -82
  27. package/dist/utils/cli-error.js +15 -0
  28. package/dist/utils/exec.js +62 -0
  29. package/dist/utils/logger.js +10 -9
  30. package/dist/utils/theme.js +87 -0
  31. package/dist/utils/validation.js +24 -13
  32. package/package.json +22 -25
  33. package/dist/core/health-check.d.ts +0 -15
  34. package/dist/core/health-check.js.map +0 -1
  35. package/dist/core/presets.d.ts +0 -48
  36. package/dist/core/presets.js.map +0 -1
  37. package/dist/core/registry-fetch.d.ts +0 -20
  38. package/dist/core/registry-fetch.js.map +0 -1
  39. package/dist/core/scaffold.d.ts +0 -40
  40. package/dist/core/scaffold.js.map +0 -1
  41. package/dist/core/template-cache.d.ts +0 -38
  42. package/dist/core/template-cache.js.map +0 -1
  43. package/dist/core/template-clone.d.ts +0 -27
  44. package/dist/core/template-clone.js.map +0 -1
  45. package/dist/core/template-list.d.ts +0 -45
  46. package/dist/core/template-list.js.map +0 -1
  47. package/dist/core/template-registry.d.ts +0 -78
  48. package/dist/core/template-registry.js.map +0 -1
  49. package/dist/index.d.ts +0 -2
  50. package/dist/index.js.bak +0 -872
  51. package/dist/index.js.map +0 -1
  52. package/dist/types/index.d.ts +0 -102
  53. package/dist/types/index.js.map +0 -1
  54. package/dist/utils/errors.d.ts +0 -15
  55. package/dist/utils/errors.js +0 -50
  56. package/dist/utils/errors.js.map +0 -1
  57. package/dist/utils/logger.d.ts +0 -37
  58. package/dist/utils/logger.js.map +0 -1
  59. package/dist/utils/validation.d.ts +0 -48
  60. package/dist/utils/validation.js.map +0 -1
@@ -0,0 +1,275 @@
1
+ /**
2
+ * Interactive wizard (clack).
3
+ *
4
+ * Questions: name → stack fields (each list only holds values that still
5
+ * have a template; single options are picked automatically) → package
6
+ * manager → extras → auth (if the backend supports it) → install → review.
7
+ * Options carry hints from the registry layer data. The review shows the
8
+ * layer log; "Change an answer" lets the user change any answer, and
9
+ * changing a stack field re-asks the later fields that no longer fit.
10
+ * Values passed as flags are pre-filled and not asked.
11
+ */
12
+ import fs from 'fs';
13
+ import path from 'path';
14
+ import * as p from '@clack/prompts';
15
+ import { PACKAGE_MANAGERS, ProjectConfigSchema, } from '../types/index.js';
16
+ import { FIELD_LABELS, STACK_FIELDS, availableOptions, autoPick, findTemplate, labelFor, } from '../core/stack.js';
17
+ import { AUTH_SUPPORT, supportsAuth } from '../core/auth/jwt.js';
18
+ import { validateProjectName } from '../utils/validation.js';
19
+ import { defaultFor } from './resolve-config.js';
20
+ import { describeExtras, rows, yesNo } from './output.js';
21
+ import { detectTheme } from '../utils/theme.js';
22
+ import { optionHint } from './ui/hints.js';
23
+ import { renderLayerLog } from './ui/layer-log.js';
24
+ const theme = () => detectTheme(process.stdout);
25
+ /** Thrown when the user cancels (Ctrl+C or "Cancel"). */
26
+ export class WizardCancelled extends Error {
27
+ constructor() {
28
+ super('Cancelled');
29
+ this.name = 'WizardCancelled';
30
+ }
31
+ }
32
+ function answer(value) {
33
+ if (p.isCancel(value))
34
+ throw new WizardCancelled();
35
+ return value;
36
+ }
37
+ const EXTRA_LABELS = {
38
+ eslint: 'ESLint',
39
+ prettier: 'Prettier',
40
+ docker: 'Docker',
41
+ ci: 'GitHub Actions CI',
42
+ git: 'Initialize git',
43
+ };
44
+ const EXTRA_HINTS = {
45
+ eslint: 'lint scripts + configs',
46
+ prettier: 'format script',
47
+ docker: 'database compose file + app Dockerfiles',
48
+ ci: '.github/workflows/ci.yml',
49
+ git: 'git init + first commit',
50
+ };
51
+ const PM_HINTS = {
52
+ npm: 'bundled with Node.js',
53
+ pnpm: 'fast, strict workspaces',
54
+ yarn: 'yarn workspaces',
55
+ };
56
+ /** Package manager that launched us (npm_config_user_agent = "pnpm/9.1.0 npm/? node/..."), else npm. */
57
+ export function detectPackageManager(userAgent = process.env.npm_config_user_agent) {
58
+ const name = userAgent?.split('/')[0];
59
+ return PACKAGE_MANAGERS.includes(name ?? '') ? name : 'npm';
60
+ }
61
+ function nameError(value) {
62
+ const name = (value ?? '').trim();
63
+ const result = validateProjectName(name);
64
+ if (!result.valid)
65
+ return result.error;
66
+ if (fs.existsSync(path.resolve(name)))
67
+ return `Directory "${name}" already exists`;
68
+ return undefined;
69
+ }
70
+ export async function askName(initial) {
71
+ const name = answer(await p.text({
72
+ message: 'Project name',
73
+ placeholder: 'my-app',
74
+ initialValue: initial,
75
+ validate: nameError,
76
+ }));
77
+ return name.trim();
78
+ }
79
+ async function askStackField(templates, stack, field, options, current) {
80
+ const dot = theme().glyphs.dot;
81
+ return answer(await p.select({
82
+ message: FIELD_LABELS[field],
83
+ options: options.map(value => ({ value, label: labelFor(value), hint: optionHint(templates, field, value, stack, dot) })),
84
+ initialValue: current && options.includes(current) ? current : defaultFor(field, options),
85
+ }));
86
+ }
87
+ /**
88
+ * Fill stack fields from `from` on: keep values that are still available,
89
+ * pick single options, ask the rest. With `ask`, the field `from` is asked
90
+ * even if it already has a value.
91
+ */
92
+ async function fillStack(templates, stack, from = 'monorepo', ask = false) {
93
+ for (const field of STACK_FIELDS.slice(STACK_FIELDS.indexOf(from))) {
94
+ const options = availableOptions(templates, field, stack);
95
+ const current = stack[field];
96
+ const forced = ask && field === from;
97
+ if (!forced && current !== undefined && options.includes(current))
98
+ continue;
99
+ const single = autoPick(options);
100
+ if (single) {
101
+ if (current !== single)
102
+ p.log.info(`${FIELD_LABELS[field]}: ${labelFor(single)} ${theme().dim('(only option)')}`);
103
+ stack[field] = single;
104
+ continue;
105
+ }
106
+ if (current !== undefined && !forced) {
107
+ p.log.warn(`${FIELD_LABELS[field]} ${labelFor(current)} is not available with this stack`);
108
+ }
109
+ stack[field] = await askStackField(templates, stack, field, options, current);
110
+ }
111
+ }
112
+ function templateFor(templates, stack) {
113
+ const template = findTemplate(templates, stack);
114
+ // fillStack only offers values that have a template
115
+ if (!template)
116
+ throw new Error(`No template for ${JSON.stringify(stack)}`);
117
+ return template;
118
+ }
119
+ function extrasFor(template) {
120
+ return ['eslint', 'prettier', 'docker', 'ci', 'git']
121
+ .filter(e => e !== 'docker' || template.supports.docker);
122
+ }
123
+ async function askPackageManager(current) {
124
+ return answer(await p.select({
125
+ message: 'Package manager',
126
+ options: PACKAGE_MANAGERS.map(value => ({
127
+ value,
128
+ label: value,
129
+ hint: value === detectPackageManager() ? `${PM_HINTS[value]}, detected` : PM_HINTS[value],
130
+ })),
131
+ initialValue: current,
132
+ }));
133
+ }
134
+ async function askExtras(state, extras) {
135
+ if (extras.length === 0)
136
+ return;
137
+ const chosen = answer(await p.multiselect({
138
+ message: 'Extras',
139
+ options: extras.map(value => ({ value, label: EXTRA_LABELS[value], hint: EXTRA_HINTS[value] })),
140
+ initialValues: extras.filter(e => state.extras[e]),
141
+ required: false,
142
+ }));
143
+ for (const extra of extras)
144
+ state.extras[extra] = chosen.includes(extra);
145
+ }
146
+ async function askAuth(current) {
147
+ return answer(await p.select({
148
+ message: 'Authentication',
149
+ options: [
150
+ { value: 'none', label: 'None', hint: 'no auth code' },
151
+ { value: 'jwt', label: 'JWT', hint: 'login + protected route, credentials from .env' },
152
+ ],
153
+ initialValue: current,
154
+ }));
155
+ }
156
+ async function askInstall(current) {
157
+ return answer(await p.confirm({ message: 'Install dependencies now?', initialValue: current }));
158
+ }
159
+ /** Drop answers the new stack no longer allows. */
160
+ function reconcile(state, template) {
161
+ if (state.auth === 'jwt' && !supportsAuth(template.stack.backend)) {
162
+ state.auth = 'none';
163
+ p.log.warn(`JWT auth is available for ${AUTH_SUPPORT.join(', ')} only; auth set to none.`);
164
+ }
165
+ }
166
+ /** Review text: the layer log (74 columns, inside the clack note) plus extras, auth and install. */
167
+ function reviewText(state, template) {
168
+ const t = theme();
169
+ const docker = state.extras.docker && template.supports.docker;
170
+ const auth = supportsAuth(template.stack.backend) ? state.auth : 'none';
171
+ const log = renderLayerLog({
172
+ title: state.name, template, packageManager: state.packageManager, docker, auth,
173
+ }, t, 74);
174
+ const details = rows([
175
+ ['template', template.key],
176
+ ['extras', describeExtras({ ...state.extras, docker })],
177
+ ['auth', supportsAuth(template.stack.backend) ? state.auth : t.dim(`not available for ${labelFor(template.stack.backend)}`)],
178
+ ['install', yesNo(state.install)],
179
+ ]).map(line => ` ${line}`);
180
+ return [log, '', ...details].join('\n');
181
+ }
182
+ /**
183
+ * Run the wizard. Returns the final config; throws WizardCancelled.
184
+ */
185
+ export async function runWizard(seed, templates) {
186
+ const given = seed.given;
187
+ const name = seed.name ?? await askName();
188
+ const stack = { ...seed.stack };
189
+ await fillStack(templates, stack);
190
+ let template = templateFor(templates, stack);
191
+ const state = {
192
+ name,
193
+ stack,
194
+ packageManager: given.packageManager ?? detectPackageManager(),
195
+ extras: {
196
+ eslint: given.eslint ?? true,
197
+ prettier: given.prettier ?? true,
198
+ docker: given.docker ?? true,
199
+ ci: given.ci ?? false,
200
+ git: given.git ?? true,
201
+ },
202
+ auth: given.auth ?? 'none',
203
+ install: given.install ?? false,
204
+ };
205
+ if (given.packageManager === undefined)
206
+ state.packageManager = await askPackageManager(state.packageManager);
207
+ await askExtras(state, extrasFor(template).filter(e => given[e] === undefined));
208
+ if (given.auth === undefined && supportsAuth(template.stack.backend))
209
+ state.auth = await askAuth(state.auth);
210
+ reconcile(state, template);
211
+ if (given.install === undefined)
212
+ state.install = await askInstall(state.install);
213
+ for (;;) {
214
+ p.note(reviewText(state, template), 'Review');
215
+ const action = answer(await p.select({
216
+ message: 'Ready?',
217
+ options: [
218
+ { value: 'create', label: 'Create project' },
219
+ { value: 'change', label: 'Change an answer' },
220
+ { value: 'cancel', label: 'Cancel' },
221
+ ],
222
+ initialValue: 'create',
223
+ }));
224
+ if (action === 'cancel')
225
+ throw new WizardCancelled();
226
+ if (action === 'create') {
227
+ return ProjectConfigSchema.parse({
228
+ name: state.name,
229
+ stack: stack,
230
+ packageManager: state.packageManager,
231
+ ...state.extras,
232
+ docker: state.extras.docker && template.supports.docker,
233
+ auth: supportsAuth(template.stack.backend) ? state.auth : 'none',
234
+ install: state.install,
235
+ });
236
+ }
237
+ const changeable = STACK_FIELDS.filter(f => availableOptions(templates, f, stack).length > 1);
238
+ const change = answer(await p.select({
239
+ message: 'Change which answer?',
240
+ options: [
241
+ { value: 'name', label: 'Name', hint: state.name },
242
+ ...changeable.map(f => ({ value: f, label: FIELD_LABELS[f], hint: labelFor(stack[f] ?? '') })),
243
+ { value: 'packageManager', label: 'Package manager', hint: state.packageManager },
244
+ { value: 'extras', label: 'Extras' },
245
+ ...(supportsAuth(template.stack.backend) ? [{ value: 'auth', label: 'Auth', hint: state.auth }] : []),
246
+ { value: 'install', label: 'Install', hint: yesNo(state.install) },
247
+ { value: 'back', label: 'Back' },
248
+ ],
249
+ initialValue: 'name',
250
+ }));
251
+ switch (change) {
252
+ case 'back':
253
+ break;
254
+ case 'name':
255
+ state.name = await askName(state.name);
256
+ break;
257
+ case 'packageManager':
258
+ state.packageManager = await askPackageManager(state.packageManager);
259
+ break;
260
+ case 'extras':
261
+ await askExtras(state, extrasFor(template));
262
+ break;
263
+ case 'auth':
264
+ state.auth = await askAuth(state.auth);
265
+ break;
266
+ case 'install':
267
+ state.install = await askInstall(state.install);
268
+ break;
269
+ default:
270
+ await fillStack(templates, stack, change, true);
271
+ template = templateFor(templates, stack);
272
+ reconcile(state, template);
273
+ }
274
+ }
275
+ }