Skip to content

Commit

Permalink
use execa.shell so npm and build commands get an env (#173)
Browse files Browse the repository at this point in the history
This provides the local env to webpack which is needed for some webpack plugins. Like anything that wants to run commands, read env vars, etc. This also lets people use proxies with npm.

Smoke tested a build with `webpack-shell-plugin` and a `shep new`
  • Loading branch information
reconbot committed Jan 25, 2017
1 parent 1c5d6fb commit 3c8d0c7
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/new/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ function createFiles ({ path, arn, region }) {
}

function npmInstall ({ path }) {
return exec('npm', ['install'], { cwd: path })
return exec('npm install', { cwd: path })
}

11 changes: 3 additions & 8 deletions src/util/build-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import exec from './modules/exec'
import { pkg } from './load'

export default function (PATTERN, NODE_ENV) {
const buildCommand = pkg().shep && pkg().shep.buildCommand ? pkg().shep.buildCommand : 'webpack --bail'
const [command, args] = parseBuildCommand(buildCommand)
return exec(command, args, { env: { PATTERN, NODE_ENV } })
const { shep } = pkg()
const buildCommand = shep && shep.buildCommand || 'webpack --bail'
return exec(buildCommand, { env: { ...process.env, PATTERN, NODE_ENV } })
.catch({ code: 'ENOENT' }, (e) => {
console.warn('No locally installed webpack found. Verify that webpack is installed')
throw e
})
}

function parseBuildCommand (command) {
const [com, ...args] = command.split(' ')
return [com, args]
}
2 changes: 1 addition & 1 deletion src/util/modules/exec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import execa from 'execa'
import Promise from 'bluebird'
export default Promise.method(execa)
export default Promise.method(execa.shell)
6 changes: 2 additions & 4 deletions test/build/index-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import test from 'ava'
import { exec, didExec } from '../helpers/exec'
import td from '../helpers/testdouble'

const command = 'custom-build'
const args = ['--cool-flag', '-x', '6']
const buildCommand = `${command} ${args.join(' ')}`
const buildCommand = 'custom-build --cool-flag -x 6'

const load = td.replace('../../src/util/load')
td.when(load.pkg()).thenReturn({ shep: { buildCommand } })
Expand All @@ -16,4 +14,4 @@ test.before(() => {
return shep.build({ quiet: true })
})

test(didExec, command, args)
test(didExec, buildCommand)
2 changes: 1 addition & 1 deletion test/build/index-no-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import td from '../helpers/testdouble'
let error = new Error()
error.code = 'ENOENT'

td.when(exec('webpack', ['--bail']), { ignoreExtraArgs: true }).thenReject(error)
td.when(exec('webpack --bail'), { ignoreExtraArgs: true }).thenReject(error)
td.replace(console, 'warn')

test('Logs to console when no webpack found', async (t) => {
Expand Down
4 changes: 2 additions & 2 deletions test/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import test from 'ava'
import { exec, didExec } from '../helpers/exec'
import td from '../helpers/testdouble'

td.when(exec('webpack', ['--bail']), { ignoreExtraArgs: true }).thenReturn(Promise.resolve())
td.when(exec('webpack --bail'), { ignoreExtraArgs: true }).thenReturn(Promise.resolve())

test.before(() => {
const shep = require('../../src')
return shep.build({ quiet: true })
})

test(didExec, 'webpack', ['--bail'])
test(didExec, 'webpack --bail')
4 changes: 2 additions & 2 deletions test/helpers/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import td from 'testdouble'

const exec = td.replace('../../src/util/modules/exec')

function didExec (t, cmd, args, options = {}) {
td.verify(exec(cmd, args, td.matchers.contains(options)))
function didExec (t, cmd, options = {}) {
td.verify(exec(cmd, td.matchers.contains(options)))
}

didExec.title = (providedTitle, cmd, subcmd) => `Executed ${cmd} ${subcmd || ''}`
Expand Down
2 changes: 1 addition & 1 deletion test/new/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ test(wroteFile, `${path}/.gitignore`)
test(wroteFile, `${path}/README.md`)
test(wroteFile, `${path}/webpack.config.js`)

test(didExec, 'npm', ['install'])
test(didExec, 'npm install')

0 comments on commit 3c8d0c7

Please sign in to comment.