Skip to content

Commit

Permalink
feat(dev): auto process.env.PORT
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Nov 20, 2018
1 parent 390995f commit c6adde5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
21 changes: 21 additions & 0 deletions packages/@moonreach/nodepack/src/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ module.exports = (api, options) => {
api.registerCommand('dev', {
description: 'Build and live-reload the app',
usage: 'nodepack dev [entry]',
options: {
'-p, --port [port]': 'Specify a default port for process.env.PORT (it may automatically change if not available)',
},
}, async (args) => {
const path = require('path')
const { info, error, chalk } = require('@moonreach/nodepack-utils')

info(chalk.blue('Preparing development pack...'))

// Entry
const { getDefaultEntry } = require('../util/defaultEntry.js')
options.entry = getDefaultEntry(api, options, args)

const moreEnv = {}

// Default port
if (!process.env.PORT || args.port) {
const { getDefaultPort } = require('../util/defaultPort')
const port = await getDefaultPort(api, options, args)
moreEnv.PORT = port
if (api.service.env === 'development') {
info(chalk.blue(`\`process.env.PORT\` has been set to ${port}`))
}
}

// Build
const webpack = require('webpack')
const webpackConfig = await api.resolveWebpackConfig()
const execa = require('execa')
Expand All @@ -32,6 +49,7 @@ module.exports = (api, options) => {
if (err) {
error(err)
} else {
// Kill previous process
if (child) {
child.kill()
}
Expand All @@ -45,15 +63,18 @@ module.exports = (api, options) => {
info(chalk.blue('App starting...'))
}

// Built entry file
const file = api.resolve(path.join(webpackConfig.output.path, 'app.js'))

// Spawn child process
child = execa('node', [
file,
], {
stdio: ['inherit', 'inherit', 'inherit'],
cwd: api.getCwd(),
cleanup: true,
shell: false,
env: moreEnv,
})

child.on('error', err => {
Expand Down
1 change: 1 addition & 0 deletions packages/@moonreach/nodepack/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.defaultOptions = function () {
externals: true,
parallel: hasMultipleCores(),
transpileDependencies: [],
defaultPort: 4000,
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/@moonreach/nodepack/src/util/defaultPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @typedef {import('../lib/PackPluginAPI.js')} PackPluginApi */
/** @typedef {import('../lib/options.js').ProjectOptions} ProjectOptions */

/**
* @param {PackPluginApi} api
* @param {ProjectOptions} options
*/
exports.getDefaultPort = async function (api, options, args) {
const portfinder = require('portfinder')
const result = await portfinder.getPortPromise({
port: args.port || options.defaultPort,
})
return result
}
29 changes: 19 additions & 10 deletions packages/@moonreach/nodepack/types/options.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import Config from 'webpack-chain'

export interface ProjectOptions {
/** folder output for prod build */
/** Folder output for prod build */
outputDir?: string
/** folder containing source */
/**
* Folder containing source.
* By default will be aliased to `@/`
* (for example, `@/lib/foo.js` will be resolved as `src/lib/foo.js`).
*/
srcDir?: string
/** entry file */
/** Entry file (relative to project root) */
entry?: string
/** enable sourcemaps in production build */
/** Enable sourcemaps in production build (can slow down build) */
productionSourceMap?: boolean
/** webpack externals */
/** Webpack externals packages */
externals?: any
/** whitelist option for webpack-node-externals */
/** Whitelist option for webpack-node-externals */
nodeExternalsWhitelist?: any
/** modify webpack config with webpack-chain */
/** Modify webpack config with webpack-chain */
chainWebpack?: (config: Config) => void
/** enable parallel compilation */
/** Enable parallel compilation */
parallel?: boolean
/** force transpile node_modules packages with babel */
/** Force transpile `node_modules` packages with babel */
transpileDependencies?: Array.<string | RegExp>
/** options for 3rd-party plugins */
/**
* Default port for `process.env.PORT` if it is not defined.
* It will change to a free port automatically if it is not available.
*/
defaultPort?: number
/** Options for 3rd-party plugins */
pluginOptions?: any
}

0 comments on commit c6adde5

Please sign in to comment.