@adonisjs/assembler 6.1.3-5 → 6.1.3-7

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.
@@ -1,338 +0,0 @@
1
- /*
2
- * @adonisjs/assembler
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- import picomatch from 'picomatch'
11
- import type tsStatic from 'typescript'
12
- import { type ExecaChildProcess } from 'execa'
13
- import { cliui, type Logger } from '@poppinss/cliui'
14
- import type { Watcher } from '@poppinss/chokidar-ts'
15
-
16
- import type { TestRunnerOptions } from './types.js'
17
- import { AssetsDevServer } from './assets_dev_server.js'
18
- import { getPort, isDotEnvFile, runNode, watch } from './helpers.js'
19
-
20
- /**
21
- * Instance of CLIUI
22
- */
23
- const ui = cliui()
24
-
25
- /**
26
- * Exposes the API to start the development. Optionally, the watch API can be
27
- * used to watch for file changes and restart the development server.
28
- *
29
- * The Dev server performs the following actions
30
- *
31
- * - Assigns a random PORT, when PORT inside .env file is in use
32
- * - Uses tsconfig.json file to collect a list of files to watch.
33
- * - Uses metaFiles from .adonisrc.json file to collect a list of files to watch.
34
- * - Restart HTTP server on every file change.
35
- */
36
- export class TestRunner {
37
- #cwd: URL
38
- #logger = ui.logger
39
- #options: TestRunnerOptions
40
- #scriptFile: string = 'bin/test.js'
41
- #isMetaFile: picomatch.Matcher
42
- #isTestFile: picomatch.Matcher
43
-
44
- /**
45
- * In watch mode, after a file is changed, we wait for the current
46
- * set of tests to finish before triggering a re-run. Therefore,
47
- * we use this flag to know if we are already busy in running
48
- * tests and ignore file-changes.
49
- */
50
- #isBusy: boolean = false
51
-
52
- #onError?: (error: any) => any
53
- #onClose?: (exitCode: number) => any
54
-
55
- #testScript?: ExecaChildProcess<string>
56
- #watcher?: ReturnType<Watcher['watch']>
57
- #assetsServer?: AssetsDevServer
58
-
59
- /**
60
- * Getting reference to colors library from logger
61
- */
62
- get #colors() {
63
- return this.#logger.getColors()
64
- }
65
-
66
- constructor(cwd: URL, options: TestRunnerOptions) {
67
- this.#cwd = cwd
68
- this.#options = options
69
- this.#isMetaFile = picomatch((this.#options.metaFiles || []).map(({ pattern }) => pattern))
70
- this.#isTestFile = picomatch(
71
- this.#options.suites
72
- .filter((suite) => {
73
- if (this.#options.filters.suites) {
74
- this.#options.filters.suites.includes(suite.name)
75
- }
76
-
77
- return true
78
- })
79
- .map((suite) => suite.files)
80
- .flat(1)
81
- )
82
- }
83
-
84
- /**
85
- * Converts all known filters to CLI args.
86
- *
87
- * The following code snippet may seem like repetitive code. But, it
88
- * is done intentionally to have visibility around how each filter
89
- * is converted to an arg.
90
- */
91
- #convertFiltersToArgs(filters: TestRunnerOptions['filters']): string[] {
92
- const args: string[] = []
93
-
94
- if (filters.suites) {
95
- args.push(...filters.suites)
96
- }
97
-
98
- if (filters.files) {
99
- args.push('--files')
100
- args.push(filters.files.join(','))
101
- }
102
-
103
- if (filters.groups) {
104
- args.push('--groups')
105
- args.push(filters.groups.join(','))
106
- }
107
-
108
- if (filters.tags) {
109
- args.push('--tags')
110
- args.push(filters.tags.join(','))
111
- }
112
-
113
- if (filters.ignoreTags) {
114
- args.push('--ignore-tags')
115
- args.push(filters.ignoreTags.join(','))
116
- }
117
-
118
- if (filters.tests) {
119
- args.push('--ignore-tests')
120
- args.push(filters.tests.join(','))
121
- }
122
-
123
- if (filters.match) {
124
- args.push('--match')
125
- args.push(filters.match.join(','))
126
- }
127
-
128
- return args
129
- }
130
-
131
- /**
132
- * Conditionally clear the terminal screen
133
- */
134
- #clearScreen() {
135
- if (this.#options.clearScreen) {
136
- process.stdout.write('\u001Bc')
137
- }
138
- }
139
-
140
- /**
141
- * Runs tests
142
- */
143
- #runTests(port: string, filtersArgs: string[], mode: 'blocking' | 'nonblocking') {
144
- this.#isBusy = true
145
-
146
- this.#testScript = runNode(this.#cwd, {
147
- script: this.#scriptFile,
148
- env: { PORT: port, ...this.#options.env },
149
- nodeArgs: this.#options.nodeArgs,
150
- scriptArgs: filtersArgs.concat(this.#options.scriptArgs),
151
- })
152
-
153
- this.#testScript
154
- .then((result) => {
155
- if (mode === 'nonblocking') {
156
- this.#onClose?.(result.exitCode)
157
- this.#watcher?.close()
158
- this.#assetsServer?.stop()
159
- }
160
- })
161
- .catch((error) => {
162
- this.#logger.warning(`unable to run test script "${this.#scriptFile}"`)
163
- this.#logger.fatal(error)
164
- this.#onError?.(error)
165
- this.#watcher?.close()
166
- this.#assetsServer?.stop()
167
- })
168
- .finally(() => {
169
- this.#isBusy = false
170
- })
171
- }
172
-
173
- /**
174
- * Starts the assets server
175
- */
176
- #startAssetsServer() {
177
- this.#assetsServer = new AssetsDevServer(this.#cwd, this.#options.assets)
178
- this.#assetsServer.start()
179
- }
180
-
181
- /**
182
- * Handles a non TypeScript file change
183
- */
184
- #handleFileChange(action: string, port: string, filters: string[], relativePath: string) {
185
- if (this.#isBusy) {
186
- return
187
- }
188
-
189
- if (isDotEnvFile(relativePath) || this.#isMetaFile(relativePath)) {
190
- this.#clearScreen()
191
- this.#logger.log(`${this.#colors.green(action)} ${relativePath}`)
192
- this.#runTests(port, filters, 'blocking')
193
- }
194
- }
195
-
196
- /**
197
- * Handles TypeScript source file change
198
- */
199
- #handleSourceFileChange(action: string, port: string, filters: string[], relativePath: string) {
200
- if (this.#isBusy) {
201
- return
202
- }
203
-
204
- this.#clearScreen()
205
- this.#logger.log(`${this.#colors.green(action)} ${relativePath}`)
206
-
207
- /**
208
- * If changed file is a test file after considering the initial filters,
209
- * then only run that file
210
- */
211
- if (this.#isTestFile(relativePath)) {
212
- this.#runTests(
213
- port,
214
- this.#convertFiltersToArgs({
215
- ...this.#options.filters,
216
- files: [relativePath],
217
- }),
218
- 'blocking'
219
- )
220
- return
221
- }
222
-
223
- this.#runTests(port, filters, 'blocking')
224
- }
225
-
226
- /**
227
- * Set a custom CLI UI logger
228
- */
229
- setLogger(logger: Logger) {
230
- this.#logger = logger
231
- this.#assetsServer?.setLogger(logger)
232
- return this
233
- }
234
-
235
- /**
236
- * Add listener to get notified when dev server is
237
- * closed
238
- */
239
- onClose(callback: (exitCode: number) => any): this {
240
- this.#onClose = callback
241
- return this
242
- }
243
-
244
- /**
245
- * Add listener to get notified when dev server exists
246
- * with an error
247
- */
248
- onError(callback: (error: any) => any): this {
249
- this.#onError = callback
250
- return this
251
- }
252
-
253
- /**
254
- * Runs tests
255
- */
256
- async run() {
257
- const port = String(await getPort(this.#cwd))
258
- const initialFilters = this.#convertFiltersToArgs(this.#options.filters)
259
-
260
- this.#clearScreen()
261
- this.#startAssetsServer()
262
-
263
- this.#logger.info('booting application to run tests...')
264
- this.#runTests(port, initialFilters, 'nonblocking')
265
- }
266
-
267
- /**
268
- * Run tests in watch mode
269
- */
270
- async runAndWatch(ts: typeof tsStatic, options?: { poll: boolean }) {
271
- const port = String(await getPort(this.#cwd))
272
- const initialFilters = this.#convertFiltersToArgs(this.#options.filters)
273
-
274
- this.#clearScreen()
275
- this.#startAssetsServer()
276
-
277
- this.#logger.info('booting application to run tests...')
278
- this.#runTests(port, initialFilters, 'blocking')
279
-
280
- /**
281
- * Create watcher using tsconfig.json file
282
- */
283
- const output = watch(this.#cwd, ts, options || {})
284
- if (!output) {
285
- this.#onClose?.(1)
286
- return
287
- }
288
-
289
- /**
290
- * Storing reference to watcher, so that we can close it
291
- * when HTTP server exists with error
292
- */
293
- this.#watcher = output.chokidar
294
-
295
- /**
296
- * Notify the watcher is ready
297
- */
298
- output.watcher.on('watcher:ready', () => {
299
- this.#logger.info('watching file system for changes...')
300
- })
301
-
302
- /**
303
- * Cleanup when watcher dies
304
- */
305
- output.chokidar.on('error', (error) => {
306
- this.#logger.warning('file system watcher failure')
307
- this.#logger.fatal(error)
308
- this.#onError?.(error)
309
- output.chokidar.close()
310
- })
311
-
312
- /**
313
- * Changes in TypeScript source file
314
- */
315
- output.watcher.on('source:add', ({ relativePath }) =>
316
- this.#handleSourceFileChange('add', port, initialFilters, relativePath)
317
- )
318
- output.watcher.on('source:change', ({ relativePath }) =>
319
- this.#handleSourceFileChange('update', port, initialFilters, relativePath)
320
- )
321
- output.watcher.on('source:unlink', ({ relativePath }) =>
322
- this.#handleSourceFileChange('delete', port, initialFilters, relativePath)
323
- )
324
-
325
- /**
326
- * Changes in non-TypeScript source files
327
- */
328
- output.watcher.on('add', ({ relativePath }) =>
329
- this.#handleFileChange('add', port, initialFilters, relativePath)
330
- )
331
- output.watcher.on('change', ({ relativePath }) =>
332
- this.#handleFileChange('update', port, initialFilters, relativePath)
333
- )
334
- output.watcher.on('unlink', ({ relativePath }) =>
335
- this.#handleFileChange('delete', port, initialFilters, relativePath)
336
- )
337
- }
338
- }
package/src/types.ts DELETED
@@ -1,110 +0,0 @@
1
- /*
2
- * @adonisjs/assembler
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- /**
11
- * Options needed to run a script file
12
- */
13
- export type RunOptions = {
14
- script: string
15
- scriptArgs: string[]
16
- nodeArgs: string[]
17
- stdio?: 'pipe' | 'inherit'
18
- env?: NodeJS.ProcessEnv
19
- }
20
-
21
- /**
22
- * Watcher options
23
- */
24
- export type WatchOptions = {
25
- poll?: boolean
26
- }
27
-
28
- /**
29
- * Meta file config defined in ".adonisrc.json" file
30
- */
31
- export type MetaFile = {
32
- pattern: string
33
- reloadServer: boolean
34
- }
35
-
36
- /**
37
- * Test suite defined in ".adonisrc.json" file
38
- */
39
- export type Suite = {
40
- files: string[]
41
- name: string
42
- }
43
-
44
- /**
45
- * Options accepted by assets bundler
46
- */
47
- export type AssetsBundlerOptions =
48
- | {
49
- serve: false
50
- args?: string[]
51
- driver?: string
52
- cmd?: string
53
- }
54
- | {
55
- serve: true
56
- args: string[]
57
- driver: string
58
- cmd: string
59
- }
60
-
61
- /**
62
- * Options accepted by the dev server
63
- */
64
- export type DevServerOptions = {
65
- scriptArgs: string[]
66
- nodeArgs: string[]
67
- clearScreen?: boolean
68
- env?: NodeJS.ProcessEnv
69
- metaFiles?: MetaFile[]
70
- assets?: AssetsBundlerOptions
71
- }
72
-
73
- /**
74
- * Options accepted by the test runner
75
- */
76
- export type TestRunnerOptions = {
77
- /**
78
- * Filter arguments are provided as a key-value
79
- * pair, so that we can mutate them (if needed)
80
- */
81
- filters: Partial<{
82
- tests: string[]
83
- suites: string[]
84
- groups: string[]
85
- files: string[]
86
- match: string[]
87
- tags: string[]
88
- ignoreTags: string[]
89
- }>
90
-
91
- /**
92
- * All other tags are provided as a collection of
93
- * arguments
94
- */
95
- scriptArgs: string[]
96
- nodeArgs: string[]
97
- clearScreen?: boolean
98
- env?: NodeJS.ProcessEnv
99
- metaFiles?: MetaFile[]
100
- assets?: AssetsBundlerOptions
101
- suites: Suite[]
102
- }
103
-
104
- /**
105
- * Options accepted by the project bundler
106
- */
107
- export type BundlerOptions = {
108
- metaFiles?: MetaFile[]
109
- assets?: AssetsBundlerOptions
110
- }