@adonisjs/assembler 6.1.3-6 → 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.
- 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 -0
- 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 +93 -4
- 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,
|
|
@@ -75,11 +109,17 @@ export class DevServer {
|
|
|
75
109
|
this.#assetsServer?.stop();
|
|
76
110
|
});
|
|
77
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Starts the assets server
|
|
114
|
+
*/
|
|
78
115
|
#startAssetsServer() {
|
|
79
116
|
this.#assetsServer = new AssetsDevServer(this.#cwd, this.#options.assets);
|
|
80
117
|
this.#assetsServer.setLogger(this.#logger);
|
|
81
118
|
this.#assetsServer.start();
|
|
82
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Restarts the HTTP server
|
|
122
|
+
*/
|
|
83
123
|
#restartHTTPServer(port) {
|
|
84
124
|
if (this.#httpServer) {
|
|
85
125
|
this.#httpServer.removeAllListeners();
|
|
@@ -87,6 +127,9 @@ export class DevServer {
|
|
|
87
127
|
}
|
|
88
128
|
this.#startHTTPServer(port, 'blocking');
|
|
89
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Handles a non TypeScript file change
|
|
132
|
+
*/
|
|
90
133
|
#handleFileChange(action, port, relativePath) {
|
|
91
134
|
if (isDotEnvFile(relativePath) || isRcFile(relativePath)) {
|
|
92
135
|
this.#clearScreen();
|
|
@@ -105,30 +148,50 @@ export class DevServer {
|
|
|
105
148
|
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
|
|
106
149
|
}
|
|
107
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Handles TypeScript source file change
|
|
153
|
+
*/
|
|
108
154
|
#handleSourceFileChange(action, port, relativePath) {
|
|
109
155
|
this.#clearScreen();
|
|
110
156
|
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`);
|
|
111
157
|
this.#restartHTTPServer(port);
|
|
112
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Set a custom CLI UI logger
|
|
161
|
+
*/
|
|
113
162
|
setLogger(logger) {
|
|
114
163
|
this.#logger = logger;
|
|
115
164
|
this.#assetsServer?.setLogger(logger);
|
|
116
165
|
return this;
|
|
117
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Add listener to get notified when dev server is
|
|
169
|
+
* closed
|
|
170
|
+
*/
|
|
118
171
|
onClose(callback) {
|
|
119
172
|
this.#onClose = callback;
|
|
120
173
|
return this;
|
|
121
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* Add listener to get notified when dev server exists
|
|
177
|
+
* with an error
|
|
178
|
+
*/
|
|
122
179
|
onError(callback) {
|
|
123
180
|
this.#onError = callback;
|
|
124
181
|
return this;
|
|
125
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Start the development server
|
|
185
|
+
*/
|
|
126
186
|
async start() {
|
|
127
187
|
this.#clearScreen();
|
|
128
188
|
this.#logger.info('starting HTTP server...');
|
|
129
189
|
this.#startHTTPServer(String(await getPort(this.#cwd)), 'nonblocking');
|
|
130
190
|
this.#startAssetsServer();
|
|
131
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Start the development server in watch mode
|
|
194
|
+
*/
|
|
132
195
|
async startAndWatch(ts, options) {
|
|
133
196
|
const port = String(await getPort(this.#cwd));
|
|
134
197
|
this.#isWatching = true;
|
|
@@ -136,24 +199,43 @@ export class DevServer {
|
|
|
136
199
|
this.#logger.info('starting HTTP server...');
|
|
137
200
|
this.#startHTTPServer(port, 'blocking');
|
|
138
201
|
this.#startAssetsServer();
|
|
202
|
+
/**
|
|
203
|
+
* Create watcher using tsconfig.json file
|
|
204
|
+
*/
|
|
139
205
|
const output = watch(this.#cwd, ts, options || {});
|
|
140
206
|
if (!output) {
|
|
141
207
|
this.#onClose?.(1);
|
|
142
208
|
return;
|
|
143
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Storing reference to watcher, so that we can close it
|
|
212
|
+
* when HTTP server exists with error
|
|
213
|
+
*/
|
|
144
214
|
this.#watcher = output.chokidar;
|
|
215
|
+
/**
|
|
216
|
+
* Notify the watcher is ready
|
|
217
|
+
*/
|
|
145
218
|
output.watcher.on('watcher:ready', () => {
|
|
146
219
|
this.#logger.info('watching file system for changes...');
|
|
147
220
|
});
|
|
221
|
+
/**
|
|
222
|
+
* Cleanup when watcher dies
|
|
223
|
+
*/
|
|
148
224
|
output.chokidar.on('error', (error) => {
|
|
149
225
|
this.#logger.warning('file system watcher failure');
|
|
150
226
|
this.#logger.fatal(error);
|
|
151
227
|
this.#onError?.(error);
|
|
152
228
|
output.chokidar.close();
|
|
153
229
|
});
|
|
230
|
+
/**
|
|
231
|
+
* Changes in TypeScript source file
|
|
232
|
+
*/
|
|
154
233
|
output.watcher.on('source:add', ({ relativePath }) => this.#handleSourceFileChange('add', port, relativePath));
|
|
155
234
|
output.watcher.on('source:change', ({ relativePath }) => this.#handleSourceFileChange('update', port, relativePath));
|
|
156
235
|
output.watcher.on('source:unlink', ({ relativePath }) => this.#handleSourceFileChange('delete', port, relativePath));
|
|
236
|
+
/**
|
|
237
|
+
* Changes in non-TypeScript source files
|
|
238
|
+
*/
|
|
157
239
|
output.watcher.on('add', ({ relativePath }) => this.#handleFileChange('add', port, relativePath));
|
|
158
240
|
output.watcher.on('change', ({ relativePath }) => this.#handleFileChange('update', port, relativePath));
|
|
159
241
|
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
|