@adonisjs/assembler 6.1.3-6 → 6.1.3-8
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/build/index.d.ts +0 -1
- package/build/index.js +8 -0
- package/build/src/assets_dev_server.d.ts +22 -1
- package/build/src/assets_dev_server.js +63 -0
- package/build/src/bundler.d.ts +9 -1
- package/build/src/bundler.js +73 -1
- package/build/src/dev_server.d.ts +28 -1
- package/build/src/dev_server.js +82 -2
- package/build/src/helpers.d.ts +31 -1
- package/build/src/helpers.js +56 -0
- package/build/src/test_runner.d.ts +28 -1
- package/build/src/test_runner.js +103 -9
- package/build/src/types.d.ts +32 -2
- package/build/src/types.js +8 -0
- package/package.json +35 -62
- package/build/index.d.ts.map +0 -1
- package/build/src/assets_dev_server.d.ts.map +0 -1
- package/build/src/bundler.d.ts.map +0 -1
- package/build/src/dev_server.d.ts.map +0 -1
- package/build/src/helpers.d.ts.map +0 -1
- package/build/src/test_runner.d.ts.map +0 -1
- package/build/src/types.d.ts.map +0 -1
- package/index.ts +0 -12
- package/src/assets_dev_server.ts +0 -182
- package/src/bundler.ts +0 -267
- package/src/dev_server.ts +0 -307
- package/src/helpers.ts +0 -162
- package/src/test_runner.ts +0 -339
- package/src/types.ts +0 -110
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
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
|
+
*/
|
|
1
9
|
export { Bundler } from './src/bundler.js';
|
|
2
10
|
export { DevServer } from './src/dev_server.js';
|
|
3
11
|
export { TestRunner } from './src/test_runner.js';
|
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import { type Logger } from '@poppinss/cliui';
|
|
3
3
|
import type { AssetsBundlerOptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Exposes the API to start the development server for processing assets during
|
|
6
|
+
* development.
|
|
7
|
+
*
|
|
8
|
+
* - Here we are running the assets dev server in a child process.
|
|
9
|
+
* - Piping the output from the child process and reformatting it before writing it to
|
|
10
|
+
* process streams.
|
|
11
|
+
*
|
|
12
|
+
* AssetsDevServer is agnostic and can run any assets dev server. Be it Vite or Encore or
|
|
13
|
+
* even Webpack directly.
|
|
14
|
+
*/
|
|
4
15
|
export declare class AssetsDevServer {
|
|
5
16
|
#private;
|
|
6
17
|
constructor(cwd: URL, options?: AssetsBundlerOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Set a custom CLI UI logger
|
|
20
|
+
*/
|
|
7
21
|
setLogger(logger: Logger): this;
|
|
22
|
+
/**
|
|
23
|
+
* Starts the assets bundler server. The assets bundler server process is
|
|
24
|
+
* considered as the secondary process and therefore we do not perform
|
|
25
|
+
* any cleanup if it dies.
|
|
26
|
+
*/
|
|
8
27
|
start(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Stop the dev server
|
|
30
|
+
*/
|
|
9
31
|
stop(): void;
|
|
10
32
|
}
|
|
11
|
-
//# sourceMappingURL=assets_dev_server.d.ts.map
|
|
@@ -1,11 +1,36 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import { cliui } from '@poppinss/cliui';
|
|
2
10
|
import { run } from './helpers.js';
|
|
11
|
+
/**
|
|
12
|
+
* Instance of CLIUI
|
|
13
|
+
*/
|
|
3
14
|
const ui = cliui();
|
|
15
|
+
/**
|
|
16
|
+
* Exposes the API to start the development server for processing assets during
|
|
17
|
+
* development.
|
|
18
|
+
*
|
|
19
|
+
* - Here we are running the assets dev server in a child process.
|
|
20
|
+
* - Piping the output from the child process and reformatting it before writing it to
|
|
21
|
+
* process streams.
|
|
22
|
+
*
|
|
23
|
+
* AssetsDevServer is agnostic and can run any assets dev server. Be it Vite or Encore or
|
|
24
|
+
* even Webpack directly.
|
|
25
|
+
*/
|
|
4
26
|
export class AssetsDevServer {
|
|
5
27
|
#cwd;
|
|
6
28
|
#logger = ui.logger;
|
|
7
29
|
#options;
|
|
8
30
|
#devServer;
|
|
31
|
+
/**
|
|
32
|
+
* Getting reference to colors library from logger
|
|
33
|
+
*/
|
|
9
34
|
get #colors() {
|
|
10
35
|
return this.#logger.getColors();
|
|
11
36
|
}
|
|
@@ -13,14 +38,24 @@ export class AssetsDevServer {
|
|
|
13
38
|
this.#cwd = cwd;
|
|
14
39
|
this.#options = options;
|
|
15
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Logs messages from vite dev server stdout and stderr
|
|
43
|
+
*/
|
|
16
44
|
#logViteDevServerMessage(data) {
|
|
17
45
|
const dataString = data.toString();
|
|
18
46
|
const lines = dataString.split('\n');
|
|
47
|
+
/**
|
|
48
|
+
* Logging VITE ready in message with proper
|
|
49
|
+
* spaces and newlines
|
|
50
|
+
*/
|
|
19
51
|
if (dataString.includes('ready in')) {
|
|
20
52
|
console.log('');
|
|
21
53
|
console.log(dataString.trim());
|
|
22
54
|
return;
|
|
23
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Put a wrapper around vite network address log
|
|
58
|
+
*/
|
|
24
59
|
if (dataString.includes('Local') && dataString.includes('Network')) {
|
|
25
60
|
const sticker = ui.sticker().useColors(this.#colors).useRenderer(this.#logger.getRenderer());
|
|
26
61
|
lines.forEach((line) => {
|
|
@@ -31,12 +66,18 @@ export class AssetsDevServer {
|
|
|
31
66
|
sticker.render();
|
|
32
67
|
return;
|
|
33
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Log rest of the lines
|
|
71
|
+
*/
|
|
34
72
|
lines.forEach((line) => {
|
|
35
73
|
if (line.trim()) {
|
|
36
74
|
console.log(line);
|
|
37
75
|
}
|
|
38
76
|
});
|
|
39
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Logs messages from assets dev server stdout and stderr
|
|
80
|
+
*/
|
|
40
81
|
#logAssetsDevServerMessage(data) {
|
|
41
82
|
const dataString = data.toString();
|
|
42
83
|
const lines = dataString.split('\n');
|
|
@@ -46,20 +87,39 @@ export class AssetsDevServer {
|
|
|
46
87
|
}
|
|
47
88
|
});
|
|
48
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Set a custom CLI UI logger
|
|
92
|
+
*/
|
|
49
93
|
setLogger(logger) {
|
|
50
94
|
this.#logger = logger;
|
|
51
95
|
return this;
|
|
52
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Starts the assets bundler server. The assets bundler server process is
|
|
99
|
+
* considered as the secondary process and therefore we do not perform
|
|
100
|
+
* any cleanup if it dies.
|
|
101
|
+
*/
|
|
53
102
|
start() {
|
|
54
103
|
if (!this.#options?.serve) {
|
|
55
104
|
return;
|
|
56
105
|
}
|
|
57
106
|
this.#logger.info(`starting "${this.#options.driver}" dev server...`);
|
|
107
|
+
/**
|
|
108
|
+
* Create child process
|
|
109
|
+
*/
|
|
58
110
|
this.#devServer = run(this.#cwd, {
|
|
59
111
|
script: this.#options.cmd,
|
|
112
|
+
/**
|
|
113
|
+
* We do not inherit the stdio for vite and encore, because in
|
|
114
|
+
* inherit mode they own the stdin and interrupts the
|
|
115
|
+
* `Ctrl + C` command.
|
|
116
|
+
*/
|
|
60
117
|
stdio: 'pipe',
|
|
61
118
|
scriptArgs: this.#options.args,
|
|
62
119
|
});
|
|
120
|
+
/**
|
|
121
|
+
* Log child process messages
|
|
122
|
+
*/
|
|
63
123
|
this.#devServer.stdout?.on('data', (data) => {
|
|
64
124
|
if (this.#options.driver === 'vite') {
|
|
65
125
|
this.#logViteDevServerMessage(data);
|
|
@@ -85,6 +145,9 @@ export class AssetsDevServer {
|
|
|
85
145
|
this.#logger.fatal(error);
|
|
86
146
|
});
|
|
87
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Stop the dev server
|
|
150
|
+
*/
|
|
88
151
|
stop() {
|
|
89
152
|
if (this.#devServer) {
|
|
90
153
|
this.#devServer.removeAllListeners();
|
package/build/src/bundler.d.ts
CHANGED
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
import type tsStatic from 'typescript';
|
|
3
3
|
import { type Logger } from '@poppinss/cliui';
|
|
4
4
|
import type { BundlerOptions } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* The bundler class exposes the API to build an AdonisJS project.
|
|
7
|
+
*/
|
|
5
8
|
export declare class Bundler {
|
|
6
9
|
#private;
|
|
7
10
|
constructor(cwd: URL, ts: typeof tsStatic, options: BundlerOptions);
|
|
11
|
+
/**
|
|
12
|
+
* Set a custom CLI UI logger
|
|
13
|
+
*/
|
|
8
14
|
setLogger(logger: Logger): this;
|
|
15
|
+
/**
|
|
16
|
+
* Bundles the application to be run in production
|
|
17
|
+
*/
|
|
9
18
|
bundle(stopOnError?: boolean, client?: 'npm' | 'yarn' | 'pnpm'): Promise<boolean>;
|
|
10
19
|
}
|
|
11
|
-
//# sourceMappingURL=bundler.d.ts.map
|
package/build/src/bundler.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import slash from 'slash';
|
|
2
10
|
import copyfiles from 'cpy';
|
|
3
11
|
import fs from 'node:fs/promises';
|
|
@@ -5,13 +13,22 @@ import { fileURLToPath } from 'node:url';
|
|
|
5
13
|
import { join, relative } from 'node:path';
|
|
6
14
|
import { cliui } from '@poppinss/cliui';
|
|
7
15
|
import { run, parseConfig } from './helpers.js';
|
|
16
|
+
/**
|
|
17
|
+
* Instance of CLIUI
|
|
18
|
+
*/
|
|
8
19
|
const ui = cliui();
|
|
20
|
+
/**
|
|
21
|
+
* The bundler class exposes the API to build an AdonisJS project.
|
|
22
|
+
*/
|
|
9
23
|
export class Bundler {
|
|
10
24
|
#cwd;
|
|
11
25
|
#cwdPath;
|
|
12
26
|
#ts;
|
|
13
27
|
#logger = ui.logger;
|
|
14
28
|
#options;
|
|
29
|
+
/**
|
|
30
|
+
* Getting reference to colors library from logger
|
|
31
|
+
*/
|
|
15
32
|
get #colors() {
|
|
16
33
|
return this.#logger.getColors();
|
|
17
34
|
}
|
|
@@ -24,9 +41,15 @@ export class Bundler {
|
|
|
24
41
|
#getRelativeName(filePath) {
|
|
25
42
|
return slash(relative(this.#cwdPath, filePath));
|
|
26
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Cleans up the build directory
|
|
46
|
+
*/
|
|
27
47
|
async #cleanupBuildDirectory(outDir) {
|
|
28
48
|
await fs.rm(outDir, { recursive: true, force: true, maxRetries: 5 });
|
|
29
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Runs assets bundler command to build assets.
|
|
52
|
+
*/
|
|
30
53
|
async #buildAssets() {
|
|
31
54
|
const assetsBundler = this.#options.assets;
|
|
32
55
|
if (!assetsBundler?.serve) {
|
|
@@ -45,6 +68,9 @@ export class Bundler {
|
|
|
45
68
|
return false;
|
|
46
69
|
}
|
|
47
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Runs tsc command to build the source.
|
|
73
|
+
*/
|
|
48
74
|
async #runTsc(outDir) {
|
|
49
75
|
try {
|
|
50
76
|
await run(this.#cwd, {
|
|
@@ -58,9 +84,12 @@ export class Bundler {
|
|
|
58
84
|
return false;
|
|
59
85
|
}
|
|
60
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Copy files to destination directory
|
|
89
|
+
*/
|
|
61
90
|
async #copyFiles(files, outDir) {
|
|
62
91
|
try {
|
|
63
|
-
await copyfiles(files, outDir, { cwd: this.#cwdPath });
|
|
92
|
+
await copyfiles(files, outDir, { parents: true, cwd: this.#cwdPath });
|
|
64
93
|
}
|
|
65
94
|
catch (error) {
|
|
66
95
|
if (!error.message.includes("the file doesn't exist")) {
|
|
@@ -68,12 +97,18 @@ export class Bundler {
|
|
|
68
97
|
}
|
|
69
98
|
}
|
|
70
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Copy meta files to the output directory
|
|
102
|
+
*/
|
|
71
103
|
async #copyMetaFiles(outDir, additionalFilesToCopy) {
|
|
72
104
|
const metaFiles = (this.#options.metaFiles || [])
|
|
73
105
|
.map((file) => file.pattern)
|
|
74
106
|
.concat(additionalFilesToCopy);
|
|
75
107
|
await this.#copyFiles(metaFiles, outDir);
|
|
76
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Copies .adonisrc.json file to the destination
|
|
111
|
+
*/
|
|
77
112
|
async #copyAdonisRcFile(outDir) {
|
|
78
113
|
const existingContents = JSON.parse(await fs.readFile(join(this.#cwdPath, '.adonisrc.json'), 'utf-8'));
|
|
79
114
|
const compiledContents = Object.assign({}, existingContents, {
|
|
@@ -83,6 +118,9 @@ export class Bundler {
|
|
|
83
118
|
await fs.mkdir(outDir, { recursive: true });
|
|
84
119
|
await fs.writeFile(join(outDir, '.adonisrc.json'), JSON.stringify(compiledContents, null, 2) + '\n');
|
|
85
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Returns the lock file name for a given packages client
|
|
123
|
+
*/
|
|
86
124
|
#getClientLockFile(client) {
|
|
87
125
|
switch (client) {
|
|
88
126
|
case 'npm':
|
|
@@ -93,6 +131,9 @@ export class Bundler {
|
|
|
93
131
|
return 'pnpm-lock.yaml';
|
|
94
132
|
}
|
|
95
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns the installation command for a given packages client
|
|
136
|
+
*/
|
|
96
137
|
#getClientInstallCommand(client) {
|
|
97
138
|
switch (client) {
|
|
98
139
|
case 'npm':
|
|
@@ -103,24 +144,46 @@ export class Bundler {
|
|
|
103
144
|
return 'pnpm i --prod';
|
|
104
145
|
}
|
|
105
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Set a custom CLI UI logger
|
|
149
|
+
*/
|
|
106
150
|
setLogger(logger) {
|
|
107
151
|
this.#logger = logger;
|
|
108
152
|
return this;
|
|
109
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Bundles the application to be run in production
|
|
156
|
+
*/
|
|
110
157
|
async bundle(stopOnError = true, client = 'npm') {
|
|
158
|
+
/**
|
|
159
|
+
* Step 1: Parse config file to get the build output directory
|
|
160
|
+
*/
|
|
111
161
|
const config = parseConfig(this.#cwd, this.#ts);
|
|
112
162
|
if (!config) {
|
|
113
163
|
return false;
|
|
114
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Step 2: Cleanup existing build directory (if any)
|
|
167
|
+
*/
|
|
115
168
|
const outDir = config.options.outDir || fileURLToPath(new URL('build/', this.#cwd));
|
|
116
169
|
this.#logger.info('cleaning up output directory', { suffix: this.#getRelativeName(outDir) });
|
|
117
170
|
await this.#cleanupBuildDirectory(outDir);
|
|
171
|
+
/**
|
|
172
|
+
* Step 3: Build frontend assets
|
|
173
|
+
*/
|
|
118
174
|
if (!(await this.#buildAssets())) {
|
|
119
175
|
return false;
|
|
120
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Step 4: Build typescript source code
|
|
179
|
+
*/
|
|
121
180
|
this.#logger.info('compiling typescript source', { suffix: 'tsc' });
|
|
122
181
|
const buildCompleted = await this.#runTsc(outDir);
|
|
123
182
|
await this.#copyFiles(['ace.js'], outDir);
|
|
183
|
+
/**
|
|
184
|
+
* Remove incomplete build directory when tsc build
|
|
185
|
+
* failed and stopOnError is set to true.
|
|
186
|
+
*/
|
|
124
187
|
if (!buildCompleted && stopOnError) {
|
|
125
188
|
await this.#cleanupBuildDirectory(outDir);
|
|
126
189
|
const instructions = ui
|
|
@@ -132,13 +195,22 @@ export class Bundler {
|
|
|
132
195
|
this.#logger.logError(instructions.prepare());
|
|
133
196
|
return false;
|
|
134
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Step 5: Copy meta files to the build directory
|
|
200
|
+
*/
|
|
135
201
|
const pkgFiles = ['package.json', this.#getClientLockFile(client)];
|
|
136
202
|
this.#logger.info('copying meta files to the output directory');
|
|
137
203
|
await this.#copyMetaFiles(outDir, pkgFiles);
|
|
204
|
+
/**
|
|
205
|
+
* Step 6: Copy .adonisrc.json file to the build directory
|
|
206
|
+
*/
|
|
138
207
|
this.#logger.info('copying .adonisrc.json file to the output directory');
|
|
139
208
|
await this.#copyAdonisRcFile(outDir);
|
|
140
209
|
this.#logger.success('build completed');
|
|
141
210
|
this.#logger.log('');
|
|
211
|
+
/**
|
|
212
|
+
* Next steps
|
|
213
|
+
*/
|
|
142
214
|
ui.instructions()
|
|
143
215
|
.useRenderer(this.#logger.getRenderer())
|
|
144
216
|
.heading('Run the following commands to start the server in production')
|
|
@@ -2,15 +2,42 @@
|
|
|
2
2
|
import type tsStatic from 'typescript';
|
|
3
3
|
import { type Logger } from '@poppinss/cliui';
|
|
4
4
|
import type { DevServerOptions } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Exposes the API to start the development. Optionally, the watch API can be
|
|
7
|
+
* used to watch for file changes and restart the development server.
|
|
8
|
+
*
|
|
9
|
+
* The Dev server performs the following actions
|
|
10
|
+
*
|
|
11
|
+
* - Assigns a random PORT, when PORT inside .env file is in use
|
|
12
|
+
* - Uses tsconfig.json file to collect a list of files to watch.
|
|
13
|
+
* - Uses metaFiles from .adonisrc.json file to collect a list of files to watch.
|
|
14
|
+
* - Restart HTTP server on every file change.
|
|
15
|
+
*/
|
|
5
16
|
export declare class DevServer {
|
|
6
17
|
#private;
|
|
7
18
|
constructor(cwd: URL, options: DevServerOptions);
|
|
19
|
+
/**
|
|
20
|
+
* Set a custom CLI UI logger
|
|
21
|
+
*/
|
|
8
22
|
setLogger(logger: Logger): this;
|
|
23
|
+
/**
|
|
24
|
+
* Add listener to get notified when dev server is
|
|
25
|
+
* closed
|
|
26
|
+
*/
|
|
9
27
|
onClose(callback: (exitCode: number) => any): this;
|
|
28
|
+
/**
|
|
29
|
+
* Add listener to get notified when dev server exists
|
|
30
|
+
* with an error
|
|
31
|
+
*/
|
|
10
32
|
onError(callback: (error: any) => any): this;
|
|
33
|
+
/**
|
|
34
|
+
* Start the development server
|
|
35
|
+
*/
|
|
11
36
|
start(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Start the development server in watch mode
|
|
39
|
+
*/
|
|
12
40
|
startAndWatch(ts: typeof tsStatic, options?: {
|
|
13
41
|
poll: boolean;
|
|
14
42
|
}): Promise<void>;
|
|
15
43
|
}
|
|
16
|
-
//# sourceMappingURL=dev_server.d.ts.map
|
package/build/src/dev_server.js
CHANGED
|
@@ -1,8 +1,30 @@
|
|
|
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
|
+
*/
|
|
1
9
|
import picomatch from 'picomatch';
|
|
2
10
|
import { cliui } from '@poppinss/cliui';
|
|
3
11
|
import { AssetsDevServer } from './assets_dev_server.js';
|
|
4
12
|
import { getPort, isDotEnvFile, isRcFile, runNode, watch } from './helpers.js';
|
|
13
|
+
/**
|
|
14
|
+
* Instance of CLIUI
|
|
15
|
+
*/
|
|
5
16
|
const ui = cliui();
|
|
17
|
+
/**
|
|
18
|
+
* Exposes the API to start the development. Optionally, the watch API can be
|
|
19
|
+
* used to watch for file changes and restart the development server.
|
|
20
|
+
*
|
|
21
|
+
* The Dev server performs the following actions
|
|
22
|
+
*
|
|
23
|
+
* - Assigns a random PORT, when PORT inside .env file is in use
|
|
24
|
+
* - Uses tsconfig.json file to collect a list of files to watch.
|
|
25
|
+
* - Uses metaFiles from .adonisrc.json file to collect a list of files to watch.
|
|
26
|
+
* - Restart HTTP server on every file change.
|
|
27
|
+
*/
|
|
6
28
|
export class DevServer {
|
|
7
29
|
#cwd;
|
|
8
30
|
#logger = ui.logger;
|
|
@@ -16,6 +38,9 @@ export class DevServer {
|
|
|
16
38
|
#httpServer;
|
|
17
39
|
#watcher;
|
|
18
40
|
#assetsServer;
|
|
41
|
+
/**
|
|
42
|
+
* Getting reference to colors library from logger
|
|
43
|
+
*/
|
|
19
44
|
get #colors() {
|
|
20
45
|
return this.#logger.getColors();
|
|
21
46
|
}
|
|
@@ -29,6 +54,9 @@ export class DevServer {
|
|
|
29
54
|
.filter(({ reloadServer }) => reloadServer !== true)
|
|
30
55
|
.map(({ pattern }) => pattern));
|
|
31
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Inspect if child process message is from AdonisJS HTTP server
|
|
59
|
+
*/
|
|
32
60
|
#isAdonisJSReadyMessage(message) {
|
|
33
61
|
return (message !== null &&
|
|
34
62
|
typeof message === 'object' &&
|
|
@@ -36,11 +64,17 @@ export class DevServer {
|
|
|
36
64
|
'environment' in message &&
|
|
37
65
|
message.environment === 'web');
|
|
38
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Conditionally clear the terminal screen
|
|
69
|
+
*/
|
|
39
70
|
#clearScreen() {
|
|
40
71
|
if (this.#options.clearScreen) {
|
|
41
72
|
process.stdout.write('\u001Bc');
|
|
42
73
|
}
|
|
43
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Starts the HTTP server
|
|
77
|
+
*/
|
|
44
78
|
#startHTTPServer(port, mode) {
|
|
45
79
|
this.#httpServer = runNode(this.#cwd, {
|
|
46
80
|
script: this.#scriptFile,
|
|
@@ -68,18 +102,22 @@ export class DevServer {
|
|
|
68
102
|
}
|
|
69
103
|
})
|
|
70
104
|
.catch((error) => {
|
|
71
|
-
this.#logger.warning('unable to connect to underlying HTTP server process');
|
|
72
|
-
this.#logger.fatal(error);
|
|
73
105
|
this.#onError?.(error);
|
|
74
106
|
this.#watcher?.close();
|
|
75
107
|
this.#assetsServer?.stop();
|
|
76
108
|
});
|
|
77
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Starts the assets server
|
|
112
|
+
*/
|
|
78
113
|
#startAssetsServer() {
|
|
79
114
|
this.#assetsServer = new AssetsDevServer(this.#cwd, this.#options.assets);
|
|
80
115
|
this.#assetsServer.setLogger(this.#logger);
|
|
81
116
|
this.#assetsServer.start();
|
|
82
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Restarts the HTTP server
|
|
120
|
+
*/
|
|
83
121
|
#restartHTTPServer(port) {
|
|
84
122
|
if (this.#httpServer) {
|
|
85
123
|
this.#httpServer.removeAllListeners();
|
|
@@ -87,6 +125,9 @@ export class DevServer {
|
|
|
87
125
|
}
|
|
88
126
|
this.#startHTTPServer(port, 'blocking');
|
|
89
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Handles a non TypeScript file change
|
|
130
|
+
*/
|
|
90
131
|
#handleFileChange(action, port, relativePath) {
|
|
91
132
|
if (isDotEnvFile(relativePath) || isRcFile(relativePath)) {
|
|
92
133
|
this.#clearScreen();
|
|
@@ -105,30 +146,50 @@ export class DevServer {
|
|
|
105
146
|
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
|
|
106
147
|
}
|
|
107
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Handles TypeScript source file change
|
|
151
|
+
*/
|
|
108
152
|
#handleSourceFileChange(action, port, relativePath) {
|
|
109
153
|
this.#clearScreen();
|
|
110
154
|
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
|
|
111
155
|
this.#restartHTTPServer(port);
|
|
112
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Set a custom CLI UI logger
|
|
159
|
+
*/
|
|
113
160
|
setLogger(logger) {
|
|
114
161
|
this.#logger = logger;
|
|
115
162
|
this.#assetsServer?.setLogger(logger);
|
|
116
163
|
return this;
|
|
117
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Add listener to get notified when dev server is
|
|
167
|
+
* closed
|
|
168
|
+
*/
|
|
118
169
|
onClose(callback) {
|
|
119
170
|
this.#onClose = callback;
|
|
120
171
|
return this;
|
|
121
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Add listener to get notified when dev server exists
|
|
175
|
+
* with an error
|
|
176
|
+
*/
|
|
122
177
|
onError(callback) {
|
|
123
178
|
this.#onError = callback;
|
|
124
179
|
return this;
|
|
125
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Start the development server
|
|
183
|
+
*/
|
|
126
184
|
async start() {
|
|
127
185
|
this.#clearScreen();
|
|
128
186
|
this.#logger.info('starting HTTP server...');
|
|
129
187
|
this.#startHTTPServer(String(await getPort(this.#cwd)), 'nonblocking');
|
|
130
188
|
this.#startAssetsServer();
|
|
131
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Start the development server in watch mode
|
|
192
|
+
*/
|
|
132
193
|
async startAndWatch(ts, options) {
|
|
133
194
|
const port = String(await getPort(this.#cwd));
|
|
134
195
|
this.#isWatching = true;
|
|
@@ -136,24 +197,43 @@ export class DevServer {
|
|
|
136
197
|
this.#logger.info('starting HTTP server...');
|
|
137
198
|
this.#startHTTPServer(port, 'blocking');
|
|
138
199
|
this.#startAssetsServer();
|
|
200
|
+
/**
|
|
201
|
+
* Create watcher using tsconfig.json file
|
|
202
|
+
*/
|
|
139
203
|
const output = watch(this.#cwd, ts, options || {});
|
|
140
204
|
if (!output) {
|
|
141
205
|
this.#onClose?.(1);
|
|
142
206
|
return;
|
|
143
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Storing reference to watcher, so that we can close it
|
|
210
|
+
* when HTTP server exists with error
|
|
211
|
+
*/
|
|
144
212
|
this.#watcher = output.chokidar;
|
|
213
|
+
/**
|
|
214
|
+
* Notify the watcher is ready
|
|
215
|
+
*/
|
|
145
216
|
output.watcher.on('watcher:ready', () => {
|
|
146
217
|
this.#logger.info('watching file system for changes...');
|
|
147
218
|
});
|
|
219
|
+
/**
|
|
220
|
+
* Cleanup when watcher dies
|
|
221
|
+
*/
|
|
148
222
|
output.chokidar.on('error', (error) => {
|
|
149
223
|
this.#logger.warning('file system watcher failure');
|
|
150
224
|
this.#logger.fatal(error);
|
|
151
225
|
this.#onError?.(error);
|
|
152
226
|
output.chokidar.close();
|
|
153
227
|
});
|
|
228
|
+
/**
|
|
229
|
+
* Changes in TypeScript source file
|
|
230
|
+
*/
|
|
154
231
|
output.watcher.on('source:add', ({ relativePath }) => this.#handleSourceFileChange('add', port, relativePath));
|
|
155
232
|
output.watcher.on('source:change', ({ relativePath }) => this.#handleSourceFileChange('update', port, relativePath));
|
|
156
233
|
output.watcher.on('source:unlink', ({ relativePath }) => this.#handleSourceFileChange('delete', port, relativePath));
|
|
234
|
+
/**
|
|
235
|
+
* Changes in non-TypeScript source files
|
|
236
|
+
*/
|
|
157
237
|
output.watcher.on('add', ({ relativePath }) => this.#handleFileChange('add', port, relativePath));
|
|
158
238
|
output.watcher.on('change', ({ relativePath }) => this.#handleFileChange('update', port, relativePath));
|
|
159
239
|
output.watcher.on('unlink', ({ relativePath }) => this.#handleFileChange('delete', port, relativePath));
|
package/build/src/helpers.d.ts
CHANGED
|
@@ -2,14 +2,44 @@
|
|
|
2
2
|
import type tsStatic from 'typescript';
|
|
3
3
|
import { Watcher } from '@poppinss/chokidar-ts';
|
|
4
4
|
import type { RunOptions, WatchOptions } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Parses tsconfig.json and prints errors using typescript compiler
|
|
7
|
+
* host
|
|
8
|
+
*/
|
|
5
9
|
export declare function parseConfig(cwd: string | URL, ts: typeof tsStatic): tsStatic.ParsedCommandLine | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Runs a Node.js script as a child process and inherits the stdio streams
|
|
12
|
+
*/
|
|
6
13
|
export declare function runNode(cwd: string | URL, options: RunOptions): import("execa").ExecaChildProcess<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Runs a script as a child process and inherits the stdio streams
|
|
16
|
+
*/
|
|
7
17
|
export declare function run(cwd: string | URL, options: Omit<RunOptions, 'nodeArgs'>): import("execa").ExecaChildProcess<string>;
|
|
18
|
+
/**
|
|
19
|
+
* Watches the file system using tsconfig file
|
|
20
|
+
*/
|
|
8
21
|
export declare function watch(cwd: string | URL, ts: typeof tsStatic, options: WatchOptions): {
|
|
9
22
|
watcher: Watcher;
|
|
10
23
|
chokidar: import("chokidar").FSWatcher;
|
|
11
24
|
} | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Check if file is an .env file
|
|
27
|
+
*/
|
|
12
28
|
export declare function isDotEnvFile(filePath: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Check if file is .adonisrc.json file
|
|
31
|
+
*/
|
|
13
32
|
export declare function isRcFile(filePath: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the port to use after inspect the dot-env files inside
|
|
35
|
+
* a given directory.
|
|
36
|
+
*
|
|
37
|
+
* A random port is used when the specified port is in use. Following
|
|
38
|
+
* is the logic for finding a specified port.
|
|
39
|
+
*
|
|
40
|
+
* - The "process.env.PORT" value is used if exists.
|
|
41
|
+
* - The dot-env files are loaded using the "EnvLoader" and the PORT
|
|
42
|
+
* value is by iterating over all the loaded files. The iteration
|
|
43
|
+
* stops after first find.
|
|
44
|
+
*/
|
|
14
45
|
export declare function getPort(cwd: URL): Promise<number>;
|
|
15
|
-
//# sourceMappingURL=helpers.d.ts.map
|