@stjoseph/prumo 0.5.1 → 0.6.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 (42) hide show
  1. package/.prumo-templates/api/auth.md +1 -1
  2. package/.prumo-templates/mcp/server.md +1 -1
  3. package/.prumo-templates/monorepo/layout.md +5 -1
  4. package/.prumo-templates/monorepo/tasks.md +5 -3
  5. package/.prumo-templates/social/sign-in.md +2 -1
  6. package/README.md +3 -1
  7. package/dist/add.js +219 -0
  8. package/dist/apps.js +205 -0
  9. package/dist/asker.js +37 -0
  10. package/dist/clean.js +3 -3
  11. package/dist/cli.js +51 -2
  12. package/dist/commands.js +10 -0
  13. package/dist/compose.js +86 -28
  14. package/dist/context.js +32 -5
  15. package/dist/database.js +3 -4
  16. package/dist/generate.js +26 -24
  17. package/dist/names.js +2 -1
  18. package/dist/next-steps.js +4 -3
  19. package/dist/questions.js +22 -3
  20. package/dist/workspace-yaml.js +91 -0
  21. package/package.json +1 -1
  22. package/templates/api/.env.example +1 -0
  23. package/templates/api/README.md +2 -1
  24. package/templates/api/scripts/ports.mjs +60 -45
  25. package/templates/api/src/infra/config/env.ts +8 -1
  26. package/templates/api/src/infra/http/controllers/oauth.controller.ts +2 -2
  27. package/templates/api/src/infra/http/controllers/social.controller.ts +29 -13
  28. package/templates/api/src/infra/http/hooks/origin.hook.ts +4 -4
  29. package/templates/api/src/infra/social/openid-identity-providers.adapter.ts +1 -1
  30. package/templates/api/test/infra/config/env.spec.ts +26 -0
  31. package/templates/api/test/infra/http/controllers/auth.controller.spec.ts +24 -0
  32. package/templates/api/test/infra/http/controllers/social.controller.spec.ts +23 -1
  33. package/templates/api/test/support/setup.ts +1 -1
  34. package/templates/mobile/scripts/ports.mjs +60 -45
  35. package/templates/mobile/src/api-contract/auth.ts +15 -3
  36. package/templates/site/scripts/ports.mjs +60 -45
  37. package/templates/web/scripts/ports.mjs +60 -45
  38. package/templates/web/src/api-contract/auth.ts +15 -3
  39. package/templates/web/src/features/auth/social-buttons.tsx +2 -2
  40. package/templates/web/test/features/auth/social-buttons.spec.tsx +1 -0
  41. package/templates/workspace/README.md +2 -2
  42. package/templates/workspace/scripts/ports.mjs +60 -45
@@ -14,22 +14,23 @@ import { basename, dirname, join, resolve } from 'node:path'
14
14
  import { createInterface } from 'node:readline/promises'
15
15
  import { parseArgs } from 'node:util'
16
16
 
17
- // Where each app keeps its port, what it falls back to, and how its dev command is told about it.
18
- const APPS = {
17
+ // Where an app of each type keeps its port, what it falls back to, and how its dev command is told about it.
18
+ const TYPES = {
19
19
  api: { key: 'PORT', fallback: 3000, flag: [] },
20
20
  web: { key: 'WEB_PORT', fallback: 5173, flag: ['--port', '--strictPort'] },
21
21
  site: { key: 'SITE_PORT', fallback: 3200, flag: ['--port'] },
22
22
  mobile: { key: 'METRO_PORT', fallback: 8081, flag: ['--port'] },
23
23
  }
24
24
 
25
- // A port that moves takes these with it: the key to write, in which app, from which app's port.
25
+ // A port that moves takes these with it: the key to write, in every app of which type. A list holds one URL per app,
26
+ // so only the moving app's entry changes.
26
27
  const FOLLOWERS = {
27
28
  api: [
28
- { app: 'api', key: 'API_URL' },
29
- { app: 'web', key: 'VITE_API_URL' },
30
- { app: 'mobile', key: 'EXPO_PUBLIC_API_URL' },
29
+ { type: 'api', key: 'API_URL' },
30
+ { type: 'web', key: 'VITE_API_URL' },
31
+ { type: 'mobile', key: 'EXPO_PUBLIC_API_URL' },
31
32
  ],
32
- web: [{ app: 'api', key: 'WEB_ORIGIN' }],
33
+ web: [{ type: 'api', key: 'WEB_ORIGIN', list: true }],
33
34
  site: [],
34
35
  mobile: [],
35
36
  }
@@ -117,36 +118,43 @@ function projectRoot() {
117
118
  const root = projectRoot()
118
119
  const workspace = existsSync(join(root, 'apps'))
119
120
 
120
- function directoryOf(type) {
121
- return workspace ? join(root, 'apps', type) : root
121
+ function readConfig() {
122
+ const path = join(root, '.prumo', 'config.json')
123
+
124
+ return existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : {}
122
125
  }
123
126
 
124
- function typeOf(directory) {
125
- const name = basename(directory)
127
+ // Every app of the project, as { name, type, directory }. A workspace lists its apps in .prumo/config.json; one written
128
+ // before apps had names holds one per type, named after it. An alone project is its one app, named after its type.
129
+ function projectApps() {
130
+ const config = readConfig()
131
+
132
+ if (!workspace) {
133
+ // A project is named by its author, so only .prumo/config.json says what it is.
134
+ const type = TYPES[basename(root)] !== undefined ? basename(root) : config.types?.[0]
126
135
 
127
- if (APPS[name] !== undefined) {
128
- return name
136
+ return type === undefined ? [] : [{ name: type, type, directory: root }]
129
137
  }
130
138
 
131
- // An alone project is named by its author, so only .prumo/config.json says what it is.
132
- const config = join(root, '.prumo', 'config.json')
133
- const types = existsSync(config) ? JSON.parse(readFileSync(config, 'utf8')).types : []
139
+ const listed = config.apps ?? (config.types ?? []).map((type) => ({ name: type, type }))
134
140
 
135
- return types[0]
141
+ return listed
142
+ .map((app) => ({ ...app, directory: join(root, 'apps', app.name) }))
143
+ .filter(
144
+ (app) => TYPES[app.type] !== undefined && existsSync(join(app.directory, 'package.json')),
145
+ )
136
146
  }
137
147
 
138
- function typesToCheck() {
139
- if (!values.all) {
140
- return [typeOf(here)]
141
- }
148
+ const apps = projectApps()
142
149
 
143
- return Object.keys(APPS).filter((type) => existsSync(join(directoryOf(type), 'package.json')))
150
+ function appsToCheck() {
151
+ return values.all ? apps : apps.filter((app) => app.directory === here)
144
152
  }
145
153
 
146
- function portOf(type) {
147
- const app = APPS[type]
154
+ function portOf(app) {
155
+ const { key, fallback } = TYPES[app.type]
148
156
 
149
- return Number(envValue(readEnv(directoryOf(type)), app.key) ?? app.fallback)
157
+ return Number(envValue(readEnv(app.directory), key) ?? fallback)
150
158
  }
151
159
 
152
160
  function listening(host, port) {
@@ -269,22 +277,31 @@ async function nextFree(from) {
269
277
  throw new Failure('port_busy', `No free port between ${from + 1} and ${from + PORTS_TRIED - 1}.`)
270
278
  }
271
279
 
272
- function movePort(type, port) {
273
- const app = APPS[type]
280
+ function movePort(app, from, port) {
281
+ const before = `http://localhost:${from}`
282
+ const after = `http://localhost:${port}`
274
283
 
275
- writeEnvValue(directoryOf(type), app.key, port)
284
+ writeEnvValue(app.directory, TYPES[app.type].key, port)
276
285
 
277
- for (const follower of FOLLOWERS[type]) {
278
- const directory = directoryOf(follower.app)
286
+ for (const follower of FOLLOWERS[app.type]) {
287
+ for (const other of apps.filter((each) => each.type === follower.type)) {
288
+ const current = envValue(readEnv(other.directory), follower.key)
289
+ const entries = (current ?? '').split(',').map((entry) => entry.trim())
290
+ const value =
291
+ follower.list && current !== undefined
292
+ ? (entries.includes(before)
293
+ ? entries.map((entry) => (entry === before ? after : entry))
294
+ : [...entries, after]
295
+ ).join(',')
296
+ : after
279
297
 
280
- if (existsSync(join(directory, 'package.json'))) {
281
- writeEnvValue(directory, follower.key, `http://localhost:${port}`)
298
+ writeEnvValue(other.directory, follower.key, value)
282
299
  }
283
300
  }
284
301
  }
285
302
 
286
- async function settle(type) {
287
- const port = portOf(type)
303
+ async function settle(app) {
304
+ const port = portOf(app)
288
305
 
289
306
  if (!(await taken(port))) {
290
307
  return port
@@ -292,12 +309,12 @@ async function settle(type) {
292
309
 
293
310
  const found = holders(port)
294
311
 
295
- say(`${type} wants port ${port}, held by ${describe(found)}.`)
312
+ say(`${app.name} wants port ${port}, held by ${describe(found)}.`)
296
313
 
297
314
  const moved = async () => {
298
315
  const free = await nextFree(port)
299
- movePort(type, free)
300
- say(`${type} moved to port ${free}, written to .env.`)
316
+ movePort(app, port, free)
317
+ say(`${app.name} moved to port ${free}, written to .env.`)
301
318
  return free
302
319
  }
303
320
 
@@ -311,7 +328,7 @@ async function settle(type) {
311
328
  if (!interactive) {
312
329
  throw new Failure(
313
330
  'port_busy',
314
- `Port ${port} is in use by ${describe(found)}. Pass --kill to stop it, or --change to move ${type} to the next free port.`,
331
+ `Port ${port} is in use by ${describe(found)}. Pass --kill to stop it, or --change to move ${app.name} to the next free port.`,
315
332
  )
316
333
  }
317
334
 
@@ -669,10 +686,8 @@ async function main() {
669
686
  const starting = command === undefined ? undefined : startJob()
670
687
 
671
688
  try {
672
- for (const type of typesToCheck()) {
673
- if (type !== undefined) {
674
- ports[type] = await settle(type)
675
- }
689
+ for (const app of appsToCheck()) {
690
+ ports[app.name] = await settle(app)
676
691
  }
677
692
  } catch (error) {
678
693
  ;(await starting)?.close()
@@ -685,10 +700,10 @@ async function main() {
685
700
 
686
701
  // Only an app tells its command which port to use. A workspace root wraps `pnpm`, which would pass a stray
687
702
  // `--port` on to every app it runs.
688
- const [type] = values.all ? [] : Object.keys(ports)
689
- const flag = APPS[type ?? '']?.flag ?? []
703
+ const [app] = values.all ? [] : appsToCheck()
704
+ const flag = app === undefined ? [] : TYPES[app.type].flag
690
705
  const withPort = flag.flatMap((entry) =>
691
- entry === '--port' ? ['--port', String(ports[type])] : [entry],
706
+ entry === '--port' ? ['--port', String(ports[app.name])] : [entry],
692
707
  )
693
708
  // Before the command starts, so that it and everything it starts are members from their first instant.
694
709
  const job = await starting