create-cwa 2.0.0-alpha.5 → 2.0.0-alpha.50
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 +55 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +415 -112
- package/dist/preflight.d.ts +12 -0
- package/dist/preflight.js +107 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.js +1 -0
- package/package.json +7 -7
- package/smoke.sh +63 -0
- package/src/index.ts +441 -123
- package/src/preflight.ts +125 -0
- package/src/types.ts +1 -0
- package/.gitignore +0 -2
package/dist/index.js
CHANGED
|
@@ -3,30 +3,132 @@ import { downloadTemplate } from 'giget';
|
|
|
3
3
|
import fse from 'fs-extra';
|
|
4
4
|
import fg from 'fast-glob';
|
|
5
5
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
6
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
6
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
7
7
|
import { join, resolve } from 'node:path';
|
|
8
8
|
import { tmpdir } from 'node:os';
|
|
9
9
|
import { randomBytes } from 'node:crypto';
|
|
10
10
|
import { spawnSync } from 'node:child_process';
|
|
11
|
+
import { styleText } from 'node:util';
|
|
12
|
+
import { preflight } from './preflight.js';
|
|
11
13
|
const REPO = 'components-web-app/components-web-app';
|
|
12
14
|
// The CLI and the template share a version: create-cwa X downloads the template at
|
|
13
15
|
// tag vX, so an install is reproducible. `--ref <branch|tag>` overrides it, for
|
|
14
16
|
// example `--ref main` for the latest unreleased template.
|
|
15
17
|
const CLI_VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;
|
|
18
|
+
const USAGE = `Usage: create-cwa [project-name] [options]
|
|
19
|
+
|
|
20
|
+
Without options every question is asked. A flag answers its question, so it isn't asked.
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--ref <tag|branch> template version to download (default v${CLI_VERSION})
|
|
24
|
+
--template-dir <path> copy the template from a local checkout instead of downloading it
|
|
25
|
+
--pipeline <id> CI/CD pipeline: github, gitlab or none
|
|
26
|
+
--features <list> comma-separated features, "all" or "none"
|
|
27
|
+
--fixtures, --no-fixtures include the sample pages, or not
|
|
28
|
+
--start, --no-start run bin/up once the project is created, or not
|
|
29
|
+
--install-deps, --no-install-deps
|
|
30
|
+
pnpm install in app/ on this machine (only when the stack isn't started)
|
|
31
|
+
-y, --yes take the default for every question no flag answers
|
|
32
|
+
--ci never prompt (implies --yes); a missing project name is an error
|
|
33
|
+
-h, --help show this help`;
|
|
34
|
+
class UsageError extends Error {
|
|
35
|
+
}
|
|
36
|
+
const VALUE_FLAGS = ['--ref', '--template-dir', '--pipeline', '--features'];
|
|
37
|
+
// Flags for unattended runs (#215); with none given, every question is asked as before.
|
|
16
38
|
function parseArgs(argv) {
|
|
17
39
|
const args = argv.slice(2);
|
|
18
|
-
|
|
40
|
+
const opts = { ref: `v${CLI_VERSION}`, refGiven: false, yes: false, ci: false, help: false };
|
|
19
41
|
const positional = [];
|
|
20
42
|
for (let i = 0; i < args.length; i++) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
43
|
+
let arg = args[i];
|
|
44
|
+
let value;
|
|
45
|
+
const eq = arg.indexOf('=');
|
|
46
|
+
if (arg.startsWith('--') && eq > 0) {
|
|
47
|
+
value = arg.slice(eq + 1);
|
|
48
|
+
arg = arg.slice(0, eq);
|
|
49
|
+
if (!VALUE_FLAGS.includes(arg))
|
|
50
|
+
throw new UsageError(`${arg} takes no value`);
|
|
51
|
+
}
|
|
52
|
+
else if (VALUE_FLAGS.includes(arg)) {
|
|
53
|
+
value = args[i + 1];
|
|
54
|
+
if (value === undefined || value.startsWith('-'))
|
|
55
|
+
throw new UsageError(`${arg} needs a value`);
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
switch (arg) {
|
|
59
|
+
case '--':
|
|
60
|
+
break;
|
|
61
|
+
case '--ref':
|
|
62
|
+
opts.ref = value;
|
|
63
|
+
opts.refGiven = true;
|
|
64
|
+
break;
|
|
65
|
+
case '--template-dir':
|
|
66
|
+
opts.templateDir = value;
|
|
67
|
+
break;
|
|
68
|
+
case '--pipeline':
|
|
69
|
+
opts.pipeline = value;
|
|
70
|
+
break;
|
|
71
|
+
case '--features':
|
|
72
|
+
opts.features = value.split(',').map((f) => f.trim()).filter(Boolean);
|
|
73
|
+
break;
|
|
74
|
+
case '--fixtures':
|
|
75
|
+
case '--no-fixtures':
|
|
76
|
+
opts.fixtures = arg === '--fixtures';
|
|
77
|
+
break;
|
|
78
|
+
case '--start':
|
|
79
|
+
case '--no-start':
|
|
80
|
+
opts.start = arg === '--start';
|
|
81
|
+
break;
|
|
82
|
+
case '--install-deps':
|
|
83
|
+
case '--no-install-deps':
|
|
84
|
+
opts.installDeps = arg === '--install-deps';
|
|
85
|
+
break;
|
|
86
|
+
case '-y':
|
|
87
|
+
case '--yes':
|
|
88
|
+
opts.yes = true;
|
|
89
|
+
break;
|
|
90
|
+
case '--ci':
|
|
91
|
+
opts.ci = true;
|
|
92
|
+
opts.yes = true;
|
|
93
|
+
break;
|
|
94
|
+
case '-h':
|
|
95
|
+
case '--help':
|
|
96
|
+
opts.help = true;
|
|
97
|
+
break;
|
|
98
|
+
default:
|
|
99
|
+
if (arg.startsWith('-'))
|
|
100
|
+
throw new UsageError(`Unknown option ${arg}`);
|
|
101
|
+
positional.push(arg);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (positional.length > 1)
|
|
105
|
+
throw new UsageError(`Unexpected argument "${positional[1]}"`);
|
|
106
|
+
if (opts.templateDir && opts.refGiven)
|
|
107
|
+
throw new UsageError("--ref and --template-dir can't be used together");
|
|
108
|
+
if (opts.start && opts.installDeps) {
|
|
109
|
+
throw new UsageError('--install-deps is only for a stack that isn\'t started: the app container installs its own');
|
|
110
|
+
}
|
|
111
|
+
opts.projectName = positional[0];
|
|
112
|
+
return opts;
|
|
113
|
+
}
|
|
114
|
+
function fail(message) {
|
|
115
|
+
cancel(message);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
// A spinner when someone is watching, plain log lines under --ci.
|
|
119
|
+
function progress(plain) {
|
|
120
|
+
return plain ? { start: (msg) => log.step(msg), stop: (msg) => log.info(msg) } : spinner();
|
|
121
|
+
}
|
|
122
|
+
async function readLocalManifest(templateDir) {
|
|
123
|
+
const path = join(resolve(templateDir), 'cwa-manifest.json');
|
|
124
|
+
if (!existsSync(path))
|
|
125
|
+
throw new Error(`No CWA template in "${templateDir}": ${path} is missing.`);
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(await readFile(path, 'utf-8'));
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
throw new Error(`${path} is not valid JSON.`);
|
|
131
|
+
}
|
|
30
132
|
}
|
|
31
133
|
async function fetchManifest(ref) {
|
|
32
134
|
let res;
|
|
@@ -66,29 +168,90 @@ function run(cmd, args, cwd) {
|
|
|
66
168
|
const result = spawnSync(cmd, args, { stdio: 'inherit', cwd });
|
|
67
169
|
return result.status === 0;
|
|
68
170
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
171
|
+
function withHint(message, hint) {
|
|
172
|
+
return hint ? `${message} ${styleText('dim', `(${hint})`)}` : message;
|
|
173
|
+
}
|
|
174
|
+
// The name becomes the directory and the compose project name.
|
|
175
|
+
const PROJECT_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
176
|
+
function validateProjectName(value) {
|
|
177
|
+
const name = value?.trim() ?? '';
|
|
178
|
+
if (!name)
|
|
179
|
+
return 'Project name is required';
|
|
180
|
+
if (!PROJECT_NAME.test(name)) {
|
|
181
|
+
return 'Use lower-case letters, digits and single dashes (for example my-cwa-app)';
|
|
182
|
+
}
|
|
183
|
+
const dir = resolve(process.cwd(), name);
|
|
184
|
+
if (existsSync(dir) && (!statSync(dir).isDirectory() || readdirSync(dir).length > 0)) {
|
|
185
|
+
return `"${name}" already exists here and is not an empty directory`;
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
// Each answer comes from its flag, else the default under --yes (what Enter picks), else a prompt (#215).
|
|
190
|
+
async function askQuestions(manifest, opts) {
|
|
191
|
+
const question = (id) => manifest.questions.find((q) => q.id === id);
|
|
192
|
+
const nameQuestion = question('projectName');
|
|
193
|
+
let projectName;
|
|
194
|
+
if (opts.yes && opts.projectName !== undefined) {
|
|
195
|
+
const error = validateProjectName(opts.projectName);
|
|
196
|
+
if (error)
|
|
197
|
+
fail(`Project name "${opts.projectName}": ${error}`);
|
|
198
|
+
projectName = opts.projectName;
|
|
199
|
+
}
|
|
200
|
+
else if (opts.ci) {
|
|
201
|
+
fail('--ci needs the project name: create-cwa <project-name> --ci');
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
projectName = checkCancel(await text({
|
|
205
|
+
message: withHint(nameQuestion?.message ?? 'Project name?', nameQuestion?.hint),
|
|
206
|
+
placeholder: nameQuestion?.placeholder ?? 'my-cwa-app',
|
|
207
|
+
initialValue: opts.projectName ?? '',
|
|
208
|
+
validate: validateProjectName,
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
const ciQuestion = question('ci');
|
|
212
|
+
const ciOptions = ciQuestion?.choices?.map((c) => ({ value: c.value, label: c.label, hint: c.hint })) ?? [
|
|
213
|
+
{ value: 'github', label: 'GitHub Actions' },
|
|
214
|
+
{ value: 'gitlab', label: 'GitLab CI' },
|
|
215
|
+
{ value: 'none', label: 'None' },
|
|
216
|
+
];
|
|
217
|
+
let ci;
|
|
218
|
+
if (opts.pipeline !== undefined) {
|
|
219
|
+
if (!ciOptions.some((c) => c.value === opts.pipeline)) {
|
|
220
|
+
fail(`--pipeline ${opts.pipeline}: choose one of ${ciOptions.map((c) => c.value).join(', ')}`);
|
|
221
|
+
}
|
|
222
|
+
ci = opts.pipeline;
|
|
223
|
+
}
|
|
224
|
+
else if (opts.yes) {
|
|
225
|
+
ci = ciOptions[0].value;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
ci = checkCancel(await select({
|
|
229
|
+
message: withHint(ciQuestion?.message ?? 'CI/CD pipeline?', ciQuestion?.hint),
|
|
230
|
+
options: ciOptions,
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
const featuresQuestion = question('features');
|
|
234
|
+
const featureChoices = featuresQuestion?.choices?.map((c) => ({ value: c.value, label: c.label, hint: c.hint })) ?? [];
|
|
235
|
+
let features;
|
|
236
|
+
if (opts.features !== undefined) {
|
|
237
|
+
const all = featureChoices.map((c) => c.value);
|
|
238
|
+
const flag = opts.features;
|
|
239
|
+
features = flag.length === 1 && flag[0] === 'all' ? all : flag.length === 1 && flag[0] === 'none' ? [] : flag;
|
|
240
|
+
const unknown = features.filter((f) => !all.includes(f));
|
|
241
|
+
if (unknown.length) {
|
|
242
|
+
fail(`--features: unknown ${unknown.join(', ')}. Choose from ${all.join(', ')}, or "all" or "none"`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else if (opts.yes) {
|
|
246
|
+
features = [];
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
features = checkCancel(await multiselect({
|
|
250
|
+
message: withHint(`${featuresQuestion?.message ?? 'Which features to include?'} (space to toggle, enter to confirm)`, featuresQuestion?.hint),
|
|
251
|
+
options: featureChoices,
|
|
252
|
+
required: false,
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
92
255
|
// Auto-include required features
|
|
93
256
|
const resolved = new Set(features);
|
|
94
257
|
for (const feat of features) {
|
|
@@ -101,14 +264,74 @@ async function askQuestions(manifest, targetArg) {
|
|
|
101
264
|
const added = [...resolved].filter((f) => !features.includes(f));
|
|
102
265
|
note(`Also enabling: ${added.join(', ')} (required by selected features)`, 'Dependencies');
|
|
103
266
|
}
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
267
|
+
const fixturesQuestion = question('fixtures');
|
|
268
|
+
const fixtures = opts.fixtures ??
|
|
269
|
+
(opts.yes
|
|
270
|
+
? (fixturesQuestion?.default ?? true)
|
|
271
|
+
: checkCancel(await confirm({
|
|
272
|
+
message: withHint(fixturesQuestion?.message ?? 'Include database fixtures for selected features?', fixturesQuestion?.hint),
|
|
273
|
+
initialValue: fixturesQuestion?.default ?? true,
|
|
274
|
+
})));
|
|
108
275
|
return { projectName: projectName.trim(), ci, features: [...resolved], fixtures };
|
|
109
276
|
}
|
|
110
|
-
|
|
111
|
-
|
|
277
|
+
// Files a download would contain: what git tracks or would track, minus export-ignore paths (GitHub's tarball honours them).
|
|
278
|
+
function templateFiles(src) {
|
|
279
|
+
const ls = spawnSync('git', ['ls-files', '-z', '--cached', '--others', '--exclude-standard'], {
|
|
280
|
+
cwd: src,
|
|
281
|
+
encoding: 'utf8',
|
|
282
|
+
});
|
|
283
|
+
if (ls.status !== 0)
|
|
284
|
+
return null;
|
|
285
|
+
const files = ls.stdout.split('\0').filter((f) => f && existsSync(join(src, f)));
|
|
286
|
+
const paths = new Set();
|
|
287
|
+
for (const file of files) {
|
|
288
|
+
const parts = file.split('/');
|
|
289
|
+
for (let i = 1; i <= parts.length; i++)
|
|
290
|
+
paths.add(parts.slice(0, i).join('/'));
|
|
291
|
+
}
|
|
292
|
+
const attr = spawnSync('git', ['check-attr', '-z', '--stdin', 'export-ignore'], {
|
|
293
|
+
cwd: src,
|
|
294
|
+
input: [...paths].join('\0'),
|
|
295
|
+
encoding: 'utf8',
|
|
296
|
+
});
|
|
297
|
+
if (attr.status !== 0)
|
|
298
|
+
return null;
|
|
299
|
+
const out = attr.stdout.split('\0');
|
|
300
|
+
const ignored = new Set();
|
|
301
|
+
for (let i = 0; i + 2 < out.length; i += 3) {
|
|
302
|
+
if (out[i + 2] === 'set')
|
|
303
|
+
ignored.add(out[i]);
|
|
304
|
+
}
|
|
305
|
+
return files.filter((file) => {
|
|
306
|
+
const parts = file.split('/');
|
|
307
|
+
for (let i = 1; i <= parts.length; i++)
|
|
308
|
+
if (ignored.has(parts.slice(0, i).join('/')))
|
|
309
|
+
return false;
|
|
310
|
+
return true;
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
// --template-dir (#215): a local checkout instead of the download; excludes and stripping still apply after.
|
|
314
|
+
async function copyTemplateDir(templateDir, tempDir, plain) {
|
|
315
|
+
const src = resolve(templateDir);
|
|
316
|
+
const s = progress(plain);
|
|
317
|
+
s.start(`Copying template from ${src}...`);
|
|
318
|
+
const files = templateFiles(src);
|
|
319
|
+
if (files) {
|
|
320
|
+
for (const file of files) {
|
|
321
|
+
await fse.copy(join(src, file), join(tempDir, file), { dereference: false });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
// Not a git checkout: everything but dependencies and VCS data.
|
|
326
|
+
const skip = new Set(['.git', 'node_modules', 'vendor']);
|
|
327
|
+
await fse.copy(src, tempDir, {
|
|
328
|
+
filter: (path) => !path.slice(src.length).split(/[\\/]/).some((part) => skip.has(part)),
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
s.stop('Template copied.');
|
|
332
|
+
}
|
|
333
|
+
async function downloadRepo(manifest, ref, tempDir, plain) {
|
|
334
|
+
const s = progress(plain);
|
|
112
335
|
s.start(`Downloading template (${ref})...`);
|
|
113
336
|
try {
|
|
114
337
|
await downloadTemplate(`github:${manifest.repo}#${ref}`, {
|
|
@@ -173,16 +396,18 @@ async function generateReadme(tempDir, answers) {
|
|
|
173
396
|
: answers.ci === 'gitlab'
|
|
174
397
|
? '## CI/CD\n\nGitLab CI is configured in `.gitlab-ci.yml`. See `bin/devops/` for the reusable pipeline scripts.\n'
|
|
175
398
|
: '';
|
|
176
|
-
const fixturesSection =
|
|
177
|
-
? `## Fixtures
|
|
399
|
+
const fixturesSection = `## Fixtures and the admin account
|
|
178
400
|
|
|
179
|
-
|
|
401
|
+
\`bin/up\` loads the fixtures with \`--append\`, which only creates what's missing: the admin
|
|
402
|
+
(\`ADMIN_USERNAME\` / \`ADMIN_PASSWORD\` / \`ADMIN_EMAIL\` in \`api/.env.local\`, default
|
|
403
|
+
\`admin\` / \`admin\` / \`admin@example.com\`)${answers.fixtures ? ' and the sample pages' : ''}. Each load resets the
|
|
404
|
+
admin's password to \`ADMIN_PASSWORD\`; \`bin/up --skip-fixtures\` keeps one changed in the admin.
|
|
405
|
+
To load them by hand:
|
|
180
406
|
|
|
181
407
|
\`\`\`bash
|
|
182
408
|
docker compose exec php bin/console doctrine:fixtures:load --append
|
|
183
409
|
\`\`\`
|
|
184
|
-
|
|
185
|
-
: '';
|
|
410
|
+
`;
|
|
186
411
|
const readme = `# ${answers.projectName}
|
|
187
412
|
|
|
188
413
|
A [CWA](https://github.com/components-web-app/components-web-app) project.
|
|
@@ -194,34 +419,40 @@ ${featureList}
|
|
|
194
419
|
## Getting started
|
|
195
420
|
|
|
196
421
|
\`\`\`bash
|
|
197
|
-
# Start the stack
|
|
198
|
-
#
|
|
199
|
-
|
|
200
|
-
docker compose up -d
|
|
422
|
+
# Start the stack, wait until it's healthy, load the fixtures, pre-compile the
|
|
423
|
+
# first pages and print where to sign in. Safe to run again.
|
|
424
|
+
bin/up
|
|
201
425
|
|
|
202
426
|
# Follow the Nuxt dev server
|
|
203
427
|
docker compose logs -f app
|
|
204
428
|
\`\`\`
|
|
205
429
|
|
|
206
|
-
\`
|
|
207
|
-
|
|
430
|
+
\`bin/up\` runs \`docker compose up -d --wait\`: the php container runs composer install,
|
|
431
|
+
waits for the database and migrates; the app container installs the Nuxt dependencies
|
|
432
|
+
and runs the dev server. Without a POSIX shell (Windows without WSL), run
|
|
433
|
+
\`docker compose up -d --wait\` and the fixtures command below instead.
|
|
434
|
+
|
|
435
|
+
\`app/\` is mounted into the app container, so its \`node_modules\` is shared: your
|
|
436
|
+
editor gets the types from the container's install. Don't run \`pnpm install\` on
|
|
437
|
+
this machine while the stack runs (it rewrites \`node_modules\` under the dev
|
|
438
|
+
server); if you do, run \`docker compose restart app\`.
|
|
208
439
|
|
|
209
440
|
| URL | Description |
|
|
210
441
|
|---|---|
|
|
211
442
|
| https://localhost | Nuxt app |
|
|
212
443
|
| https://localhost/_api | API (JSON-LD / HAL) |
|
|
213
444
|
| https://localhost/login | Sign in to the CWA admin |
|
|
445
|
+
| http://localhost:8025 | Mailpit: every email the API sends in dev |
|
|
214
446
|
|
|
215
|
-
> **
|
|
216
|
-
>
|
|
217
|
-
> then add \`caddy-local-root.crt\` to your system or browser trust store.
|
|
447
|
+
> **Certificate warning:** Caddy signs the local certificate with its own CA, so the browser warns
|
|
448
|
+
> on the first visit. Accepting the warning is expected for local development.
|
|
218
449
|
|
|
219
450
|
${fixturesSection}
|
|
220
451
|
${ciSection}## Learn more
|
|
221
452
|
|
|
222
453
|
- [CWA documentation](https://cwa.rocks)
|
|
223
|
-
- [API Components Bundle](https://github.com/
|
|
224
|
-
- [\`@cwa/nuxt\` module](https://github.com/components-web-app/cwa-nuxt-
|
|
454
|
+
- [API Components Bundle](https://github.com/components-web-app/api-components-bundle)
|
|
455
|
+
- [\`@cwa/nuxt\` module](https://github.com/components-web-app/cwa-nuxt-module)
|
|
225
456
|
`;
|
|
226
457
|
await writeFile(join(tempDir, 'README.md'), readme, 'utf-8');
|
|
227
458
|
}
|
|
@@ -234,25 +465,99 @@ async function moveToTarget(tempDir, targetDir) {
|
|
|
234
465
|
}
|
|
235
466
|
await fse.move(tempDir, targetDir, { overwrite: false });
|
|
236
467
|
}
|
|
468
|
+
// Decides, before anything is downloaded, whether to start the stack and whether to
|
|
469
|
+
// install app/ dependencies on the host. The host install shares app/node_modules with
|
|
470
|
+
// the app container, so it's only offered when the stack isn't started now (#204).
|
|
471
|
+
// --start fails when the stack can't be started; without it (prompted or --yes) the project is created anyway (#215).
|
|
472
|
+
async function askSetup(opts) {
|
|
473
|
+
let startDocker = false;
|
|
474
|
+
if (commandExists('docker')) {
|
|
475
|
+
startDocker =
|
|
476
|
+
opts.start ??
|
|
477
|
+
(opts.yes ||
|
|
478
|
+
checkCancel(await confirm({
|
|
479
|
+
message: withHint('Start the Docker stack when the project is ready?', 'bin/up: starts it, loads fixtures, prints the login'),
|
|
480
|
+
initialValue: true,
|
|
481
|
+
})));
|
|
482
|
+
}
|
|
483
|
+
else if (opts.start) {
|
|
484
|
+
fail('--start: docker not found. Install Docker Desktop (or Docker Engine with the compose plugin).');
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
log.warn('docker not found. Install Docker Desktop, then run `bin/up` in the project.');
|
|
488
|
+
}
|
|
489
|
+
if (startDocker) {
|
|
490
|
+
const s = progress(opts.ci);
|
|
491
|
+
s.start('Checking Docker and ports...');
|
|
492
|
+
const result = await preflight();
|
|
493
|
+
s.stop('Docker checked.');
|
|
494
|
+
const problem = !result.dockerRunning
|
|
495
|
+
? 'Docker is installed but `docker info` failed: start Docker Desktop (or the Docker daemon).'
|
|
496
|
+
: result.takenPorts.join('\n\n');
|
|
497
|
+
if (result.memoryWarning)
|
|
498
|
+
log.warn(result.memoryWarning);
|
|
499
|
+
if (problem) {
|
|
500
|
+
if (opts.start)
|
|
501
|
+
fail(`--start: ${problem}`);
|
|
502
|
+
log.warn(problem);
|
|
503
|
+
log.info('The project will be created without starting Docker. Run `bin/up` in it once that is fixed.');
|
|
504
|
+
startDocker = false;
|
|
505
|
+
}
|
|
506
|
+
return { startDocker, installDeps: false };
|
|
507
|
+
}
|
|
508
|
+
let installDeps = false;
|
|
509
|
+
if (commandExists('pnpm')) {
|
|
510
|
+
installDeps =
|
|
511
|
+
opts.installDeps ??
|
|
512
|
+
(!opts.yes &&
|
|
513
|
+
checkCancel(await confirm({
|
|
514
|
+
message: withHint('Install app/ dependencies on this machine?', 'only for editor types; the app container installs its own on first start'),
|
|
515
|
+
initialValue: false,
|
|
516
|
+
})));
|
|
517
|
+
}
|
|
518
|
+
else if (opts.installDeps) {
|
|
519
|
+
fail('--install-deps: pnpm not found.');
|
|
520
|
+
}
|
|
521
|
+
return { startDocker, installDeps };
|
|
522
|
+
}
|
|
237
523
|
async function main() {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
524
|
+
let args;
|
|
525
|
+
try {
|
|
526
|
+
args = parseArgs(process.argv);
|
|
527
|
+
}
|
|
528
|
+
catch (err) {
|
|
529
|
+
if (!(err instanceof UsageError))
|
|
530
|
+
throw err;
|
|
531
|
+
console.error(`create-cwa: ${err.message}\n\n${USAGE}`);
|
|
532
|
+
process.exit(2);
|
|
533
|
+
}
|
|
534
|
+
if (args.help) {
|
|
535
|
+
console.log(USAGE);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
intro(`create-cwa ${CLI_VERSION} — Components Web App scaffolder`);
|
|
539
|
+
log.warn('CWA is a prerelease: expect breaking changes between versions (see CHANGELOG.md on GitHub).\n' +
|
|
540
|
+
'create-cwa collects no telemetry; it only downloads the template from GitHub.');
|
|
541
|
+
const s = progress(args.ci);
|
|
542
|
+
s.start(args.templateDir ? `Reading the manifest in ${args.templateDir}...` : 'Fetching manifest...');
|
|
242
543
|
let manifest;
|
|
243
544
|
try {
|
|
244
|
-
manifest = await fetchManifest(args.ref);
|
|
545
|
+
manifest = args.templateDir ? await readLocalManifest(args.templateDir) : await fetchManifest(args.ref);
|
|
245
546
|
s.stop('Manifest loaded.');
|
|
246
547
|
}
|
|
247
548
|
catch (err) {
|
|
248
549
|
s.stop(err.message);
|
|
249
550
|
process.exit(1);
|
|
250
551
|
}
|
|
251
|
-
const answers = await askQuestions(manifest, args
|
|
552
|
+
const answers = await askQuestions(manifest, args);
|
|
553
|
+
const setup = await askSetup(args);
|
|
252
554
|
const targetDir = resolve(process.cwd(), answers.projectName);
|
|
253
555
|
const tempDir = join(tmpdir(), `cwa-${randomBytes(6).toString('hex')}`);
|
|
254
|
-
|
|
255
|
-
|
|
556
|
+
if (args.templateDir)
|
|
557
|
+
await copyTemplateDir(args.templateDir, tempDir, args.ci);
|
|
558
|
+
else
|
|
559
|
+
await downloadRepo(manifest, args.ref, tempDir, args.ci);
|
|
560
|
+
const s2 = progress(args.ci);
|
|
256
561
|
s2.start('Configuring project...');
|
|
257
562
|
try {
|
|
258
563
|
await removeExcluded(tempDir, manifest.alwaysExclude);
|
|
@@ -264,7 +569,8 @@ async function main() {
|
|
|
264
569
|
}
|
|
265
570
|
}
|
|
266
571
|
if (!answers.fixtures) {
|
|
267
|
-
|
|
572
|
+
// Sample content only: UsersFixture stays, so the admin is still created from ADMIN_* (#142).
|
|
573
|
+
await removeExcluded(tempDir, ['api/src/DataFixtures/AppScaffold.php', 'api/src/DataFixtures/Parts/']);
|
|
268
574
|
}
|
|
269
575
|
await processNuxtConfig(tempDir, answers.features);
|
|
270
576
|
await replaceProjectName(tempDir, answers.projectName);
|
|
@@ -283,61 +589,58 @@ async function main() {
|
|
|
283
589
|
' • waits for PostgreSQL to be ready',
|
|
284
590
|
' • runs database migrations',
|
|
285
591
|
'',
|
|
286
|
-
'The app container installs the Nuxt dependencies
|
|
287
|
-
'
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
: ''
|
|
592
|
+
'The app container installs the Nuxt dependencies into app/node_modules',
|
|
593
|
+
'(which your editor reads too) and runs the dev server.',
|
|
594
|
+
'',
|
|
595
|
+
'bin/up waits for all that, loads the fixtures (the admin' +
|
|
596
|
+
(answers.fixtures ? ' and sample pages' : '') +
|
|
597
|
+
'),',
|
|
598
|
+
'pre-compiles the first pages and prints where to sign in. Run it again any time.',
|
|
291
599
|
]
|
|
292
600
|
.join('\n')
|
|
293
601
|
.trim(), 'What Docker handles for you');
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
const ok = run('docker', ['compose', 'up', '-d'], targetDir);
|
|
301
|
-
if (!ok) {
|
|
302
|
-
log.warn('docker compose up -d failed. Run it manually from the project directory.');
|
|
303
|
-
}
|
|
602
|
+
if (setup.installDeps) {
|
|
603
|
+
log.step('Running: pnpm install (in app/)');
|
|
604
|
+
if (!run('pnpm', ['install'], join(targetDir, 'app'))) {
|
|
605
|
+
log.warn('pnpm install failed. Run it manually inside app/.');
|
|
606
|
+
if (args.ci)
|
|
607
|
+
process.exitCode = 1;
|
|
304
608
|
}
|
|
305
609
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
610
|
+
// bin/up is POSIX sh; Windows without sh (no WSL or Git Bash) gets the steps instead (#206).
|
|
611
|
+
const hasShell = process.platform !== 'win32' || commandExists('sh');
|
|
612
|
+
let ready = false;
|
|
613
|
+
if (setup.startDocker) {
|
|
614
|
+
if (hasShell) {
|
|
615
|
+
log.step('Running: bin/up');
|
|
616
|
+
ready = run('sh', ['bin/up'], targetDir);
|
|
617
|
+
if (!ready)
|
|
618
|
+
log.warn('bin/up failed (see above). Fix it, then run bin/up again in the project directory.');
|
|
619
|
+
// Under --ci a failed start fails the run (#215).
|
|
620
|
+
if (!ready && args.ci)
|
|
621
|
+
process.exitCode = 1;
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
log.step('Running: docker compose up -d --wait');
|
|
625
|
+
if (!run('docker', ['compose', 'up', '-d', '--wait'], targetDir)) {
|
|
626
|
+
log.warn('docker compose up -d --wait failed. Run it again in the project directory.');
|
|
627
|
+
if (args.ci)
|
|
628
|
+
process.exitCode = 1;
|
|
321
629
|
}
|
|
322
630
|
}
|
|
323
631
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if (answers.fixtures) {
|
|
337
|
-
outroLines.push('');
|
|
338
|
-
outroLines.push('Load sample content (once the API container is healthy):');
|
|
339
|
-
outroLines.push(' docker compose exec php bin/console doctrine:fixtures:load --append');
|
|
340
|
-
}
|
|
632
|
+
const outroLines = ready
|
|
633
|
+
? [`The site is ready: sign in with the login printed above. Next time: cd ${answers.projectName} && bin/up`]
|
|
634
|
+
: hasShell
|
|
635
|
+
? ['Start the stack, load the fixtures and get the login with:', ` cd ${answers.projectName} && bin/up`]
|
|
636
|
+
: [
|
|
637
|
+
'Start the stack and create the admin (and any sample pages) with:',
|
|
638
|
+
` cd ${answers.projectName}`,
|
|
639
|
+
' docker compose up -d --wait',
|
|
640
|
+
' docker compose exec php bin/console doctrine:fixtures:load --append',
|
|
641
|
+
'Then sign in at https://localhost/login as admin / admin (accept the certificate warning).',
|
|
642
|
+
];
|
|
643
|
+
outroLines.push('', 'Follow the dev server with: docker compose logs -f app', "Don't run pnpm install on this machine while the stack runs: app/node_modules", 'is shared with the dev server. If you do, run: docker compose restart app');
|
|
341
644
|
outro(outroLines.join('\n'));
|
|
342
645
|
}
|
|
343
646
|
main().catch((err) => {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const STACK_PORTS: {
|
|
2
|
+
port: number;
|
|
3
|
+
service: string;
|
|
4
|
+
culprits: string;
|
|
5
|
+
alternative?: string;
|
|
6
|
+
}[];
|
|
7
|
+
export interface PreflightResult {
|
|
8
|
+
dockerRunning: boolean;
|
|
9
|
+
takenPorts: string[];
|
|
10
|
+
memoryWarning: string | null;
|
|
11
|
+
}
|
|
12
|
+
export declare function preflight(): Promise<PreflightResult>;
|