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.
@@ -0,0 +1,125 @@
1
+ import { spawnSync } from 'node:child_process'
2
+ import { createConnection, createServer } from 'node:net'
3
+
4
+ // Ports the dev stack publishes: compose.yaml (80, 443) and compose.override.yaml (3001, 5432, 8025).
5
+ export const STACK_PORTS: { port: number; service: string; culprits: string; alternative?: string }[] = [
6
+ {
7
+ port: 80,
8
+ service: 'Caddy (php container), HTTP',
9
+ culprits:
10
+ 'another CWA project (`docker compose stop` in its directory), Laravel Valet (`valet stop`), Herd (quit it), nginx or Apache',
11
+ },
12
+ {
13
+ port: 443,
14
+ service: 'Caddy (php container), HTTPS',
15
+ culprits:
16
+ 'another CWA project (`docker compose stop` in its directory), Laravel Valet (`valet stop`), Herd (quit it), nginx or Apache',
17
+ },
18
+ {
19
+ port: 3001,
20
+ service: 'Nuxt dev server (app container)',
21
+ culprits: 'another CWA project (`docker compose stop` in its directory) or a Node dev server',
22
+ alternative: 'Or change `published: 3001` under `app:` in compose.override.yaml to a free port.',
23
+ },
24
+ {
25
+ port: 5432,
26
+ service: 'PostgreSQL (database container)',
27
+ culprits:
28
+ 'another CWA project (`docker compose stop` in its directory) or a local PostgreSQL (Postgres.app, `brew services stop postgresql`)',
29
+ alternative: 'Or change `published: 5432` under `database:` in compose.override.yaml to a free port.',
30
+ },
31
+ {
32
+ port: 8025,
33
+ service: 'Mailpit web UI (mailpit container)',
34
+ culprits: 'another CWA project (`docker compose stop` in its directory) or a local Mailpit or MailHog',
35
+ alternative: 'Or change `published: 8025` under `mailpit:` in compose.override.yaml to a free port.',
36
+ },
37
+ ]
38
+
39
+ // Docker Desktop reports a little less than the VM's allocation, so 4 GiB shows as ~3.8 GiB.
40
+ const MIN_DOCKER_MEMORY = 3.5 * 1024 ** 3
41
+
42
+ function canConnect(port: number): Promise<boolean> {
43
+ return new Promise((resolve) => {
44
+ const socket = createConnection({ host: '127.0.0.1', port })
45
+ const done = (result: boolean) => {
46
+ socket.destroy()
47
+ resolve(result)
48
+ }
49
+ socket.setTimeout(1000, () => done(false))
50
+ socket.once('connect', () => done(true))
51
+ socket.once('error', () => done(false))
52
+ })
53
+ }
54
+
55
+ function addressInUse(port: number): Promise<boolean> {
56
+ return new Promise((resolve) => {
57
+ const server = createServer()
58
+ server.once('error', (err: NodeJS.ErrnoException) => resolve(err.code === 'EADDRINUSE'))
59
+ server.once('listening', () => server.close(() => resolve(false)))
60
+ server.listen(port, '0.0.0.0')
61
+ })
62
+ }
63
+
64
+ function output(cmd: string, args: string[]): string | null {
65
+ const result = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })
66
+ return result.status === 0 ? result.stdout.trim() : null
67
+ }
68
+
69
+ // What holds the port: the containers Docker publishes it for, else the process lsof sees.
70
+ function portHolder(port: number): { holder: string; stop?: string } | null {
71
+ const containers = output('docker', [
72
+ 'ps',
73
+ '--filter',
74
+ `publish=${port}`,
75
+ '--format',
76
+ '{{.Names}}\t{{.Label "com.docker.compose.project"}}',
77
+ ])
78
+ if (containers) {
79
+ const rows = containers.split('\n').map((row) => row.split('\t'))
80
+ const names = rows.map(([name]) => name).join(', ')
81
+ const projects = [...new Set(rows.map(([, project]) => project).filter(Boolean))]
82
+ return projects.length
83
+ ? {
84
+ holder: `container ${names} (compose project ${projects.join(', ')})`,
85
+ stop: projects.map((p) => `docker compose -p ${p} stop`).join(' and '),
86
+ }
87
+ : { holder: `container ${names}`, stop: `docker stop ${rows.map(([name]) => name).join(' ')}` }
88
+ }
89
+ if (process.platform === 'win32') return null
90
+ const lsof = output('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fc'])
91
+ const command = lsof?.split('\n').find((l) => l.startsWith('c'))?.slice(1)
92
+ return command ? { holder: `process ${command}` } : null
93
+ }
94
+
95
+ export interface PreflightResult {
96
+ dockerRunning: boolean
97
+ takenPorts: string[]
98
+ memoryWarning: string | null
99
+ }
100
+
101
+ export async function preflight(): Promise<PreflightResult> {
102
+ const memTotal = output('docker', ['info', '--format', '{{.MemTotal}}'])
103
+ if (memTotal === null) {
104
+ return { dockerRunning: false, takenPorts: [], memoryWarning: null }
105
+ }
106
+
107
+ const takenPorts: string[] = []
108
+ for (const { port, service, culprits, alternative } of STACK_PORTS) {
109
+ if ((await canConnect(port)) || (await addressInUse(port))) {
110
+ const found = portHolder(port)
111
+ const lines = [`Port ${port} (${service}) is in use${found ? ` by ${found.holder}` : ''}.`]
112
+ lines.push(found?.stop ? ` Stop it with: ${found.stop}` : ` Free it. Likely: ${culprits}.`)
113
+ if (alternative) lines.push(` ${alternative}`)
114
+ takenPorts.push(lines.join('\n'))
115
+ }
116
+ }
117
+
118
+ const bytes = Number(memTotal)
119
+ const memoryWarning =
120
+ bytes > 0 && bytes < MIN_DOCKER_MEMORY
121
+ ? `Docker has ${(bytes / 1024 ** 3).toFixed(1)} GiB of memory; the stack needs about 4 GiB. Raise it in Docker Desktop → Settings → Resources.`
122
+ : null
123
+
124
+ return { dockerRunning: true, takenPorts, memoryWarning }
125
+ }
package/src/types.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export interface ManifestChoice {
2
2
  value: string
3
3
  label: string
4
+ hint?: string
4
5
  requires?: string[]
5
6
  }
6
7
 
package/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- dist/
2
- node_modules/