create-cwa 2.0.0-alpha.5 → 2.0.0-alpha.51
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
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { createConnection, createServer } from 'node:net';
|
|
3
|
+
// Ports the dev stack publishes: compose.yaml (80, 443) and compose.override.yaml (3001, 5432, 8025).
|
|
4
|
+
export const STACK_PORTS = [
|
|
5
|
+
{
|
|
6
|
+
port: 80,
|
|
7
|
+
service: 'Caddy (php container), HTTP',
|
|
8
|
+
culprits: 'another CWA project (`docker compose stop` in its directory), Laravel Valet (`valet stop`), Herd (quit it), nginx or Apache',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
port: 443,
|
|
12
|
+
service: 'Caddy (php container), HTTPS',
|
|
13
|
+
culprits: 'another CWA project (`docker compose stop` in its directory), Laravel Valet (`valet stop`), Herd (quit it), nginx or Apache',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
port: 3001,
|
|
17
|
+
service: 'Nuxt dev server (app container)',
|
|
18
|
+
culprits: 'another CWA project (`docker compose stop` in its directory) or a Node dev server',
|
|
19
|
+
alternative: 'Or change `published: 3001` under `app:` in compose.override.yaml to a free port.',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
port: 5432,
|
|
23
|
+
service: 'PostgreSQL (database container)',
|
|
24
|
+
culprits: 'another CWA project (`docker compose stop` in its directory) or a local PostgreSQL (Postgres.app, `brew services stop postgresql`)',
|
|
25
|
+
alternative: 'Or change `published: 5432` under `database:` in compose.override.yaml to a free port.',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
port: 8025,
|
|
29
|
+
service: 'Mailpit web UI (mailpit container)',
|
|
30
|
+
culprits: 'another CWA project (`docker compose stop` in its directory) or a local Mailpit or MailHog',
|
|
31
|
+
alternative: 'Or change `published: 8025` under `mailpit:` in compose.override.yaml to a free port.',
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
// Docker Desktop reports a little less than the VM's allocation, so 4 GiB shows as ~3.8 GiB.
|
|
35
|
+
const MIN_DOCKER_MEMORY = 3.5 * 1024 ** 3;
|
|
36
|
+
function canConnect(port) {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const socket = createConnection({ host: '127.0.0.1', port });
|
|
39
|
+
const done = (result) => {
|
|
40
|
+
socket.destroy();
|
|
41
|
+
resolve(result);
|
|
42
|
+
};
|
|
43
|
+
socket.setTimeout(1000, () => done(false));
|
|
44
|
+
socket.once('connect', () => done(true));
|
|
45
|
+
socket.once('error', () => done(false));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function addressInUse(port) {
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
const server = createServer();
|
|
51
|
+
server.once('error', (err) => resolve(err.code === 'EADDRINUSE'));
|
|
52
|
+
server.once('listening', () => server.close(() => resolve(false)));
|
|
53
|
+
server.listen(port, '0.0.0.0');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function output(cmd, args) {
|
|
57
|
+
const result = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' });
|
|
58
|
+
return result.status === 0 ? result.stdout.trim() : null;
|
|
59
|
+
}
|
|
60
|
+
// What holds the port: the containers Docker publishes it for, else the process lsof sees.
|
|
61
|
+
function portHolder(port) {
|
|
62
|
+
const containers = output('docker', [
|
|
63
|
+
'ps',
|
|
64
|
+
'--filter',
|
|
65
|
+
`publish=${port}`,
|
|
66
|
+
'--format',
|
|
67
|
+
'{{.Names}}\t{{.Label "com.docker.compose.project"}}',
|
|
68
|
+
]);
|
|
69
|
+
if (containers) {
|
|
70
|
+
const rows = containers.split('\n').map((row) => row.split('\t'));
|
|
71
|
+
const names = rows.map(([name]) => name).join(', ');
|
|
72
|
+
const projects = [...new Set(rows.map(([, project]) => project).filter(Boolean))];
|
|
73
|
+
return projects.length
|
|
74
|
+
? {
|
|
75
|
+
holder: `container ${names} (compose project ${projects.join(', ')})`,
|
|
76
|
+
stop: projects.map((p) => `docker compose -p ${p} stop`).join(' and '),
|
|
77
|
+
}
|
|
78
|
+
: { holder: `container ${names}`, stop: `docker stop ${rows.map(([name]) => name).join(' ')}` };
|
|
79
|
+
}
|
|
80
|
+
if (process.platform === 'win32')
|
|
81
|
+
return null;
|
|
82
|
+
const lsof = output('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fc']);
|
|
83
|
+
const command = lsof?.split('\n').find((l) => l.startsWith('c'))?.slice(1);
|
|
84
|
+
return command ? { holder: `process ${command}` } : null;
|
|
85
|
+
}
|
|
86
|
+
export async function preflight() {
|
|
87
|
+
const memTotal = output('docker', ['info', '--format', '{{.MemTotal}}']);
|
|
88
|
+
if (memTotal === null) {
|
|
89
|
+
return { dockerRunning: false, takenPorts: [], memoryWarning: null };
|
|
90
|
+
}
|
|
91
|
+
const takenPorts = [];
|
|
92
|
+
for (const { port, service, culprits, alternative } of STACK_PORTS) {
|
|
93
|
+
if ((await canConnect(port)) || (await addressInUse(port))) {
|
|
94
|
+
const found = portHolder(port);
|
|
95
|
+
const lines = [`Port ${port} (${service}) is in use${found ? ` by ${found.holder}` : ''}.`];
|
|
96
|
+
lines.push(found?.stop ? ` Stop it with: ${found.stop}` : ` Free it. Likely: ${culprits}.`);
|
|
97
|
+
if (alternative)
|
|
98
|
+
lines.push(` ${alternative}`);
|
|
99
|
+
takenPorts.push(lines.join('\n'));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const bytes = Number(memTotal);
|
|
103
|
+
const memoryWarning = bytes > 0 && bytes < MIN_DOCKER_MEMORY
|
|
104
|
+
? `Docker has ${(bytes / 1024 ** 3).toFixed(1)} GiB of memory; the stack needs about 4 GiB. Raise it in Docker Desktop → Settings → Resources.`
|
|
105
|
+
: null;
|
|
106
|
+
return { dockerRunning: true, takenPorts, memoryWarning };
|
|
107
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface ManifestChoice {
|
|
2
|
+
value: string;
|
|
3
|
+
label: string;
|
|
4
|
+
hint?: string;
|
|
5
|
+
requires?: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface ManifestQuestion {
|
|
8
|
+
id: string;
|
|
9
|
+
type: 'text' | 'select' | 'multiselect' | 'confirm';
|
|
10
|
+
message: string;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
hint?: string;
|
|
13
|
+
default?: boolean;
|
|
14
|
+
choices?: ManifestChoice[];
|
|
15
|
+
}
|
|
16
|
+
export interface ManifestFeature {
|
|
17
|
+
requires?: string[];
|
|
18
|
+
exclude: string[];
|
|
19
|
+
}
|
|
20
|
+
export interface ManifestCi {
|
|
21
|
+
exclude: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface Manifest {
|
|
24
|
+
version: string;
|
|
25
|
+
repo: string;
|
|
26
|
+
branch: string;
|
|
27
|
+
alwaysExclude: string[];
|
|
28
|
+
questions: ManifestQuestion[];
|
|
29
|
+
features: Record<string, ManifestFeature>;
|
|
30
|
+
ci: Record<string, ManifestCi>;
|
|
31
|
+
}
|
|
32
|
+
export interface Answers {
|
|
33
|
+
projectName: string;
|
|
34
|
+
ci: string;
|
|
35
|
+
features: string[];
|
|
36
|
+
fixtures: boolean;
|
|
37
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-cwa",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.51",
|
|
4
4
|
"description": "Create a new CWA (Components Web App) project",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"node": ">=22.13.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@clack/prompts": "^
|
|
27
|
+
"@clack/prompts": "^1.8.1",
|
|
28
28
|
"fast-glob": "^3.3.0",
|
|
29
|
-
"fs-extra": "^11.
|
|
30
|
-
"giget": "^
|
|
29
|
+
"fs-extra": "^11.4.1",
|
|
30
|
+
"giget": "^3.3.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/fs-extra": "^11.0.4",
|
|
34
|
-
"@types/node": "^20.
|
|
35
|
-
"tsx": "^4.
|
|
36
|
-
"typescript": "^
|
|
34
|
+
"@types/node": "^22.20.5",
|
|
35
|
+
"tsx": "^4.23.15",
|
|
36
|
+
"typescript": "^7.0.2"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc",
|
package/smoke.sh
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Template CI only (#215): creates a project from this checkout with the built CLI, starts it through bin/up and checks the login.
|
|
3
|
+
# Usage: packages/create-cwa/smoke.sh <work dir> (SMOKE_FEATURES: all|none|a,b; SMOKE_FIXTURES: true|false)
|
|
4
|
+
# Needs node and pnpm (or a built dist/), git, docker with compose, and free ports 80, 443, 3001, 5432 and 8025.
|
|
5
|
+
set -u
|
|
6
|
+
|
|
7
|
+
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
|
|
8
|
+
WORK=${1:?usage: smoke.sh <work dir>}
|
|
9
|
+
FEATURES=${SMOKE_FEATURES:-all}
|
|
10
|
+
FIXTURES=${SMOKE_FIXTURES:-true}
|
|
11
|
+
NAME=cwa-smoke
|
|
12
|
+
PROJECT="$WORK/$NAME"
|
|
13
|
+
# The images' first build, composer and pnpm installs on a CI runner take longer than a laptop's default 900 s.
|
|
14
|
+
export CWA_UP_TIMEOUT="${CWA_UP_TIMEOUT:-1800}"
|
|
15
|
+
|
|
16
|
+
fail() {
|
|
17
|
+
[ -z "${GITHUB_ACTIONS:-}" ] || echo "::error::create-cwa smoke: $*"
|
|
18
|
+
echo "create-cwa smoke failed: $*" >&2
|
|
19
|
+
[ "$(pwd)" != "$PROJECT" ] || docker compose logs --no-color --tail 80 php app >&2
|
|
20
|
+
exit 1
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
# A release commit names its own cwa-php-base before publish-php-base.yml has pushed it: skip, don't fail. GitLab shows exit 3 orange (allow_failure exit_codes).
|
|
24
|
+
base=$(sed -n 's/^ARG CWA_PHP_BASE=//p' "$ROOT/api/Dockerfile" | head -n 1)
|
|
25
|
+
if [ -n "$base" ] && ! out=$(docker buildx imagetools inspect "$base" 2>&1) && echo "$out" | grep -qi 'not found'; then
|
|
26
|
+
[ -z "${GITHUB_ACTIONS:-}" ] || echo "::warning::create-cwa smoke skipped: $base isn't published yet; re-run once publish-php-base.yml has pushed it"
|
|
27
|
+
echo "create-cwa smoke skipped: $base isn't published yet (a release commit before publish-php-base.yml). Re-run once it's pushed." >&2
|
|
28
|
+
[ -n "${GITLAB_CI:-}" ] && exit 3
|
|
29
|
+
exit 0
|
|
30
|
+
fi
|
|
31
|
+
|
|
32
|
+
if [ ! -f "$ROOT/packages/create-cwa/dist/index.js" ]; then
|
|
33
|
+
(cd "$ROOT/packages/create-cwa" && pnpm install --frozen-lockfile && pnpm run build) || fail "couldn't build the CLI"
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
[ ! -e "$PROJECT" ] || fail "$PROJECT already exists"
|
|
37
|
+
mkdir -p "$WORK" || exit 1
|
|
38
|
+
# CI checkouts can belong to another user than the one running git here (GitLab's dind job, GitHub containers).
|
|
39
|
+
if [ -n "${CI:-}" ]; then git config --global --add safe.directory "$ROOT"; fi
|
|
40
|
+
|
|
41
|
+
[ "$FIXTURES" = true ] && fixtures=--fixtures || fixtures=--no-fixtures
|
|
42
|
+
cd "$WORK" || exit 1
|
|
43
|
+
echo "==> create-cwa $NAME --ci --template-dir $ROOT --pipeline none --features $FEATURES $fixtures --start"
|
|
44
|
+
# The exit status goes through a file: POSIX sh has no pipefail.
|
|
45
|
+
{ node "$ROOT/packages/create-cwa/bin/create-cwa.js" "$NAME" --ci --template-dir "$ROOT" \
|
|
46
|
+
--pipeline none --features "$FEATURES" "$fixtures" --start 2>&1; echo $? > "$WORK/status"; } | tee "$WORK/create-cwa.log"
|
|
47
|
+
status=$(cat "$WORK/status")
|
|
48
|
+
[ "$status" = 0 ] || fail "create-cwa exited $status (bin/up's output is above)"
|
|
49
|
+
|
|
50
|
+
grep -q 'Sign in: https://localhost/login' "$WORK/create-cwa.log" || fail "bin/up didn't print the login URL"
|
|
51
|
+
grep -q 'Admin: admin / admin' "$WORK/create-cwa.log" || fail "bin/up didn't print the admin login"
|
|
52
|
+
|
|
53
|
+
cd "$PROJECT" || exit 1
|
|
54
|
+
# From inside php, as bin/up does: the published ports are on the Docker host, which isn't this machine under dind.
|
|
55
|
+
login=$(docker compose exec -T php curl -fsSk --max-time 60 --resolve localhost:443:127.0.0.1 https://localhost/login) \
|
|
56
|
+
|| fail "https://localhost/login didn't answer 200"
|
|
57
|
+
echo "$login" | grep -q 'type="password"' || fail "https://localhost/login has no password field"
|
|
58
|
+
|
|
59
|
+
code=$(docker compose exec -T php curl -sk --max-time 30 --resolve localhost:443:127.0.0.1 -o /dev/null -w '%{http_code}' \
|
|
60
|
+
-H 'Content-Type: application/json' -d '{"username":"admin","password":"admin"}' https://localhost/_api/login | tr -d '\r')
|
|
61
|
+
[ "$code" = 204 ] || fail "signing in as admin / admin answered $code, not 204"
|
|
62
|
+
|
|
63
|
+
echo "create-cwa smoke passed: features $FEATURES, fixtures $FIXTURES"
|