@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/index.ts DELETED
@@ -1,12 +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
- export { Bundler } from './src/bundler.js'
11
- export { DevServer } from './src/dev_server.js'
12
- export { TestRunner } from './src/test_runner.js'
@@ -1,182 +0,0 @@
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
- }
package/src/bundler.ts DELETED
@@ -1,267 +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 slash from 'slash'
11
- import copyfiles from 'cpy'
12
- import fs from 'node:fs/promises'
13
- import type tsStatic from 'typescript'
14
- import { fileURLToPath } from 'node:url'
15
- import { join, relative } from 'node:path'
16
- import { cliui, type Logger } from '@poppinss/cliui'
17
-
18
- import { run, parseConfig } from './helpers.js'
19
- import type { BundlerOptions } from './types.js'
20
-
21
- /**
22
- * Instance of CLIUI
23
- */
24
- const ui = cliui()
25
-
26
- /**
27
- * The bundler class exposes the API to build an AdonisJS project.
28
- */
29
- export class Bundler {
30
- #cwd: URL
31
- #cwdPath: string
32
- #ts: typeof tsStatic
33
- #logger = ui.logger
34
- #options: BundlerOptions
35
-
36
- /**
37
- * Getting reference to colors library from logger
38
- */
39
- get #colors() {
40
- return this.#logger.getColors()
41
- }
42
-
43
- constructor(cwd: URL, ts: typeof tsStatic, options: BundlerOptions) {
44
- this.#cwd = cwd
45
- this.#cwdPath = fileURLToPath(this.#cwd)
46
- this.#ts = ts
47
- this.#options = options
48
- }
49
-
50
- #getRelativeName(filePath: string) {
51
- return slash(relative(this.#cwdPath, filePath))
52
- }
53
-
54
- /**
55
- * Cleans up the build directory
56
- */
57
- async #cleanupBuildDirectory(outDir: string) {
58
- await fs.rm(outDir, { recursive: true, force: true, maxRetries: 5 })
59
- }
60
-
61
- /**
62
- * Runs assets bundler command to build assets.
63
- */
64
- async #buildAssets(): Promise<boolean> {
65
- const assetsBundler = this.#options.assets
66
- if (!assetsBundler?.serve) {
67
- return true
68
- }
69
-
70
- try {
71
- this.#logger.info('compiling frontend assets', { suffix: assetsBundler.cmd })
72
- await run(this.#cwd, {
73
- stdio: 'inherit',
74
- script: assetsBundler.cmd,
75
- scriptArgs: [],
76
- })
77
- return true
78
- } catch {
79
- return false
80
- }
81
- }
82
-
83
- /**
84
- * Runs tsc command to build the source.
85
- */
86
- async #runTsc(outDir: string): Promise<boolean> {
87
- try {
88
- await run(this.#cwd, {
89
- stdio: 'inherit',
90
- script: 'tsc',
91
- scriptArgs: ['--outDir', outDir],
92
- })
93
- return true
94
- } catch {
95
- return false
96
- }
97
- }
98
-
99
- /**
100
- * Copy files to destination directory
101
- */
102
- async #copyFiles(files: string[], outDir: string) {
103
- try {
104
- await copyfiles(files, outDir, { cwd: this.#cwdPath })
105
- } catch (error) {
106
- if (!error.message.includes("the file doesn't exist")) {
107
- throw error
108
- }
109
- }
110
- }
111
-
112
- /**
113
- * Copy meta files to the output directory
114
- */
115
- async #copyMetaFiles(outDir: string, additionalFilesToCopy: string[]) {
116
- const metaFiles = (this.#options.metaFiles || [])
117
- .map((file) => file.pattern)
118
- .concat(additionalFilesToCopy)
119
-
120
- await this.#copyFiles(metaFiles, outDir)
121
- }
122
-
123
- /**
124
- * Copies .adonisrc.json file to the destination
125
- */
126
- async #copyAdonisRcFile(outDir: string) {
127
- const existingContents = JSON.parse(
128
- await fs.readFile(join(this.#cwdPath, '.adonisrc.json'), 'utf-8')
129
- )
130
- const compiledContents = Object.assign({}, existingContents, {
131
- typescript: false,
132
- lastCompiledAt: new Date().toISOString(),
133
- })
134
-
135
- await fs.mkdir(outDir, { recursive: true })
136
- await fs.writeFile(
137
- join(outDir, '.adonisrc.json'),
138
- JSON.stringify(compiledContents, null, 2) + '\n'
139
- )
140
- }
141
-
142
- /**
143
- * Returns the lock file name for a given packages client
144
- */
145
- #getClientLockFile(client: 'npm' | 'yarn' | 'pnpm') {
146
- switch (client) {
147
- case 'npm':
148
- return 'package-lock.json'
149
- case 'yarn':
150
- return 'yarn.lock'
151
- case 'pnpm':
152
- return 'pnpm-lock.yaml'
153
- }
154
- }
155
-
156
- /**
157
- * Returns the installation command for a given packages client
158
- */
159
- #getClientInstallCommand(client: 'npm' | 'yarn' | 'pnpm') {
160
- switch (client) {
161
- case 'npm':
162
- return 'npm ci --omit="dev"'
163
- case 'yarn':
164
- return 'yarn install --production'
165
- case 'pnpm':
166
- return 'pnpm i --prod'
167
- }
168
- }
169
-
170
- /**
171
- * Set a custom CLI UI logger
172
- */
173
- setLogger(logger: Logger) {
174
- this.#logger = logger
175
- return this
176
- }
177
-
178
- /**
179
- * Bundles the application to be run in production
180
- */
181
- async bundle(
182
- stopOnError: boolean = true,
183
- client: 'npm' | 'yarn' | 'pnpm' = 'npm'
184
- ): Promise<boolean> {
185
- /**
186
- * Step 1: Parse config file to get the build output directory
187
- */
188
- const config = parseConfig(this.#cwd, this.#ts)
189
- if (!config) {
190
- return false
191
- }
192
-
193
- /**
194
- * Step 2: Cleanup existing build directory (if any)
195
- */
196
- const outDir = config.options.outDir || fileURLToPath(new URL('build/', this.#cwd))
197
- this.#logger.info('cleaning up output directory', { suffix: this.#getRelativeName(outDir) })
198
- await this.#cleanupBuildDirectory(outDir)
199
-
200
- /**
201
- * Step 3: Build frontend assets
202
- */
203
- if (!(await this.#buildAssets())) {
204
- return false
205
- }
206
-
207
- /**
208
- * Step 4: Build typescript source code
209
- */
210
- this.#logger.info('compiling typescript source', { suffix: 'tsc' })
211
- const buildCompleted = await this.#runTsc(outDir)
212
- await this.#copyFiles(['ace.js'], outDir)
213
-
214
- /**
215
- * Remove incomplete build directory when tsc build
216
- * failed and stopOnError is set to true.
217
- */
218
- if (!buildCompleted && stopOnError) {
219
- await this.#cleanupBuildDirectory(outDir)
220
- const instructions = ui
221
- .sticker()
222
- .fullScreen()
223
- .drawBorder((borderChar, colors) => colors.red(borderChar))
224
-
225
- instructions.add(
226
- this.#colors.red('Cannot complete the build process as there are TypeScript errors.')
227
- )
228
- instructions.add(
229
- this.#colors.red(
230
- 'Use "--ignore-ts-errors" flag to ignore TypeScript errors and continue the build.'
231
- )
232
- )
233
-
234
- this.#logger.logError(instructions.prepare())
235
- return false
236
- }
237
-
238
- /**
239
- * Step 5: Copy meta files to the build directory
240
- */
241
- const pkgFiles = ['package.json', this.#getClientLockFile(client)]
242
- this.#logger.info('copying meta files to the output directory')
243
- await this.#copyMetaFiles(outDir, pkgFiles)
244
-
245
- /**
246
- * Step 6: Copy .adonisrc.json file to the build directory
247
- */
248
- this.#logger.info('copying .adonisrc.json file to the output directory')
249
- await this.#copyAdonisRcFile(outDir)
250
-
251
- this.#logger.success('build completed')
252
- this.#logger.log('')
253
-
254
- /**
255
- * Next steps
256
- */
257
- ui.instructions()
258
- .useRenderer(this.#logger.getRenderer())
259
- .heading('Run the following commands to start the server in production')
260
- .add(this.#colors.cyan(`cd ${this.#getRelativeName(outDir)}`))
261
- .add(this.#colors.cyan(this.#getClientInstallCommand(client)))
262
- .add(this.#colors.cyan('node bin/server.js'))
263
- .render()
264
-
265
- return true
266
- }
267
- }