@c9up/ream 0.2.25 → 0.2.27
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 +2 -0
- package/dist/Codemods.d.ts +12 -0
- package/dist/Codemods.d.ts.map +1 -1
- package/dist/Codemods.js +114 -23
- package/dist/Codemods.js.map +1 -1
- package/dist/HttpKernel.d.ts +5 -1
- package/dist/HttpKernel.d.ts.map +1 -1
- package/dist/HttpKernel.js +6 -6
- package/dist/HttpKernel.js.map +1 -1
- package/dist/Ignitor.d.ts.map +1 -1
- package/dist/Ignitor.js +87 -14
- package/dist/Ignitor.js.map +1 -1
- package/dist/console/Kernel.d.ts.map +1 -1
- package/dist/console/Kernel.js +20 -1
- package/dist/console/Kernel.js.map +1 -1
- package/dist/console/cliui.d.ts +9 -245
- package/dist/console/cliui.d.ts.map +1 -1
- package/dist/console/cliui.js +9 -583
- package/dist/console/cliui.js.map +1 -1
- package/dist/console/make/commands.d.ts +19 -0
- package/dist/console/make/commands.d.ts.map +1 -0
- package/dist/console/make/commands.js +252 -0
- package/dist/console/make/commands.js.map +1 -0
- package/dist/console/make/eject.d.ts +28 -0
- package/dist/console/make/eject.d.ts.map +1 -0
- package/dist/console/make/eject.js +161 -0
- package/dist/console/make/eject.js.map +1 -0
- package/dist/console/make/flush.d.ts +51 -0
- package/dist/console/make/flush.d.ts.map +1 -0
- package/dist/console/make/flush.js +93 -0
- package/dist/console/make/flush.js.map +1 -0
- package/dist/console/make/generators.d.ts +36 -0
- package/dist/console/make/generators.d.ts.map +1 -0
- package/dist/console/make/generators.js +148 -0
- package/dist/console/make/generators.js.map +1 -0
- package/dist/console/make/naming.d.ts +32 -0
- package/dist/console/make/naming.d.ts.map +1 -0
- package/dist/console/make/naming.js +82 -0
- package/dist/console/make/naming.js.map +1 -0
- package/dist/console/make/resolve.d.ts +46 -0
- package/dist/console/make/resolve.d.ts.map +1 -0
- package/dist/console/make/resolve.js +113 -0
- package/dist/console/make/resolve.js.map +1 -0
- package/dist/console/prompts.d.ts.map +1 -1
- package/dist/console/prompts.js +34 -46
- package/dist/console/prompts.js.map +1 -1
- package/dist/console/selection.d.ts +54 -0
- package/dist/console/selection.d.ts.map +1 -0
- package/dist/console/selection.js +235 -0
- package/dist/console/selection.js.map +1 -0
- package/dist/console/ui.d.ts +9 -15
- package/dist/console/ui.d.ts.map +1 -1
- package/dist/console/ui.js +11 -24
- package/dist/console/ui.js.map +1 -1
- package/dist/dev/fullReload.d.ts +35 -10
- package/dist/dev/fullReload.d.ts.map +1 -1
- package/dist/dev/fullReload.js +55 -14
- package/dist/dev/fullReload.js.map +1 -1
- package/dist/dev/hotWatcher.d.ts +19 -3
- package/dist/dev/hotWatcher.d.ts.map +1 -1
- package/dist/dev/hotWatcher.js +168 -18
- package/dist/dev/hotWatcher.js.map +1 -1
- package/dist/dev/readyBanner.d.ts +45 -0
- package/dist/dev/readyBanner.d.ts.map +1 -0
- package/dist/dev/readyBanner.js +72 -0
- package/dist/dev/readyBanner.js.map +1 -0
- package/dist/dev/shortcuts.d.ts +41 -0
- package/dist/dev/shortcuts.d.ts.map +1 -0
- package/dist/dev/shortcuts.js +142 -0
- package/dist/dev/shortcuts.js.map +1 -0
- package/dist/errors/prettyPrintError.d.ts.map +1 -1
- package/dist/errors/prettyPrintError.js +5 -8
- package/dist/errors/prettyPrintError.js.map +1 -1
- package/dist/errors/renderError.d.ts +46 -0
- package/dist/errors/renderError.d.ts.map +1 -0
- package/dist/errors/renderError.js +197 -0
- package/dist/errors/renderError.js.map +1 -0
- package/dist/hot.js +12 -2
- package/dist/hot.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/profiler/MemoryProfileStore.d.ts +23 -0
- package/dist/profiler/MemoryProfileStore.d.ts.map +1 -0
- package/dist/profiler/MemoryProfileStore.js +70 -0
- package/dist/profiler/MemoryProfileStore.js.map +1 -0
- package/dist/profiler/Profiler.d.ts +56 -0
- package/dist/profiler/Profiler.d.ts.map +1 -0
- package/dist/profiler/Profiler.js +162 -0
- package/dist/profiler/Profiler.js.map +1 -0
- package/dist/profiler/collectors.d.ts +105 -0
- package/dist/profiler/collectors.d.ts.map +1 -0
- package/dist/profiler/collectors.js +164 -0
- package/dist/profiler/collectors.js.map +1 -0
- package/dist/profiler/endpoint.d.ts +27 -0
- package/dist/profiler/endpoint.d.ts.map +1 -0
- package/dist/profiler/endpoint.js +79 -0
- package/dist/profiler/endpoint.js.map +1 -0
- package/dist/profiler/index.d.ts +26 -0
- package/dist/profiler/index.d.ts.map +1 -0
- package/dist/profiler/index.js +41 -0
- package/dist/profiler/index.js.map +1 -0
- package/dist/profiler/middleware.d.ts +17 -0
- package/dist/profiler/middleware.d.ts.map +1 -0
- package/dist/profiler/middleware.js +79 -0
- package/dist/profiler/middleware.js.map +1 -0
- package/dist/profiler/render.d.ts +23 -0
- package/dist/profiler/render.d.ts.map +1 -0
- package/dist/profiler/render.js +141 -0
- package/dist/profiler/render.js.map +1 -0
- package/dist/profiler/toolbar.d.ts +34 -0
- package/dist/profiler/toolbar.d.ts.map +1 -0
- package/dist/profiler/toolbar.js +104 -0
- package/dist/profiler/toolbar.js.map +1 -0
- package/dist/profiler/types.d.ts +70 -0
- package/dist/profiler/types.d.ts.map +1 -0
- package/dist/profiler/types.js +11 -0
- package/dist/profiler/types.js.map +1 -0
- package/dist/router/Router.d.ts +11 -0
- package/dist/router/Router.d.ts.map +1 -1
- package/dist/router/Router.js +16 -0
- package/dist/router/Router.js.map +1 -1
- package/dist/stubs/index.d.ts +11 -0
- package/dist/stubs/index.d.ts.map +1 -0
- package/dist/stubs/index.js +12 -0
- package/dist/stubs/index.js.map +1 -0
- package/dist/stubs/template.d.ts +63 -0
- package/dist/stubs/template.d.ts.map +1 -0
- package/dist/stubs/template.js +209 -0
- package/dist/stubs/template.js.map +1 -0
- package/events.win32-x64-msvc.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +7 -1
- package/scheduler.win32-x64-msvc.node +0 -0
- package/src/Codemods.ts +153 -29
- package/src/HttpKernel.ts +12 -8
- package/src/Ignitor.ts +95 -14
- package/src/console/Kernel.ts +21 -1
- package/src/console/cliui.ts +44 -754
- package/src/console/make/commands.ts +270 -0
- package/src/console/make/eject.ts +163 -0
- package/src/console/make/flush.ts +125 -0
- package/src/console/make/generators.ts +176 -0
- package/src/console/make/naming.ts +85 -0
- package/src/console/make/resolve.ts +134 -0
- package/src/console/prompts.ts +45 -64
- package/src/console/selection.ts +274 -0
- package/src/console/ui.ts +12 -24
- package/src/dev/fullReload.ts +57 -15
- package/src/dev/hotWatcher.ts +165 -21
- package/src/dev/readyBanner.ts +84 -0
- package/src/dev/shortcuts.ts +158 -0
- package/src/errors/prettyPrintError.ts +5 -5
- package/src/errors/renderError.ts +230 -0
- package/src/hot.ts +12 -2
- package/src/index.ts +23 -3
- package/src/profiler/MemoryProfileStore.ts +75 -0
- package/src/profiler/Profiler.ts +182 -0
- package/src/profiler/collectors.ts +230 -0
- package/src/profiler/endpoint.ts +98 -0
- package/src/profiler/index.ts +56 -0
- package/src/profiler/middleware.ts +83 -0
- package/src/profiler/render.ts +173 -0
- package/src/profiler/toolbar.ts +106 -0
- package/src/profiler/types.ts +75 -0
- package/src/router/Router.ts +16 -0
- package/src/stubs/index.ts +14 -0
- package/src/stubs/template.ts +262 -0
- package/stubs/make/command.stub +29 -0
- package/stubs/make/controller.stub +30 -0
- package/stubs/make/entity.stub +11 -0
- package/stubs/make/event.stub +13 -0
- package/stubs/make/listener.stub +13 -0
- package/stubs/make/middleware.stub +16 -0
- package/stubs/make/provider.stub +26 -0
- package/stubs/make/service.stub +27 -0
- package/stubs/make/validator.stub +10 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What each `make:` command generates.
|
|
3
|
+
*
|
|
4
|
+
* One table rather than ten near-identical commands: every generator is a
|
|
5
|
+
* name, a stub and the variables that stub reads. The differences that matter
|
|
6
|
+
* — which suffix a class carries, whether the name is snake_cased for the file
|
|
7
|
+
* — live here, in one place, instead of being spread across ten files that
|
|
8
|
+
* drift.
|
|
9
|
+
*
|
|
10
|
+
* The DESTINATION is not here. It is in the stub's own front matter, so an
|
|
11
|
+
* application that publishes `stubs/make/controller.stub` can move the file as
|
|
12
|
+
* well as change its contents.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { StubState } from '../../stubs/template.js'
|
|
16
|
+
import { ensureSuffix, stripSuffixInsensitive, toPascalCase, toSnakeCase } from './naming.js'
|
|
17
|
+
|
|
18
|
+
/** Extra input a generator takes beyond the name. */
|
|
19
|
+
export interface GeneratorInput {
|
|
20
|
+
/** `app/<module>/…` — where a module-scoped file lands. */
|
|
21
|
+
module?: string
|
|
22
|
+
/** `make:middleware --stack server|router|named`. */
|
|
23
|
+
stack?: string
|
|
24
|
+
/** `make:listener --event OrderShipped`. */
|
|
25
|
+
event?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Generator {
|
|
29
|
+
/** The stub, under `stubs/make/`. */
|
|
30
|
+
kind: string
|
|
31
|
+
commandName: string
|
|
32
|
+
description: string
|
|
33
|
+
/** Does it take `--module`? */
|
|
34
|
+
moduleScoped: boolean
|
|
35
|
+
/** The variables its stub reads. */
|
|
36
|
+
state(name: string, input: GeneratorInput): StubState
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The shared trio every stub may read. */
|
|
40
|
+
function base(name: string, className: string, module: string): StubState {
|
|
41
|
+
return { className, module, name }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const GENERATORS: readonly Generator[] = [
|
|
45
|
+
{
|
|
46
|
+
kind: 'controller',
|
|
47
|
+
commandName: 'make:controller',
|
|
48
|
+
description: 'Generate an HTTP controller',
|
|
49
|
+
moduleScoped: true,
|
|
50
|
+
state: (name, input) =>
|
|
51
|
+
base(name, ensureSuffix(name, 'Controller'), input.module ?? 'controllers'),
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
kind: 'service',
|
|
55
|
+
commandName: 'make:service',
|
|
56
|
+
description: 'Generate a service class',
|
|
57
|
+
moduleScoped: true,
|
|
58
|
+
state: (name, input) => base(name, ensureSuffix(name, 'Service'), input.module ?? 'services'),
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
kind: 'entity',
|
|
62
|
+
commandName: 'make:entity',
|
|
63
|
+
description: 'Generate an Atlas entity',
|
|
64
|
+
moduleScoped: true,
|
|
65
|
+
state: (name, input) => ({
|
|
66
|
+
// No suffix: an entity is `User`, not `UserEntity`.
|
|
67
|
+
...base(name, toPascalCase(name), input.module ?? 'entities'),
|
|
68
|
+
tableName: `${toSnakeCase(name)}s`,
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
kind: 'validator',
|
|
73
|
+
commandName: 'make:validator',
|
|
74
|
+
description: 'Generate a Rune validator',
|
|
75
|
+
moduleScoped: true,
|
|
76
|
+
state: (name, input) =>
|
|
77
|
+
base(name, ensureSuffix(name, 'Validator'), input.module ?? 'validators'),
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
kind: 'provider',
|
|
81
|
+
commandName: 'make:provider',
|
|
82
|
+
description: 'Generate a service provider',
|
|
83
|
+
moduleScoped: false,
|
|
84
|
+
state: (name) => base(name, ensureSuffix(name, 'Provider'), ''),
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
kind: 'command',
|
|
88
|
+
commandName: 'make:command',
|
|
89
|
+
description: 'Generate a console command',
|
|
90
|
+
moduleScoped: false,
|
|
91
|
+
state: (name) => {
|
|
92
|
+
// `app:provision` is the command NAME; the class and the file come from
|
|
93
|
+
// its last segment, or the published stub would generate a command
|
|
94
|
+
// unreachable under the name it was asked for.
|
|
95
|
+
const segments = name.split(':')
|
|
96
|
+
const last = segments[segments.length - 1] ?? name
|
|
97
|
+
return {
|
|
98
|
+
className: toPascalCase(toSnakeCase(last)),
|
|
99
|
+
fileName: toSnakeCase(last),
|
|
100
|
+
name,
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
kind: 'middleware',
|
|
106
|
+
commandName: 'make:middleware',
|
|
107
|
+
description: 'Generate an HTTP middleware',
|
|
108
|
+
moduleScoped: false,
|
|
109
|
+
state: (name, input) => {
|
|
110
|
+
const fileName = toSnakeCase(stripSuffixInsensitive(name, 'middleware'))
|
|
111
|
+
const stack = input.stack ?? 'router'
|
|
112
|
+
const importer = `() => import('#middleware/${fileName}_middleware.js')`
|
|
113
|
+
// The line the user pastes into their kernel — different per stack, and
|
|
114
|
+
// wrong in a way that is hard to spot if it is guessed.
|
|
115
|
+
const registration =
|
|
116
|
+
stack === 'server'
|
|
117
|
+
? `server.use([${importer}])`
|
|
118
|
+
: stack === 'named'
|
|
119
|
+
? `router.named({ ${fileName}: ${importer} })`
|
|
120
|
+
: `router.use([${importer}])`
|
|
121
|
+
return {
|
|
122
|
+
className: `${toPascalCase(fileName)}Middleware`,
|
|
123
|
+
fileName,
|
|
124
|
+
name,
|
|
125
|
+
registration,
|
|
126
|
+
stack,
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
kind: 'event',
|
|
132
|
+
commandName: 'make:event',
|
|
133
|
+
description: 'Generate an event class',
|
|
134
|
+
moduleScoped: false,
|
|
135
|
+
state: (name) => {
|
|
136
|
+
const fileName = toSnakeCase(name)
|
|
137
|
+
return { className: toPascalCase(fileName), fileName, name }
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
kind: 'listener',
|
|
142
|
+
commandName: 'make:listener',
|
|
143
|
+
description: 'Generate an event listener',
|
|
144
|
+
moduleScoped: false,
|
|
145
|
+
state: (name, input) => {
|
|
146
|
+
const fileName = toSnakeCase(name)
|
|
147
|
+
const className = toPascalCase(fileName)
|
|
148
|
+
if (input.event === undefined) {
|
|
149
|
+
// Without `--event` there is nothing to type the handler with, and
|
|
150
|
+
// inventing an import would point at a file that does not exist.
|
|
151
|
+
return {
|
|
152
|
+
className,
|
|
153
|
+
fileName,
|
|
154
|
+
name,
|
|
155
|
+
importLine: '',
|
|
156
|
+
eventType: 'unknown',
|
|
157
|
+
registration: `emitter.on(SomeEvent, ${className})`,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const eventFile = toSnakeCase(input.event)
|
|
161
|
+
const eventClass = toPascalCase(eventFile)
|
|
162
|
+
return {
|
|
163
|
+
className,
|
|
164
|
+
fileName,
|
|
165
|
+
name,
|
|
166
|
+
importLine: `import type ${eventClass} from '#app/events/${eventFile}.js'\n\n`,
|
|
167
|
+
eventType: eventClass,
|
|
168
|
+
registration: `emitter.on(${eventClass}, ${className})`,
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
export function generatorFor(kind: string): Generator | undefined {
|
|
175
|
+
return GENERATORS.find((generator) => generator.kind === kind)
|
|
176
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The naming conventions the generators follow.
|
|
3
|
+
*
|
|
4
|
+
* Ported from the Rust generator they replace, case for case: a rename here
|
|
5
|
+
* changes what `make:controller user` produces, so the rules are written out
|
|
6
|
+
* rather than approximated.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Add the suffix unless the name already carries it. */
|
|
10
|
+
export function ensureSuffix(name: string, suffix: string): string {
|
|
11
|
+
return name.endsWith(suffix) ? name : `${name}${suffix}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** `user_profile` / `user-profile` / `user profile` → `UserProfile`. */
|
|
15
|
+
export function toPascalCase(name: string): string {
|
|
16
|
+
return name
|
|
17
|
+
.split(/[_\-\s]/)
|
|
18
|
+
.filter((part) => part !== '')
|
|
19
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
20
|
+
.join('')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `UserProfile` → `user_profile`, `HTTPServer` → `http_server`.
|
|
25
|
+
*
|
|
26
|
+
* The acronym case is why this is not a one-line regex: an underscore goes
|
|
27
|
+
* before a capital that FOLLOWS a lowercase, or that follows a capital and is
|
|
28
|
+
* followed by a lowercase — which is where `HTTPServer` splits.
|
|
29
|
+
*/
|
|
30
|
+
export function toSnakeCase(name: string): string {
|
|
31
|
+
const chars = [...name]
|
|
32
|
+
let out = ''
|
|
33
|
+
for (const [index, char] of chars.entries()) {
|
|
34
|
+
if (char.toUpperCase() === char && char.toLowerCase() !== char) {
|
|
35
|
+
const previous = chars[index - 1]
|
|
36
|
+
const next = chars[index + 1]
|
|
37
|
+
const previousLower =
|
|
38
|
+
previous !== undefined &&
|
|
39
|
+
previous === previous.toLowerCase() &&
|
|
40
|
+
previous !== previous.toUpperCase()
|
|
41
|
+
const nextLower =
|
|
42
|
+
next !== undefined && next === next.toLowerCase() && next !== next.toUpperCase()
|
|
43
|
+
const previousUpper =
|
|
44
|
+
previous !== undefined &&
|
|
45
|
+
previous === previous.toUpperCase() &&
|
|
46
|
+
previous !== previous.toLowerCase()
|
|
47
|
+
if (index > 0 && (previousLower || (nextLower && previousUpper))) out += '_'
|
|
48
|
+
out += char.toLowerCase()
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
out += char
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Drop a trailing `Middleware` / `middleware` / `_middleware`, whatever the case. */
|
|
57
|
+
export function stripSuffixInsensitive(name: string, suffix: string): string {
|
|
58
|
+
const lower = name.toLowerCase()
|
|
59
|
+
const suffixLower = suffix.toLowerCase()
|
|
60
|
+
if (lower.length > suffixLower.length && lower.endsWith(suffixLower)) {
|
|
61
|
+
return name.slice(0, name.length - suffix.length).replace(/[_-]+$/, '')
|
|
62
|
+
}
|
|
63
|
+
return name
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Longer than this is not a name, it is a paste accident. */
|
|
67
|
+
export const MAX_NAME_LENGTH = 128
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Is this a name a generator may build a path from?
|
|
71
|
+
*
|
|
72
|
+
* Refused rather than sanitised: a name with a separator in it is either a
|
|
73
|
+
* mistake or an attempt to write outside the project, and quietly rewriting it
|
|
74
|
+
* would produce a file somewhere the user did not ask for.
|
|
75
|
+
*/
|
|
76
|
+
export function nameProblem(name: string): string | undefined {
|
|
77
|
+
if (name.trim() === '') return 'the name is empty'
|
|
78
|
+
if (name.length > MAX_NAME_LENGTH) return `the name is longer than ${MAX_NAME_LENGTH} characters`
|
|
79
|
+
if (/[/\\]/.test(name)) return 'a name may not contain a path separator'
|
|
80
|
+
if (name.includes('..')) return 'a name may not contain ".."'
|
|
81
|
+
if (!/^[A-Za-z][A-Za-z0-9_\-:]*$/.test(name)) {
|
|
82
|
+
return 'a name starts with a letter and holds letters, digits, "_", "-" or ":"'
|
|
83
|
+
}
|
|
84
|
+
return undefined
|
|
85
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding a stub, rendering it, and knowing where it goes.
|
|
3
|
+
*
|
|
4
|
+
* A stub is addressed the way `codemods.makeUsingStub` addresses one — a
|
|
5
|
+
* package's `stubsRoot` plus a path inside it, `make/controller.stub` — so a
|
|
6
|
+
* generator and a package's `configure()` hook read the same file through the
|
|
7
|
+
* same rule. The application's own copy under `stubs/` wins over the shipped
|
|
8
|
+
* one, which is what `ream eject` is for.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as fs from 'node:fs'
|
|
12
|
+
import * as path from 'node:path'
|
|
13
|
+
import { render, type StubState } from '../../stubs/template.js'
|
|
14
|
+
|
|
15
|
+
/** Where an application keeps its own copies — Adonis' `stubs/` root. */
|
|
16
|
+
export const PUBLISHED_ROOT = 'stubs'
|
|
17
|
+
|
|
18
|
+
/** The stubs this package ships. */
|
|
19
|
+
export const REAM_STUBS_ROOT = path.join(import.meta.dirname, '..', '..', '..', 'stubs')
|
|
20
|
+
|
|
21
|
+
export interface RenderedStub {
|
|
22
|
+
/** Relative to the project root, as the stub's front matter declares it. */
|
|
23
|
+
path: string
|
|
24
|
+
contents: string
|
|
25
|
+
/** Did the application's own copy win? */
|
|
26
|
+
published: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A stub path that could escape its root never reaches the filesystem.
|
|
31
|
+
*
|
|
32
|
+
* `eject` takes one from the command line, so this is the boundary: no
|
|
33
|
+
* absolute path, no `..`, and a `.stub` file at the end of it.
|
|
34
|
+
*/
|
|
35
|
+
export function isSafeStubPath(stubPath: string): boolean {
|
|
36
|
+
if (stubPath === '' || stubPath.length > 256) return false
|
|
37
|
+
if (path.isAbsolute(stubPath)) return false
|
|
38
|
+
const segments = stubPath.split(/[/\\]/)
|
|
39
|
+
if (segments.includes('..') || segments.includes('')) return false
|
|
40
|
+
return segments.every((segment) => /^[\w.-]+$/.test(segment))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where the application's copy of `stubPath` would live. */
|
|
44
|
+
export function publishedPath(stubPath: string, cwd: string): string | undefined {
|
|
45
|
+
if (!isSafeStubPath(stubPath)) return undefined
|
|
46
|
+
return path.join(cwd, PUBLISHED_ROOT, stubPath)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Read `stubPath` — the application's copy when there is one. */
|
|
50
|
+
export function readStub(
|
|
51
|
+
stubsRoot: string,
|
|
52
|
+
stubPath: string,
|
|
53
|
+
cwd: string,
|
|
54
|
+
): { source: string; published: boolean } {
|
|
55
|
+
const published = publishedPath(stubPath, cwd)
|
|
56
|
+
if (published !== undefined && fs.existsSync(published)) {
|
|
57
|
+
return { source: fs.readFileSync(published, 'utf8'), published: true }
|
|
58
|
+
}
|
|
59
|
+
if (!isSafeStubPath(stubPath)) {
|
|
60
|
+
throw new Error(`[make] unusable stub path: ${stubPath}`)
|
|
61
|
+
}
|
|
62
|
+
const shipped = path.join(stubsRoot, stubPath)
|
|
63
|
+
if (!fs.existsSync(shipped)) {
|
|
64
|
+
throw new Error(`[make] no stub at "${stubPath}" (looked in ${stubsRoot})`)
|
|
65
|
+
}
|
|
66
|
+
return { source: fs.readFileSync(shipped, 'utf8'), published: false }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Split `---\nto: …\n---\n<body>`; the front matter names the destination. */
|
|
70
|
+
export function splitFrontMatter(template: string): { to?: string; body: string } {
|
|
71
|
+
const trimmed = template.replace(/^[\r\n]+/, '')
|
|
72
|
+
if (!trimmed.startsWith('---')) return { body: template }
|
|
73
|
+
const after = trimmed.slice(3).replace(/^\r?\n/, '')
|
|
74
|
+
const end = after.indexOf('\n---')
|
|
75
|
+
if (end === -1) {
|
|
76
|
+
throw new Error('[make] a stub opened its front matter with `---` and never closed it')
|
|
77
|
+
}
|
|
78
|
+
const block = after.slice(0, end)
|
|
79
|
+
const body = after.slice(end + 4).replace(/^\r?\n/, '')
|
|
80
|
+
let to: string | undefined
|
|
81
|
+
for (const rawLine of block.split('\n')) {
|
|
82
|
+
const line = rawLine.trim()
|
|
83
|
+
if (line === '' || line.startsWith('#')) continue
|
|
84
|
+
const at = line.indexOf(':')
|
|
85
|
+
if (at === -1) throw new Error(`[make] unreadable stub front-matter line: \`${line}\``)
|
|
86
|
+
if (line.slice(0, at).trim() === 'to') to = line.slice(at + 1).trim()
|
|
87
|
+
}
|
|
88
|
+
return to === undefined ? { body } : { to, body }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The destination a stub declares, checked.
|
|
93
|
+
*
|
|
94
|
+
* Absolute paths and `..` are refused rather than normalised: a published stub
|
|
95
|
+
* is a file in the project, and a generator is not a way to write outside it.
|
|
96
|
+
*/
|
|
97
|
+
function assertInsideProject(destination: string): void {
|
|
98
|
+
if (destination === '') throw new Error('[make] the stub declares an empty destination')
|
|
99
|
+
if (path.isAbsolute(destination)) {
|
|
100
|
+
throw new Error(`[make] a stub may not write to an absolute path: ${destination}`)
|
|
101
|
+
}
|
|
102
|
+
if (destination.split(/[/\\]/).includes('..')) {
|
|
103
|
+
throw new Error(`[make] a stub may not write outside the project: ${destination}`)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function renderStub(
|
|
108
|
+
stubsRoot: string,
|
|
109
|
+
stubPath: string,
|
|
110
|
+
state: StubState,
|
|
111
|
+
cwd: string,
|
|
112
|
+
fallbackPath?: string,
|
|
113
|
+
): RenderedStub {
|
|
114
|
+
const { source, published } = readStub(stubsRoot, stubPath, cwd)
|
|
115
|
+
const { to, body } = splitFrontMatter(source)
|
|
116
|
+
const destination = to === undefined ? fallbackPath : render(to, state)
|
|
117
|
+
if (destination === undefined) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`[make] the stub at "${stubPath}" declares no destination — add a \`to:\` front-matter line.`,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
assertInsideProject(destination)
|
|
123
|
+
return { path: destination, contents: render(body, state), published }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `make/controller.stub` — the stub a generator of that kind reads. */
|
|
127
|
+
export function stubPathFor(kind: string): string {
|
|
128
|
+
return `make/${kind}.stub`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Render one of this package's own generator stubs. */
|
|
132
|
+
export function renderStubFor(kind: string, state: StubState, cwd: string): RenderedStub {
|
|
133
|
+
return renderStub(REAM_STUBS_ROOT, stubPathFor(kind), state, cwd)
|
|
134
|
+
}
|
package/src/console/prompts.ts
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { stdin, stdout } from 'node:process'
|
|
13
13
|
import { clearLine, createInterface, cursorTo } from 'node:readline'
|
|
14
14
|
import { ReamError } from '../errors/ReamError.js'
|
|
15
|
+
import { runSelection } from './selection.js'
|
|
15
16
|
import { colourise } from './ui.js'
|
|
16
17
|
|
|
17
18
|
/** A selectable option — a bare string, or a value with its display text. */
|
|
@@ -246,31 +247,18 @@ export class Prompt {
|
|
|
246
247
|
return this.#applyResult(picked[0], options)
|
|
247
248
|
}
|
|
248
249
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const first = matches[0]
|
|
262
|
-
if (matches.length === 1 && first !== undefined) {
|
|
263
|
-
return this.#finish(nameOf(first), options)
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const picked = await this.#select(
|
|
267
|
-
message,
|
|
268
|
-
matches.slice(0, limit),
|
|
269
|
-
{ ...options, name: undefined },
|
|
270
|
-
false,
|
|
271
|
-
)
|
|
272
|
-
return picked[0]
|
|
273
|
-
}
|
|
250
|
+
// One list that narrows as you type, rather than a filter question
|
|
251
|
+
// followed by a list: upstream's autocomplete is a single prompt, and
|
|
252
|
+
// answering twice for one value is a different thing wearing its name.
|
|
253
|
+
const picked = await this.#select(
|
|
254
|
+
message,
|
|
255
|
+
choices,
|
|
256
|
+
{ ...options, name: undefined },
|
|
257
|
+
false,
|
|
258
|
+
true,
|
|
259
|
+
options.limit ?? 10,
|
|
260
|
+
)
|
|
261
|
+
return picked[0]
|
|
274
262
|
}
|
|
275
263
|
|
|
276
264
|
// ─── internals ──────────────────────────────────────────────
|
|
@@ -299,6 +287,8 @@ export class Prompt {
|
|
|
299
287
|
choices: readonly PromptChoice[],
|
|
300
288
|
options: SelectPromptOptions | MultiplePromptOptions,
|
|
301
289
|
many: boolean,
|
|
290
|
+
filter?: boolean,
|
|
291
|
+
limit?: number,
|
|
302
292
|
): Promise<unknown[]> {
|
|
303
293
|
if (choices.length === 0) {
|
|
304
294
|
throw new ReamError('E_CONSOLE_EMPTY_CHOICES', `"${message}" was asked with no options.`)
|
|
@@ -313,50 +303,41 @@ export class Prompt {
|
|
|
313
303
|
return many ? picked : picked.map((value) => this.#applyResult(value, options))
|
|
314
304
|
}
|
|
315
305
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
? undefined
|
|
319
|
-
: [options.default]
|
|
320
|
-
.flat()
|
|
321
|
-
.map((index) => {
|
|
322
|
-
const choice = choices[index]
|
|
323
|
-
return choice === undefined ? String(index) : displayOf(choice)
|
|
324
|
-
})
|
|
325
|
-
.join(', ')
|
|
326
|
-
|
|
306
|
+
// Arrow keys, as upstream's prompts navigate. The list redraws in place
|
|
307
|
+
// until the user answers; a cancel is no answer, not an empty one.
|
|
327
308
|
for (;;) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
309
|
+
const outcome = await runSelection({
|
|
310
|
+
title: this.#label(message, { ...options, default: undefined }).trimEnd(),
|
|
311
|
+
items: choices.map((choice) => {
|
|
312
|
+
const hint = typeof choice === 'string' ? undefined : choice.hint
|
|
313
|
+
return hint === undefined
|
|
314
|
+
? { label: displayOf(choice) }
|
|
315
|
+
: { label: displayOf(choice), hint }
|
|
316
|
+
}),
|
|
317
|
+
many,
|
|
318
|
+
...(options.default === undefined
|
|
319
|
+
? {}
|
|
320
|
+
: {
|
|
321
|
+
selected: [options.default]
|
|
322
|
+
.flat()
|
|
323
|
+
.filter(
|
|
324
|
+
(index): index is number =>
|
|
325
|
+
Number.isInteger(index) && index >= 0 && index < choices.length,
|
|
326
|
+
),
|
|
327
|
+
}),
|
|
328
|
+
...(filter === true ? { filter: true } : {}),
|
|
329
|
+
...(limit === undefined ? {} : { limit }),
|
|
335
330
|
})
|
|
336
331
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
// An empty answer takes the default — which for a selection prompt is an
|
|
342
|
-
// index, as Adonis spells it. Without this the default was shown in the
|
|
343
|
-
// label and then rejected as an invalid selection.
|
|
344
|
-
const indexes =
|
|
345
|
-
answer === '' && options.default !== undefined
|
|
346
|
-
? [options.default]
|
|
347
|
-
.flat()
|
|
348
|
-
.filter((index) => Number.isInteger(index) && index >= 0 && index < choices.length)
|
|
349
|
-
: answer
|
|
350
|
-
.split(',')
|
|
351
|
-
.map((part) => Number(part.trim()) - 1)
|
|
352
|
-
.filter((index) => Number.isInteger(index) && index >= 0 && index < choices.length)
|
|
353
|
-
|
|
354
|
-
if (indexes.length === 0 || (!many && indexes.length !== 1)) {
|
|
355
|
-
stdout.write(`${colourise('Invalid selection.', 'yellow')}\n`)
|
|
356
|
-
continue
|
|
332
|
+
if (outcome.kind === 'cancelled') {
|
|
333
|
+
throw new ReamError('E_CONSOLE_PROMPT_CANCELLED', `"${message}" was cancelled.`, {
|
|
334
|
+
hint: 'Answer the prompt, or pass the value as a flag.',
|
|
335
|
+
})
|
|
357
336
|
}
|
|
358
337
|
|
|
359
|
-
|
|
338
|
+
// A multiple with nothing ticked is an answer of none, which is only
|
|
339
|
+
// valid if the command says so — `validate` is where that is decided.
|
|
340
|
+
const picked = outcome.indexes.map((index) => nameOf(choices[index] as PromptChoice))
|
|
360
341
|
const problem = await this.#selectionProblem(picked, options, many)
|
|
361
342
|
if (problem !== undefined) {
|
|
362
343
|
stdout.write(`${colourise(problem, 'yellow')}\n`)
|