Skip to content

Commit 8274350

Browse files
committed
refactor: remove installation step from production build
The installation step doesn't solve a real problem, since one should always install production dependencies on the production server. So the build now only creates the final build, leaving the installation aside.
1 parent c6afaad commit 8274350

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

commands/Build.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class Build extends BaseCommand {
2020
/**
2121
* Allows watching for file changes
2222
*/
23-
@flags.boolean({ description: 'Watch for file changes and re-build the project', alias: 'w' })
23+
@flags.boolean({ description: 'Watch filesystem and re-compile changes', alias: 'w' })
2424
public watch: boolean
2525

2626
/**
@@ -30,11 +30,21 @@ export default class Build extends BaseCommand {
3030
public production: boolean
3131

3232
/**
33-
* Use yarn when building for production to install dependencies
33+
* Select the client for deciding the lock file to copy to the
34+
* build folder
3435
*/
35-
@flags.string({ description: 'Select between npm or yarn for installing dependencies' })
36+
@flags.string({ description: 'Select the package manager to decide which lock file to copy to the build folder' })
3637
public client: string
3738

39+
/**
40+
* Detect changes by polling files
41+
*/
42+
@flags.boolean({
43+
description: 'Detect file changes by polling files instead of listening to filesystem events',
44+
alias: 'p',
45+
})
46+
public poll: boolean
47+
3848
/**
3949
* Invoked automatically by ace
4050
*/
@@ -51,7 +61,7 @@ export default class Build extends BaseCommand {
5161
*/
5262
if (!cwd || !ADONIS_IS_TYPESCRIPT()) {
5363
this.logger.error(
54-
'Cannot build non-typescript project. Make sure to run "node ace build" from the project root',
64+
'Cannot build a non-typescript project. Make sure to run "node ace build" from the project root',
5565
)
5666
return
5767
}
@@ -76,7 +86,7 @@ export default class Build extends BaseCommand {
7686
if (this.production) {
7787
await new Compiler(cwd, false, [], this.logger).compileForProduction(this.client)
7888
} else if (this.watch) {
79-
await new Watcher(cwd, false, [], this.logger).watch()
89+
await new Watcher(cwd, false, [], this.logger).watch(this.poll)
8090
} else {
8191
await new Compiler(cwd, false, [], this.logger).compile()
8292
}

src/Compiler/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import copyfiles from 'cpy'
1313
import debounce from 'debounce'
1414
import tsStatic from 'typescript'
1515
import { join, relative } from 'path'
16+
import { Colors } from '@poppinss/colors'
1617
import { Logger } from '@poppinss/fancy-logs'
1718
import { remove, outputJSON } from 'fs-extra'
1819
import { resolveFrom } from '@poppinss/utils'
@@ -21,7 +22,6 @@ import { iocTransformer } from '@adonisjs/ioc-transformer'
2122

2223
import { RcFile } from '../RcFile'
2324
import { Manifest } from '../Manifest'
24-
import { Installer } from '../Installer'
2525
import { HttpServer, DummyHttpServer } from '../HttpServer'
2626
import {
2727
RCFILE_NAME,
@@ -70,6 +70,8 @@ export class Compiler {
7070
*/
7171
private getRelativeUnixPath = mem((absPath: string) => slash(relative(this.appRoot, absPath)))
7272

73+
private colors = new Colors()
74+
7375
constructor (
7476
public appRoot: string,
7577
private serveApp: boolean,
@@ -244,22 +246,18 @@ export class Compiler {
244246
await this.copyMetaFiles(config.options.outDir!, pkgFiles)
245247
this.buildTypescriptSource(config)
246248
await this.copyAdonisRcFile(config.options.outDir!)
249+
await this.manifest.generate()
247250

248-
this.logger.info({ message: 'installing production dependencies', suffix: client })
249-
await new Installer(config.options.outDir!, client).install()
251+
const installCommand = client === 'npm'
252+
? 'npm ci --production'
253+
: 'yarn install --production'
250254

251-
/**
252-
* Manifest can be generated without blocking the flow
253-
*/
254-
this.manifest.generate()
255+
console.log(' Run the following commands to start the server in production')
256+
const relativeBuildPath = this.getRelativeUnixPath(config.options.outDir!)
255257

256-
/**
257-
* Start HTTP server in production
258-
*/
259-
if (this.serveApp) {
260-
this.createHttpServer(config.options.outDir!)
261-
this.httpServer.start()
262-
}
258+
console.log(` ${this.colors.gray('$')} ${this.colors.cyan(`cd ${relativeBuildPath}`)}`)
259+
console.log(` ${this.colors.gray('$')} ${this.colors.cyan(installCommand)}`)
260+
console.log(` ${this.colors.gray('$')} ${this.colors.cyan('node server.js')}`)
263261

264262
return true
265263
}

test/compiler.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ test.group('Compiler', (group) => {
275275
])
276276
}).timeout(0)
277277

278-
test('build for production by doing npm install', async (assert) => {
278+
test('build for production should copy package files to build folder', async (assert) => {
279279
const logger = new Logger({ fake: true })
280280

281281
await fs.add('.adonisrc.json', JSON.stringify({
@@ -325,7 +325,6 @@ test.group('Compiler', (group) => {
325325
'underline(magenta(pending)) compiling typescript source files',
326326
'underline(green(success)) built successfully',
327327
'underline(blue(info)) copy .adonisrc.json dim(yellow(build))',
328-
'underline(blue(info)) installing production dependencies dim(yellow(npm))',
329328
])
330329

331330
const hasPackageLock = await fs.fsExtra.pathExists(join(fs.basePath, 'build', 'package-lock.json'))

0 commit comments

Comments
 (0)