Commit ebf217cb authored by DevSide's avatar DevSide Committed by Tim Neutkens

add --node-args option (#5858)

This message is from @timneutkens after making changes:
- Convert executables to Typescript
- Remove `minimist` in favor of `arg` 
- Implement `--node-args` usage: `--node-args="--throw-deprecation"`
- Adds tests for usage of the `next` cli
parent dfa0f826
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
- [Configuring the onDemandEntries](#configuring-the-ondemandentries) - [Configuring the onDemandEntries](#configuring-the-ondemandentries)
- [Configuring extensions looked for when resolving pages in `pages`](#configuring-extensions-looked-for-when-resolving-pages-in-pages) - [Configuring extensions looked for when resolving pages in `pages`](#configuring-extensions-looked-for-when-resolving-pages-in-pages)
- [Configuring the build ID](#configuring-the-build-id) - [Configuring the build ID](#configuring-the-build-id)
- [Configuring Next process script](#configuring-next-process-script)
- [Customizing webpack config](#customizing-webpack-config) - [Customizing webpack config](#customizing-webpack-config)
- [Customizing babel config](#customizing-babel-config) - [Customizing babel config](#customizing-babel-config)
- [Exposing configuration to the server / client side](#exposing-configuration-to-the-server--client-side) - [Exposing configuration to the server / client side](#exposing-configuration-to-the-server--client-side)
...@@ -1337,6 +1338,17 @@ module.exports = { ...@@ -1337,6 +1338,17 @@ module.exports = {
} }
``` ```
#### Configuring next process script
You can pass any node arguments to `next` cli command.
```bash
next --node-args="--throw-deprecation"
next start --node-args="--inspect"
next build --node-args="-r esm"
```
### Customizing webpack config ### Customizing webpack config
<details> <details>
......
#!/usr/bin/env node #!/usr/bin/env node
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import parseArgs from 'minimist' import arg from 'arg'
import build from '../build' import build from '../build'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
const argv = parseArgs(process.argv.slice(2), { const args = arg({
alias: { // Types
h: 'help', '--help': Boolean,
l: 'lambdas' '--lambdas': Boolean,
},
boolean: ['h', 'l'] // Aliases
'-h': '--help',
'-l': '--lambdas'
}) })
if (argv.help) { if (args['--help']) {
printAndExit(` printAndExit(`
Description Description
Compiles the application for production deployment Compiles the application for production deployment
...@@ -27,8 +29,8 @@ if (argv.help) { ...@@ -27,8 +29,8 @@ if (argv.help) {
`, 0) `, 0)
} }
const dir = resolve(argv._[0] || '.') const dir = resolve(args._[0] || '.')
const lambdas = argv.lambdas const lambdas = args['--lambdas']
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!existsSync(dir)) { if (!existsSync(dir)) {
......
#!/usr/bin/env node #!/usr/bin/env node
import { resolve, join } from 'path' import { resolve, join } from 'path'
import parseArgs from 'minimist' import arg from 'arg'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import startServer from '../server/lib/start-server' import startServer from '../server/lib/start-server'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
const argv = parseArgs(process.argv.slice(2), { const args = arg({
alias: { // Types
h: 'help', '--help': Boolean,
H: 'hostname', '--port': Number,
p: 'port' '--hostname': String,
},
boolean: ['h'], // Aliases
string: ['H'], '-h': '--help',
default: { p: 3000 } '-p': '--port',
'-H': '--hostname'
}) })
if (argv.help) { if (args['--help']) {
console.log(` console.log(`
Description Description
Starts the application in development mode (hot-code reloading, error Starts the application in development mode (hot-code reloading, error
...@@ -37,7 +38,7 @@ if (argv.help) { ...@@ -37,7 +38,7 @@ if (argv.help) {
process.exit(0) process.exit(0)
} }
const dir = resolve(argv._[0] || '.') const dir = resolve(args._[0] || '.')
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!existsSync(dir)) { if (!existsSync(dir)) {
...@@ -52,13 +53,14 @@ if (!existsSync(join(dir, 'pages'))) { ...@@ -52,13 +53,14 @@ if (!existsSync(join(dir, 'pages'))) {
printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root') printAndExit('> Couldn\'t find a `pages` directory. Please create one under the project root')
} }
startServer({dir, dev: true}, argv.port, argv.hostname) const port = args['--port'] || 3000
startServer({dir, dev: true}, port, args['--hostname'])
.then(async () => { .then(async () => {
console.log(`> Ready on http://${argv.hostname ? argv.hostname : 'localhost'}:${argv.port}`) console.log(`> Ready on http://${args['--hostname'] || 'localhost'}:${port}`)
}) })
.catch((err) => { .catch((err) => {
if (err.code === 'EADDRINUSE') { if (err.code === 'EADDRINUSE') {
let errorMessage = `Port ${argv.port} is already in use.` let errorMessage = `Port ${port} is already in use.`
const pkgAppPath = require('find-up').sync('package.json', { const pkgAppPath = require('find-up').sync('package.json', {
cwd: dir cwd: dir
}) })
......
#!/usr/bin/env node #!/usr/bin/env node
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import parseArgs from 'minimist' import arg from 'arg'
import exportApp from '../export' import exportApp from '../export'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
const argv = parseArgs(process.argv.slice(2), { const args = arg({
alias: { // Types
h: 'help', '--help': Boolean,
s: 'silent', '--silent': Boolean,
o: 'outdir' '--outdir': String,
}, '--threads': Number,
boolean: ['h'], '--concurrency': Number,
default: {
s: false, // Aliases
o: null '-h': '--help',
} '-s': '--silent',
'-o': '--outdir'
}) })
if (argv.help) { if (args['--help']) {
console.log(` console.log(`
Description Description
Exports the application for production deployment Exports the application for production deployment
...@@ -37,7 +38,7 @@ if (argv.help) { ...@@ -37,7 +38,7 @@ if (argv.help) {
process.exit(0) process.exit(0)
} }
const dir = resolve(argv._[0] || '.') const dir = resolve(args._[0] || '.')
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!existsSync(dir)) { if (!existsSync(dir)) {
...@@ -53,10 +54,10 @@ if (!existsSync(join(dir, 'pages'))) { ...@@ -53,10 +54,10 @@ if (!existsSync(join(dir, 'pages'))) {
} }
const options = { const options = {
silent: argv.silent, silent: args['--silent'] || false,
threads: argv.threads, threads: args['--threads'],
concurrency: argv.concurrency, concurrency: args['--concurrency'],
outdir: argv.outdir ? resolve(argv.outdir) : resolve(dir, 'out') outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out')
} }
exportApp(dir, options) exportApp(dir, options)
......
#!/usr/bin/env node
console.log('`next init` is not supported anymore. These community projects provide the same functionality as `next init` with additional features: http://npmjs.com/next-init and http://npmjs.com/create-next-app.')
process.exit(0)
#!/usr/bin/env node #!/usr/bin/env node
import { resolve } from 'path' import { resolve } from 'path'
import parseArgs from 'minimist' import arg from 'arg'
import startServer from '../server/lib/start-server' import startServer from '../server/lib/start-server'
const argv = parseArgs(process.argv.slice(2), { const args = arg({
alias: { // Types
h: 'help', '--help': Boolean,
H: 'hostname', '--port': Number,
p: 'port' '--hostname': String,
},
boolean: ['h'],
string: ['H'],
default: { p: 3000 }
})
if (argv.hostname === '') { // Aliases
console.error(`> Provided hostname argument has no value`) '-h': '--help',
process.exit(1) '-p': '--port',
} '-H': '--hostname'
})
if (argv.help) { if (args['--help']) {
console.log(` console.log(`
Description Description
Starts the application in production mode. Starts the application in production mode.
...@@ -42,11 +38,11 @@ if (argv.help) { ...@@ -42,11 +38,11 @@ if (argv.help) {
process.exit(0) process.exit(0)
} }
const dir = resolve(argv._[0] || '.') const dir = resolve(args._[0] || '.')
const port = args['--port'] || 3000
startServer({dir}, argv.port, argv.hostname) startServer({dir}, port, args['--hostname'])
.then(() => { .then(() => {
console.log(`> Ready on http://${argv.hostname ? argv.hostname : 'localhost'}:${argv.port}`) console.log(`> Ready on http://${args['--hostname']}:${port}`)
}) })
.catch((err) => { .catch((err) => {
console.error(err) console.error(err)
......
#!/usr/bin/env node #!/usr/bin/env node
import { join } from 'path' import { join } from 'path'
import { spawn } from 'cross-spawn' import spawn from 'cross-spawn'
import pkg from '../../package.json' import arg from 'arg'
import {CONFIG_FILE} from 'next-server/constants'
['react', 'react-dom'].forEach(dependency => {
if (pkg.peerDependencies) { try {
Object.keys(pkg.peerDependencies).forEach(dependency => { // When 'npm link' is used it checks the clone location. Not the project.
try { require.resolve(dependency)
// When 'npm link' is used it checks the clone location. Not the project. } catch (err) {
require.resolve(dependency) console.warn(`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install --save ${dependency}'`)
} catch (err) { }
console.warn(`The module '${dependency}' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install --save ${dependency}'`) })
}
})
}
const defaultCommand = 'dev' const defaultCommand = 'dev'
const commands = new Set([ const commands = [
'init',
'build', 'build',
'start', 'start',
'export', 'export',
defaultCommand defaultCommand
]) ]
const args = arg({
// Types
'--version': Boolean,
'--help': Boolean,
'--node-args': String,
'--inspect': Boolean,
let cmd = process.argv[2] // Aliases
let args = [] '-v': '--version',
let nodeArgs = [] '-h': '--help'
}, {
permissive: true
})
if (new Set(['--version', '-v']).has(cmd)) { // Version is inlined into the file using taskr build pipeline
console.log(`next.js v${pkg.version}`) if (args['--version']) {
console.log(`Next.js v${process.env.NEXT_VERSION}`)
process.exit(0) process.exit(0)
} }
const inspectArg = process.argv.find(arg => arg.includes('--inspect')) // Check if we are running `next <subcommand>` or `next`
if (inspectArg) { const foundCommand = args._.find(cmd => commands.includes(cmd))
nodeArgs.push(inspectArg)
}
if (new Set(['--help', '-h']).has(cmd)) { // Makes sure the `next <subcommand> --help` case is covered
// This help message is only showed for `next --help`
if (!foundCommand && args['--help']) {
console.log(` console.log(`
Usage Usage
$ next <command> $ next <command>
Available commands Available commands
${Array.from(commands).join(', ')} ${commands.join(', ')}
Options
--version, -p Version number
--node-args Node arguments applied to the process
--help, -h Displays this message
For more information run a command with the --help flag For more information run a command with the --help flag
$ next init --help $ next build --help
`) `)
process.exit(0) process.exit(0)
} }
if (commands.has(cmd)) { // Add support for `--node-args` to send Node.js arguments to the spawned process
args = process.argv.slice(3) const nodeArguments = args['--node-args'] && args['--node-args'] !== '' ? args['--node-args'].split(' ') : []
} else { if (args['--inspect']) {
cmd = defaultCommand console.log('The `--inspect` option is deprecated in favor of `--node-args`')
args = process.argv.slice(2) nodeArguments.push('--inspect')
} }
const defaultEnv = cmd === 'dev' ? 'development' : 'production' if (nodeArguments.length > 0) {
console.log(`Passing arguments to Node.js: "${nodeArguments.join(' ')}"`)
}
const command = foundCommand || defaultCommand
const forwardedArgs = args._.filter(arg => arg !== command)
// Make sure the `next <subcommand> --help` case is covered
if (args['--help']) {
forwardedArgs.push('--help')
}
const defaultEnv = command === 'dev' ? 'development' : 'production'
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
const bin = join(__dirname, 'next-' + cmd) const bin = join(__dirname, 'next-' + command)
const startProcess = () => { const startProcess = () => {
const proc = spawn('node', [...nodeArgs, ...[bin], ...args], { stdio: 'inherit', customFds: [0, 1, 2] }) const proc = spawn('node', [...nodeArguments, bin, ...forwardedArgs], { stdio: 'inherit' })
proc.on('close', (code, signal) => { proc.on('close', (code, signal) => {
if (code !== null) { if (code !== null) {
process.exit(code) process.exit(code)
...@@ -88,18 +112,20 @@ const startProcess = () => { ...@@ -88,18 +112,20 @@ const startProcess = () => {
let proc = startProcess() let proc = startProcess()
const wrapper = () => { function wrapper () {
if (proc) { if (proc) {
proc.kill() proc.kill()
} }
} }
process.on('SIGINT', wrapper) process.on('SIGINT', wrapper)
process.on('SIGTERM', wrapper) process.on('SIGTERM', wrapper)
process.on('exit', wrapper) process.on('exit', wrapper)
if (cmd === 'dev') { if (command === 'dev') {
const {CONFIG_FILE} = require('next-server/constants')
const {watchFile} = require('fs') const {watchFile} = require('fs')
watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur, prev) => { watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur: any, prev: any) => {
if (cur.size > 0 || prev.size > 0) { if (cur.size > 0 || prev.size > 0) {
console.log(`\n> Found a change in ${CONFIG_FILE}, restarting the server...`) console.log(`\n> Found a change in ${CONFIG_FILE}, restarting the server...`)
// Don't listen to 'close' now since otherwise parent gets killed by listener // Don't listen to 'close' now since otherwise parent gets killed by listener
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
"@babel/runtime-corejs2": "7.1.2", "@babel/runtime-corejs2": "7.1.2",
"@babel/template": "7.1.2", "@babel/template": "7.1.2",
"ansi-html": "0.0.7", "ansi-html": "0.0.7",
"arg": "3.0.0",
"async-sema": "^2.1.4", "async-sema": "^2.1.4",
"autodll-webpack-plugin": "0.4.2", "autodll-webpack-plugin": "0.4.2",
"babel-core": "7.0.0-bridge.0", "babel-core": "7.0.0-bridge.0",
...@@ -68,7 +69,6 @@ ...@@ -68,7 +69,6 @@
"http-status": "1.0.1", "http-status": "1.0.1",
"launch-editor": "2.2.1", "launch-editor": "2.2.1",
"loader-utils": "1.1.0", "loader-utils": "1.1.0",
"minimist": "1.2.0",
"mkdirp-then": "1.2.0", "mkdirp-then": "1.2.0",
"nanoid": "1.2.1", "nanoid": "1.2.1",
"next-server": "^7.0.2-canary.43", "next-server": "^7.0.2-canary.43",
...@@ -103,6 +103,7 @@ ...@@ -103,6 +103,7 @@
"@types/babel__generator": "7.0.1", "@types/babel__generator": "7.0.1",
"@types/babel__template": "7.0.1", "@types/babel__template": "7.0.1",
"@types/babel__traverse": "7.0.3", "@types/babel__traverse": "7.0.3",
"@types/cross-spawn": "6.0.0",
"@types/etag": "1.8.0", "@types/etag": "1.8.0",
"@types/fresh": "0.5.0", "@types/fresh": "0.5.0",
"@types/nanoid": "1.2.0", "@types/nanoid": "1.2.0",
......
...@@ -6,11 +6,13 @@ try { ...@@ -6,11 +6,13 @@ try {
module.exports = function (task) { module.exports = function (task) {
task.plugin('typescript', { every: true }, function * (file, options) { task.plugin('typescript', { every: true }, function * (file, options) {
const {stripExtension} = options
const opts = { const opts = {
fileName: file.base, fileName: file.base,
compilerOptions: { compilerOptions: {
...config.compilerOptions, ...config.compilerOptions,
...options ...options,
stripExtension: undefined // since it's an option of the typescript taskr plugin
} }
} }
...@@ -19,7 +21,7 @@ try { ...@@ -19,7 +21,7 @@ try {
if (ext) { if (ext) {
// Replace `.ts` with `.js` // Replace `.ts` with `.js`
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i') const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i')
file.base = file.base.replace(extRegex, '.js') file.base = file.base.replace(extRegex, stripExtension ? '' : '.js')
} }
// compile output // compile output
...@@ -35,7 +37,7 @@ try { ...@@ -35,7 +37,7 @@ try {
} }
// update file's data // update file's data
file.data = Buffer.from(result.outputText, 'utf8') file.data = Buffer.from(result.outputText.replace(/process\.env\.NEXT_VERSION/, `"${require('./package.json').version}"`), 'utf8')
}) })
} }
} catch (err) { } catch (err) {
......
...@@ -5,7 +5,7 @@ export async function compile (task) { ...@@ -5,7 +5,7 @@ export async function compile (task) {
} }
export async function bin (task, opts) { export async function bin (task, opts) {
await task.source(opts.src || 'bin/*').typescript({module: 'commonjs'}).target('dist/bin', {mode: '0755'}) await task.source(opts.src || 'bin/*').typescript({module: 'commonjs', stripExtension: true}).target('dist/bin', {mode: '0755'})
notify('Compiled binaries') notify('Compiled binaries')
} }
......
declare module '@babel/plugin-transform-modules-commonjs'; declare module '@babel/plugin-transform-modules-commonjs';
declare module 'next-server/next-config'; declare module 'next-server/next-config';
declare module 'next-server/constants'; declare module 'next-server/constants';
declare module 'arg' {
function arg<T extends arg.Spec>(spec: T, options?: {argv?: string[], permissive?: boolean}): arg.Result<T>;
namespace arg {
export type Handler = (value: string) => any;
export interface Spec {
[key: string]: string | Handler | [Handler];
}
export type Result<T extends Spec> = { _: string[] } & {
[K in keyof T]: T[K] extends string
? never
: T[K] extends Handler
? ReturnType<T[K]>
: T[K] extends [Handler]
? Array<ReturnType<T[K][0]>>
: never
};
}
export = arg;
}
\ No newline at end of file
export default () => 'test'
/* eslint-env jest */
/* global jasmine */
import {
runNextCommand,
runNextCommandDev,
findPort
} from 'next-test-utils'
import {join} from 'path'
import pkg from 'next/package'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
const dir = join(__dirname, '..')
describe('CLI Usage', () => {
describe('no command', () => {
test('--help', async () => {
const help = await runNextCommand(['--help'], {
stdout: true
})
expect(help.stdout).toMatch(/Usage/)
})
test('-h', async () => {
const help = await runNextCommand(['-h'], {
stdout: true
})
expect(help.stdout).toMatch(/Usage/)
})
test('--version', async () => {
const output = await runNextCommand(['--version'], {
stdout: true
})
expect(output.stdout).toMatch(new RegExp(`Next\\.js v${pkg.version.replace(/\./g, '\\.')}`))
})
test('-v', async () => {
const output = await runNextCommand(['--version'], {
stdout: true
})
expect(output.stdout).toMatch(new RegExp(`Next\\.js v${pkg.version.replace(/\./g, '\\.')}`))
})
test('--inspect', async () => {
const output = await runNextCommand(['build', dir, '--inspect'], {
stdout: true
})
expect(output.stdout).toMatch(/The `--inspect` option is deprecated/)
})
test('--node-args', async () => {
const output = await runNextCommand(['build', dir, '--node-args=--inspect'], {
stdout: true
})
expect(output.stdout).toMatch(/Passing arguments to Node\.js: "--inspect"/)
})
})
describe('build', () => {
test('--help', async () => {
const help = await runNextCommand(['build', '--help'], {
stdout: true
})
expect(help.stdout).toMatch(/Compiles the application for production deployment/)
})
test('-h', async () => {
const help = await runNextCommand(['build', '-h'], {
stdout: true
})
expect(help.stdout).toMatch(/Compiles the application for production deployment/)
})
})
describe('dev', () => {
test('--help', async () => {
const help = await runNextCommand(['dev', '--help'], {
stdout: true
})
expect(help.stdout).toMatch(/Starts the application in development mode/)
})
test('-h', async () => {
const help = await runNextCommand(['dev', '-h'], {
stdout: true
})
expect(help.stdout).toMatch(/Starts the application in development mode/)
})
test('custom directory', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '--port', port], true)
expect(output).toMatch(/Ready on/)
})
test('--port', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '--port', port], true)
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})
test('-p', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '-p', port], true)
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})
test('--hostname', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '--hostname', '0.0.0.0', '--port', port], true)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
})
test('-H', async () => {
const port = await findPort()
const output = await runNextCommandDev([dir, '-H', '0.0.0.0', '--port', port], true)
expect(output).toMatch(new RegExp(`http://0.0.0.0:${port}`))
})
})
describe('start', () => {
test('--help', async () => {
const help = await runNextCommand(['start', '--help'], {
stdout: true
})
expect(help.stdout).toMatch(/Starts the application in production mode/)
})
test('-h', async () => {
const help = await runNextCommand(['start', '-h'], {
stdout: true
})
expect(help.stdout).toMatch(/Starts the application in production mode/)
})
})
describe('export', () => {
test('--help', async () => {
const help = await runNextCommand(['export', '--help'], {
stdout: true
})
expect(help.stdout).toMatch(/Exports the application/)
})
test('-h', async () => {
const help = await runNextCommand(['export', '-h'], {
stdout: true
})
expect(help.stdout).toMatch(/Exports the application/)
})
})
})
...@@ -4,7 +4,7 @@ import http from 'http' ...@@ -4,7 +4,7 @@ import http from 'http'
import express from 'express' import express from 'express'
import path from 'path' import path from 'path'
import getPort from 'get-port' import getPort from 'get-port'
import { spawn } from 'child_process' import spawn from 'cross-spawn'
import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs' import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs'
import fkill from 'fkill' import fkill from 'fkill'
...@@ -12,13 +12,9 @@ import fkill from 'fkill' ...@@ -12,13 +12,9 @@ import fkill from 'fkill'
// This is done so that requiring from `next` works. // This is done so that requiring from `next` works.
// The reason we don't import the relative path `../../dist/<etc>` is that it would lead to inconsistent module singletons // The reason we don't import the relative path `../../dist/<etc>` is that it would lead to inconsistent module singletons
import server from 'next/dist/server/next' import server from 'next/dist/server/next'
import build from 'next/dist/build'
import _export from 'next/dist/export'
import _pkg from 'next/package.json' import _pkg from 'next/package.json'
export const nextServer = server export const nextServer = server
export const nextBuild = build
export const nextExport = _export
export const pkg = _pkg export const pkg = _pkg
export function initNextServerScript (scriptPath, successRegexp, env) { export function initNextServerScript (scriptPath, successRegexp, env) {
...@@ -69,16 +65,40 @@ export function findPort () { ...@@ -69,16 +65,40 @@ export function findPort () {
return getPort() return getPort()
} }
// Launch the app in dev mode. export function runNextCommand (argv, options = {}) {
export function launchApp (dir, port) { const cwd = path.dirname(require.resolve('next/package'))
return new Promise((resolve, reject) => {
console.log(`Running command "next ${argv.join(' ')}"`)
const instance = spawn('node', ['dist/bin/next', ...argv], { cwd, stdio: options.stdout ? ['ignore', 'pipe', 'ignore'] : 'inherit' })
let stdoutOutput = ''
if (options.stdout) {
instance.stdout.on('data', function (chunk) {
stdoutOutput += chunk
})
}
instance.on('close', () => {
resolve({
stdout: stdoutOutput
})
})
instance.on('error', (err) => {
reject(err)
})
})
}
export function runNextCommandDev (argv, stdOut) {
const cwd = path.dirname(require.resolve('next/package')) const cwd = path.dirname(require.resolve('next/package'))
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instance = spawn('node', ['dist/bin/next', dir, '-p', port], { cwd }) const instance = spawn('node', ['dist/bin/next', ...argv], { cwd })
function handleStdout (data) { function handleStdout (data) {
const message = data.toString() const message = data.toString()
if (/> Ready on/.test(message)) { if (/> Ready on/.test(message)) {
resolve(instance) resolve(stdOut ? message : instance)
} }
process.stdout.write(message) process.stdout.write(message)
} }
...@@ -101,6 +121,19 @@ export function launchApp (dir, port) { ...@@ -101,6 +121,19 @@ export function launchApp (dir, port) {
}) })
} }
// Launch the app in dev mode.
export function launchApp (dir, port) {
return runNextCommandDev([dir, '-p', port])
}
export function nextBuild (dir) {
return runNextCommand(['build', dir])
}
export function nextExport (dir, {outdir}) {
return runNextCommand(['export', dir, '--outdir', outdir])
}
// Kill a launched app // Kill a launched app
export async function killApp (instance) { export async function killApp (instance) {
await fkill(instance.pid) await fkill(instance.pid)
......
...@@ -1378,6 +1378,13 @@ ...@@ -1378,6 +1378,13 @@
dependencies: dependencies:
"@babel/types" "^7.0.0" "@babel/types" "^7.0.0"
"@types/cross-spawn@6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@types/cross-spawn/-/cross-spawn-6.0.0.tgz#320aaf1d1a12979f1b84fe7a5590a7e860bf3a80"
integrity sha512-evp2ZGsFw9YKprDbg8ySgC9NA15g3YgiI8ANkGmKKvvi0P2aDGYLPxQIC5qfeKNUOe3TjABVGuah6omPRpIYhg==
dependencies:
"@types/node" "*"
"@types/etag@1.8.0": "@types/etag@1.8.0":
version "1.8.0" version "1.8.0"
resolved "https://registry.yarnpkg.com/@types/etag/-/etag-1.8.0.tgz#37f0b1f3ea46da7ae319bbedb607e375b4c99f7e" resolved "https://registry.yarnpkg.com/@types/etag/-/etag-1.8.0.tgz#37f0b1f3ea46da7ae319bbedb607e375b4c99f7e"
...@@ -1900,6 +1907,11 @@ are-we-there-yet@~1.1.2: ...@@ -1900,6 +1907,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0" delegates "^1.0.0"
readable-stream "^2.0.6" readable-stream "^2.0.6"
arg@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/arg/-/arg-3.0.0.tgz#386c20035dfbeb13e280ca8a1e6868aa17942def"
integrity sha512-C5Scb477yHhNck9AFzW5RwAzS2Eqn0HR+Fv0pmcZBXBT8g/g7OOuZTr0upVSSUGWZQH+XWdAKIw2OfC86EuggQ==
argparse@^1.0.7: argparse@^1.0.7:
version "1.0.10" version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
...@@ -7530,7 +7542,7 @@ minimist@0.0.8: ...@@ -7530,7 +7542,7 @@ minimist@0.0.8:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=
minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0: minimist@^1.1.0, minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ= integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment