@adonisjs/assembler 6.1.3-3 → 6.1.3-5

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,178 @@
1
+ import picomatch from 'picomatch';
2
+ import { cliui } from '@poppinss/cliui';
3
+ import { AssetsDevServer } from './assets_dev_server.js';
4
+ import { getPort, isDotEnvFile, runNode, watch } from './helpers.js';
5
+ const ui = cliui();
6
+ export class TestRunner {
7
+ #cwd;
8
+ #logger = ui.logger;
9
+ #options;
10
+ #scriptFile = 'bin/test.js';
11
+ #isMetaFile;
12
+ #isTestFile;
13
+ #isBusy = false;
14
+ #onError;
15
+ #onClose;
16
+ #testScript;
17
+ #watcher;
18
+ #assetsServer;
19
+ get #colors() {
20
+ return this.#logger.getColors();
21
+ }
22
+ constructor(cwd, options) {
23
+ this.#cwd = cwd;
24
+ this.#options = options;
25
+ this.#isMetaFile = picomatch((this.#options.metaFiles || []).map(({ pattern }) => pattern));
26
+ this.#isTestFile = picomatch(this.#options.suites
27
+ .filter((suite) => {
28
+ if (this.#options.filters.suites) {
29
+ this.#options.filters.suites.includes(suite.name);
30
+ }
31
+ return true;
32
+ })
33
+ .map((suite) => suite.files)
34
+ .flat(1));
35
+ }
36
+ #convertFiltersToArgs(filters) {
37
+ const args = [];
38
+ if (filters.suites) {
39
+ args.push(...filters.suites);
40
+ }
41
+ if (filters.files) {
42
+ args.push('--files');
43
+ args.push(filters.files.join(','));
44
+ }
45
+ if (filters.groups) {
46
+ args.push('--groups');
47
+ args.push(filters.groups.join(','));
48
+ }
49
+ if (filters.tags) {
50
+ args.push('--tags');
51
+ args.push(filters.tags.join(','));
52
+ }
53
+ if (filters.ignoreTags) {
54
+ args.push('--ignore-tags');
55
+ args.push(filters.ignoreTags.join(','));
56
+ }
57
+ if (filters.tests) {
58
+ args.push('--ignore-tests');
59
+ args.push(filters.tests.join(','));
60
+ }
61
+ if (filters.match) {
62
+ args.push('--match');
63
+ args.push(filters.match.join(','));
64
+ }
65
+ return args;
66
+ }
67
+ #clearScreen() {
68
+ if (this.#options.clearScreen) {
69
+ process.stdout.write('\u001Bc');
70
+ }
71
+ }
72
+ #runTests(port, filtersArgs, mode) {
73
+ this.#isBusy = true;
74
+ this.#testScript = runNode(this.#cwd, {
75
+ script: this.#scriptFile,
76
+ env: { PORT: port, ...this.#options.env },
77
+ nodeArgs: this.#options.nodeArgs,
78
+ scriptArgs: filtersArgs.concat(this.#options.scriptArgs),
79
+ });
80
+ this.#testScript
81
+ .then((result) => {
82
+ if (mode === 'nonblocking') {
83
+ this.#onClose?.(result.exitCode);
84
+ this.#watcher?.close();
85
+ this.#assetsServer?.stop();
86
+ }
87
+ })
88
+ .catch((error) => {
89
+ this.#logger.warning(`unable to run test script "${this.#scriptFile}"`);
90
+ this.#logger.fatal(error);
91
+ this.#onError?.(error);
92
+ this.#watcher?.close();
93
+ this.#assetsServer?.stop();
94
+ })
95
+ .finally(() => {
96
+ this.#isBusy = false;
97
+ });
98
+ }
99
+ #startAssetsServer() {
100
+ this.#assetsServer = new AssetsDevServer(this.#cwd, this.#options.assets);
101
+ this.#assetsServer.start();
102
+ }
103
+ #handleFileChange(action, port, filters, relativePath) {
104
+ if (this.#isBusy) {
105
+ return;
106
+ }
107
+ if (isDotEnvFile(relativePath) || this.#isMetaFile(relativePath)) {
108
+ this.#clearScreen();
109
+ this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
110
+ this.#runTests(port, filters, 'blocking');
111
+ }
112
+ }
113
+ #handleSourceFileChange(action, port, filters, relativePath) {
114
+ if (this.#isBusy) {
115
+ return;
116
+ }
117
+ this.#clearScreen();
118
+ this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
119
+ if (this.#isTestFile(relativePath)) {
120
+ this.#runTests(port, this.#convertFiltersToArgs({
121
+ ...this.#options.filters,
122
+ files: [relativePath],
123
+ }), 'blocking');
124
+ return;
125
+ }
126
+ this.#runTests(port, filters, 'blocking');
127
+ }
128
+ setLogger(logger) {
129
+ this.#logger = logger;
130
+ this.#assetsServer?.setLogger(logger);
131
+ return this;
132
+ }
133
+ onClose(callback) {
134
+ this.#onClose = callback;
135
+ return this;
136
+ }
137
+ onError(callback) {
138
+ this.#onError = callback;
139
+ return this;
140
+ }
141
+ async run() {
142
+ const port = String(await getPort(this.#cwd));
143
+ const initialFilters = this.#convertFiltersToArgs(this.#options.filters);
144
+ this.#clearScreen();
145
+ this.#startAssetsServer();
146
+ this.#logger.info('booting application to run tests...');
147
+ this.#runTests(port, initialFilters, 'nonblocking');
148
+ }
149
+ async runAndWatch(ts, options) {
150
+ const port = String(await getPort(this.#cwd));
151
+ const initialFilters = this.#convertFiltersToArgs(this.#options.filters);
152
+ this.#clearScreen();
153
+ this.#startAssetsServer();
154
+ this.#logger.info('booting application to run tests...');
155
+ this.#runTests(port, initialFilters, 'blocking');
156
+ const output = watch(this.#cwd, ts, options || {});
157
+ if (!output) {
158
+ this.#onClose?.(1);
159
+ return;
160
+ }
161
+ this.#watcher = output.chokidar;
162
+ output.watcher.on('watcher:ready', () => {
163
+ this.#logger.info('watching file system for changes...');
164
+ });
165
+ output.chokidar.on('error', (error) => {
166
+ this.#logger.warning('file system watcher failure');
167
+ this.#logger.fatal(error);
168
+ this.#onError?.(error);
169
+ output.chokidar.close();
170
+ });
171
+ output.watcher.on('source:add', ({ relativePath }) => this.#handleSourceFileChange('add', port, initialFilters, relativePath));
172
+ output.watcher.on('source:change', ({ relativePath }) => this.#handleSourceFileChange('update', port, initialFilters, relativePath));
173
+ output.watcher.on('source:unlink', ({ relativePath }) => this.#handleSourceFileChange('delete', port, initialFilters, relativePath));
174
+ output.watcher.on('add', ({ relativePath }) => this.#handleFileChange('add', port, initialFilters, relativePath));
175
+ output.watcher.on('change', ({ relativePath }) => this.#handleFileChange('update', port, initialFilters, relativePath));
176
+ output.watcher.on('unlink', ({ relativePath }) => this.#handleFileChange('delete', port, initialFilters, relativePath));
177
+ }
178
+ }
@@ -13,12 +13,18 @@ export type MetaFile = {
13
13
  pattern: string;
14
14
  reloadServer: boolean;
15
15
  };
16
+ export type Suite = {
17
+ files: string[];
18
+ name: string;
19
+ };
16
20
  export type AssetsBundlerOptions = {
17
21
  serve: false;
22
+ args?: string[];
18
23
  driver?: string;
19
24
  cmd?: string;
20
25
  } | {
21
26
  serve: true;
27
+ args: string[];
22
28
  driver: string;
23
29
  cmd: string;
24
30
  };
@@ -30,7 +36,26 @@ export type DevServerOptions = {
30
36
  metaFiles?: MetaFile[];
31
37
  assets?: AssetsBundlerOptions;
32
38
  };
39
+ export type TestRunnerOptions = {
40
+ filters: Partial<{
41
+ tests: string[];
42
+ suites: string[];
43
+ groups: string[];
44
+ files: string[];
45
+ match: string[];
46
+ tags: string[];
47
+ ignoreTags: string[];
48
+ }>;
49
+ scriptArgs: string[];
50
+ nodeArgs: string[];
51
+ clearScreen?: boolean;
52
+ env?: NodeJS.ProcessEnv;
53
+ metaFiles?: MetaFile[];
54
+ assets?: AssetsBundlerOptions;
55
+ suites: Suite[];
56
+ };
33
57
  export type BundlerOptions = {
34
58
  metaFiles?: MetaFile[];
35
59
  assets?: AssetsBundlerOptions;
36
60
  };
61
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAYA,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,EAAE,CAAA;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAA;IAClB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAA;CACxB,CAAA;AAKD,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,CAAC,EAAE,OAAO,CAAA;CACf,CAAA;AAKD,MAAM,MAAM,QAAQ,GAAG;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,YAAY,EAAE,OAAO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,EAAE,QAAQ,EAAE,CAAA;IACtB,MAAM,CAAC,EAAE,oBAAoB,CAAA;IAC7B,MAAM,EAAE,KAAK,EAAE,CAAA;CAChB,CAAA;AAKD,MAAM,MAAM,cAAc,GAAG;IAC3B,SAAS,CAAC,EAAE,QAAQ,EAAE,CAAA;IACtB,MAAM,CAAC,EAAE,oBAAoB,CAAA;CAC9B,CAAA"}
package/index.ts ADDED
@@ -0,0 +1,12 @@
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
+ export { Bundler } from './src/bundler.js'
11
+ export { DevServer } from './src/dev_server.js'
12
+ export { TestRunner } from './src/test_runner.js'
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@adonisjs/assembler",
3
- "version": "6.1.3-3",
3
+ "version": "6.1.3-5",
4
4
  "description": "Provides utilities to run AdonisJS development server and build project for production",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
7
7
  "files": [
8
+ "src",
9
+ "index.ts",
8
10
  "build/src",
9
11
  "build/index.d.ts",
12
+ "build/index.d.ts.map",
10
13
  "build/index.js"
11
14
  ],
12
15
  "exports": {
@@ -35,34 +38,34 @@
35
38
  "author": "virk,adonisjs",
36
39
  "license": "MIT",
37
40
  "devDependencies": {
38
- "@commitlint/cli": "^17.4.4",
39
- "@commitlint/config-conventional": "^17.4.4",
41
+ "@commitlint/cli": "^17.6.1",
42
+ "@commitlint/config-conventional": "^17.6.1",
40
43
  "@japa/assert": "^1.4.1",
41
44
  "@japa/file-system": "^1.0.1",
42
45
  "@japa/run-failed-tests": "^1.1.1",
43
46
  "@japa/runner": "^2.5.1",
44
47
  "@japa/spec-reporter": "^1.3.3",
45
- "@swc/core": "^1.3.39",
46
- "@types/node": "^18.15.0",
48
+ "@swc/core": "^1.3.51",
49
+ "@types/node": "^18.15.11",
47
50
  "c8": "^7.13.0",
48
51
  "cross-env": "^7.0.3",
49
52
  "del-cli": "^5.0.0",
50
- "eslint": "^8.36.0",
51
- "eslint-config-prettier": "^8.7.0",
53
+ "eslint": "^8.38.0",
54
+ "eslint-config-prettier": "^8.8.0",
52
55
  "eslint-plugin-adonis": "^3.0.3",
53
56
  "eslint-plugin-prettier": "^4.2.1",
54
57
  "github-label-sync": "^2.3.1",
55
58
  "husky": "^8.0.3",
56
- "np": "^7.6.3",
59
+ "np": "^7.7.0",
57
60
  "p-event": "^5.0.1",
58
- "prettier": "^2.8.4",
61
+ "prettier": "^2.8.7",
59
62
  "ts-node": "^10.9.1",
60
- "typescript": "^4.9.5"
63
+ "typescript": "^5.0.4"
61
64
  },
62
65
  "dependencies": {
63
- "@adonisjs/env": "^4.2.0-1",
64
- "@poppinss/chokidar-ts": "^4.1.0-1",
65
- "@poppinss/cliui": "^6.1.1-1",
66
+ "@adonisjs/env": "^4.2.0-2",
67
+ "@poppinss/chokidar-ts": "^4.1.0-3",
68
+ "@poppinss/cliui": "^6.1.1-2",
66
69
  "@types/picomatch": "^2.3.0",
67
70
  "cpy": "^9.0.1",
68
71
  "execa": "^7.0.0",
@@ -0,0 +1,182 @@
1
+ /*
2
+ * @adonisjs/core
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 type { ExecaChildProcess } from 'execa'
11
+ import { type Logger, cliui } from '@poppinss/cliui'
12
+
13
+ import { run } from './helpers.js'
14
+ import type { AssetsBundlerOptions } from './types.js'
15
+
16
+ /**
17
+ * Instance of CLIUI
18
+ */
19
+ const ui = cliui()
20
+
21
+ /**
22
+ * Exposes the API to start the development server for processing assets during
23
+ * development.
24
+ *
25
+ * - Here we are running the assets dev server in a child process.
26
+ * - Piping the output from the child process and reformatting it before writing it to
27
+ * process streams.
28
+ *
29
+ * AssetsDevServer is agnostic and can run any assets dev server. Be it Vite or Encore or
30
+ * even Webpack directly.
31
+ */
32
+ export class AssetsDevServer {
33
+ #cwd: URL
34
+ #logger = ui.logger
35
+ #options?: AssetsBundlerOptions
36
+ #devServer?: ExecaChildProcess<string>
37
+
38
+ /**
39
+ * Getting reference to colors library from logger
40
+ */
41
+ get #colors() {
42
+ return this.#logger.getColors()
43
+ }
44
+
45
+ constructor(cwd: URL, options?: AssetsBundlerOptions) {
46
+ this.#cwd = cwd
47
+ this.#options = options
48
+ }
49
+
50
+ /**
51
+ * Logs messages from vite dev server stdout and stderr
52
+ */
53
+ #logViteDevServerMessage(data: Buffer) {
54
+ const dataString = data.toString()
55
+ const lines = dataString.split('\n')
56
+
57
+ /**
58
+ * Logging VITE ready in message with proper
59
+ * spaces and newlines
60
+ */
61
+ if (dataString.includes('ready in')) {
62
+ console.log('')
63
+ console.log(dataString.trim())
64
+ return
65
+ }
66
+
67
+ /**
68
+ * Put a wrapper around vite network address log
69
+ */
70
+ if (dataString.includes('Local') && dataString.includes('Network')) {
71
+ const sticker = ui.sticker().useColors(this.#colors).useRenderer(this.#logger.getRenderer())
72
+
73
+ lines.forEach((line: string) => {
74
+ if (line.trim()) {
75
+ sticker.add(line)
76
+ }
77
+ })
78
+
79
+ sticker.render()
80
+ return
81
+ }
82
+
83
+ /**
84
+ * Log rest of the lines
85
+ */
86
+ lines.forEach((line: string) => {
87
+ if (line.trim()) {
88
+ console.log(line)
89
+ }
90
+ })
91
+ }
92
+
93
+ /**
94
+ * Logs messages from assets dev server stdout and stderr
95
+ */
96
+ #logAssetsDevServerMessage(data: Buffer) {
97
+ const dataString = data.toString()
98
+ const lines = dataString.split('\n')
99
+ lines.forEach((line: string) => {
100
+ if (line.trim()) {
101
+ console.log(line)
102
+ }
103
+ })
104
+ }
105
+
106
+ /**
107
+ * Set a custom CLI UI logger
108
+ */
109
+ setLogger(logger: Logger) {
110
+ this.#logger = logger
111
+ return this
112
+ }
113
+
114
+ /**
115
+ * Starts the assets bundler server. The assets bundler server process is
116
+ * considered as the secondary process and therefore we do not perform
117
+ * any cleanup if it dies.
118
+ */
119
+ start() {
120
+ if (!this.#options?.serve) {
121
+ return
122
+ }
123
+
124
+ this.#logger.info(`starting "${this.#options.driver}" dev server...`)
125
+
126
+ /**
127
+ * Create child process
128
+ */
129
+ this.#devServer = run(this.#cwd, {
130
+ script: this.#options.cmd,
131
+
132
+ /**
133
+ * We do not inherit the stdio for vite and encore, because in
134
+ * inherit mode they own the stdin and interrupts the
135
+ * `Ctrl + C` command.
136
+ */
137
+ stdio: 'pipe',
138
+ scriptArgs: this.#options.args,
139
+ })
140
+
141
+ /**
142
+ * Log child process messages
143
+ */
144
+ this.#devServer.stdout?.on('data', (data) => {
145
+ if (this.#options!.driver === 'vite') {
146
+ this.#logViteDevServerMessage(data)
147
+ } else {
148
+ this.#logAssetsDevServerMessage(data)
149
+ }
150
+ })
151
+
152
+ this.#devServer.stderr?.on('data', (data) => {
153
+ if (this.#options!.driver === 'vite') {
154
+ this.#logViteDevServerMessage(data)
155
+ } else {
156
+ this.#logAssetsDevServerMessage(data)
157
+ }
158
+ })
159
+
160
+ this.#devServer
161
+ .then((result) => {
162
+ this.#logger.warning(
163
+ `"${this.#options!.driver}" dev server closed with status code "${result.exitCode}"`
164
+ )
165
+ })
166
+ .catch((error) => {
167
+ this.#logger.warning(`unable to connect to "${this.#options!.driver}" dev server`)
168
+ this.#logger.fatal(error)
169
+ })
170
+ }
171
+
172
+ /**
173
+ * Stop the dev server
174
+ */
175
+ stop() {
176
+ if (this.#devServer) {
177
+ this.#devServer.removeAllListeners()
178
+ this.#devServer.kill('SIGKILL')
179
+ this.#devServer = undefined
180
+ }
181
+ }
182
+ }