Skip to content

Commit

Permalink
use execa for cross-platform-ness
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 5, 2018
1 parent 42e0545 commit 4e6024b
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 96 deletions.
3 changes: 2 additions & 1 deletion __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { fs } = require('memfs')

// overwrite config path when fs is mocked
// overwrite config path and context when fs is mocked
process.env.VUE_CLI_CONTEXT = '/'
process.env.VUE_CLI_CONFIG_PATH = '/.vuerc'

module.exports = fs
6 changes: 3 additions & 3 deletions packages/@vue/cli-plugin-eslint/__tests__/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ jest.setTimeout(10000)

const create = require('@vue/cli-test-utils/createTestProjectWithOptions')

it('should work', async () => {
const { read, write, exec } = await create('eslint', {
test('should work', async () => {
const { read, write, run } = await create('eslint', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
Expand All @@ -18,7 +18,7 @@ it('should work', async () => {
// remove semicolons
await write('src/main.js', main.replace(/;/g, ''))
// lint
await exec('node ./node_modules/.bin/vue-cli-service lint')
const child = await run('vue-cli-service lint')
expect(await read('src/main.js')).toContain(';')

// TODO lint-on-commit
Expand Down
8 changes: 5 additions & 3 deletions packages/@vue/cli-plugin-eslint/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ module.exports = (api, { config, lintOn }) => {
api.extendPackage(pkg)

// lint & fix after create to ensure files adhere to chosen config
api.onCreateComplete(() => {
require('./lint')(api.resolve('.'), { silent: true })
})
if (config) {
api.onCreateComplete(() => {
require('./lint')(api.resolve('.'), { silent: true })
})
}
}
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module.exports = api => {
process.env.VUE_CLI_BABEL_TARGET_NODE = true
process.env.VUE_CLI_BABEL_TRANSPILE_MODULES = true

// TODO spawn jest w/ --config jest.config.js
// TODO execa jest w/ --config jest.config.js
})
}
1 change: 1 addition & 0 deletions packages/@vue/cli-plugin-unit-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"babel-jest": "^22.0.4",
"execa": "^0.8.0",
"jest": "^22.0.4",
"jest-serializer-vue": "^0.3.0",
"vue-jest": "^1.4.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const generateWithPlugin = require('@vue/cli-test-utils/generateWithPlugin')

it('base', async () => {
test('base', async () => {
const { pkg, files } = await generateWithPlugin([
{
id: 'unit-mocha-webpack',
Expand All @@ -21,7 +21,7 @@ it('base', async () => {
expect(files['test/unit/HelloWorld.spec.js']).toContain('// assert wrapper.text() equals msg')
})

it('chai', async () => {
test('chai', async () => {
const { pkg, files } = await generateWithPlugin([
{
id: 'unit-mocha-webpack-chai',
Expand All @@ -40,7 +40,7 @@ it('chai', async () => {
expect(spec).toContain(`expect(wrapper.text()).to.include(msg)`)
})

it('chai', async () => {
test('chai', async () => {
const { pkg, files } = await generateWithPlugin([
{
id: 'unit-mocha-webpack-chai',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ jest.setTimeout(20000)

const create = require('@vue/cli-test-utils/createTestProjectWithOptions')

it('should work', async () => {
const { exec } = await create('unit-mocha-webpack', {
test('should work', async () => {
const { run } = await create('unit-mocha-webpack', {
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-unit-mocha-webpack': {
assertionLibrary: 'chai'
}
}
})
await exec(`./node_modules/.bin/vue-cli-service test`)
await run(`vue-cli-service test`)
})
4 changes: 2 additions & 2 deletions packages/@vue/cli-plugin-unit-mocha-webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = api => {
// for @vue/babel-preset-app
process.env.VUE_CLI_BABEL_TARGET_NODE = true
// start runner
const { spawn } = require('child_process')
const execa = require('execa')
const bin = require.resolve('mocha-webpack/bin/mocha-webpack')
const argv = rawArgv.concat([
'--recursive',
Expand All @@ -45,7 +45,7 @@ module.exports = api => {
])

return new Promise((resolve, reject) => {
const child = spawn(bin, argv, { stdio: 'inherit' })
const child = execa(bin, argv, { stdio: 'inherit' })
child.on('error', reject)
child.on('exit', code => {
if (code !== 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli-plugin-unit-mocha-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/cli-plugin-unit-mocha-webpack#readme",
"dependencies": {
"execa": "^0.8.0",
"jsdom": "^11.5.1",
"jsdom-global": "^3.0.2",
"mocha": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require('fs')
const path = require('path')
const debug = require('debug')
const chalk = require('chalk')
Expand Down Expand Up @@ -96,6 +95,7 @@ module.exports = class Service {
let command = this.commands[name]
if (!command && name) {
error(`command "${name}" does not exist.`)
process.exit(1)
}
if (!command || args.help) {
command = this.commands.help
Expand Down
22 changes: 11 additions & 11 deletions packages/@vue/cli-service/lib/__tests__/Service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeEach(() => {
mockPkg({})
})

it('env loading', () => {
test('env loading', () => {
fs.writeFileSync('/.env', `FOO=1\nBAR=2`)
fs.writeFileSync('/.env.local', `FOO=3\nBAZ=4`)
new Service('/', [])
Expand All @@ -23,7 +23,7 @@ it('env loading', () => {
expect(process.env.BAZ).toBe('4')
})

it('loading plugins from package.json', () => {
test('loading plugins from package.json', () => {
mockPkg({
devDependencies: {
'bar': '^1.0.0',
Expand All @@ -37,7 +37,7 @@ it('loading plugins from package.json', () => {
expect(service.plugins.some(({ id }) => id === 'bar')).toBe(false)
})

it('load project options from package.json', () => {
test('load project options from package.json', () => {
mockPkg({
vue: {
lintOn: 'save'
Expand All @@ -47,7 +47,7 @@ it('load project options from package.json', () => {
expect(service.projectOptions.lintOn).toBe('save')
})

it('load project options from vue.config.js', () => {
test('load project options from vue.config.js', () => {
process.env.VUE_CLI_SERVICE_CONFIG_PATH = 'mock-config'
mockPkg({
vue: {
Expand All @@ -59,7 +59,7 @@ it('load project options from vue.config.js', () => {
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
})

it('api: setMode', () => {
test('api: setMode', () => {
fs.writeFileSync('/.env.foo', `FOO=5\nBAR=6`)
fs.writeFileSync('/.env.foo.local', `FOO=7\nBAZ=8`)

Expand Down Expand Up @@ -89,7 +89,7 @@ it('api: setMode', () => {
expect(process.env.BABEL_ENV).toBe('test')
})

it('api: registerCommand', () => {
test('api: registerCommand', () => {
let args
const service = new Service('/', [{
id: 'test',
Expand All @@ -104,7 +104,7 @@ it('api: registerCommand', () => {
expect(args).toEqual({ _: [], n: 1 })
})

it('api: chainWebpack', () => {
test('api: chainWebpack', () => {
const service = new Service('/', [{
id: 'test',
apply: api => {
Expand All @@ -118,7 +118,7 @@ it('api: chainWebpack', () => {
expect(config.output.path).toBe('test-dist')
})

it('api: configureWebpack', () => {
test('api: configureWebpack', () => {
const service = new Service('/', [{
id: 'test',
apply: api => {
Expand All @@ -134,7 +134,7 @@ it('api: configureWebpack', () => {
expect(config.output.path).toBe('test-dist-2')
})

it('api: configureDevServer', () => {
test('api: configureDevServer', () => {
const cb = () => {}
const service = new Service('/', [{
id: 'test',
Expand All @@ -145,7 +145,7 @@ it('api: configureDevServer', () => {
expect(service.devServerConfigFns).toContain(cb)
})

it('api: resolve', () => {
test('api: resolve', () => {
new Service('/', [{
id: 'test',
apply: api => {
Expand All @@ -154,7 +154,7 @@ it('api: resolve', () => {
}])
})

it('api: hasPlugin', () => {
test('api: hasPlugin', () => {
new Service('/', [
{
id: 'vue-cli-plugin-foo',
Expand Down
64 changes: 30 additions & 34 deletions packages/@vue/cli-test-utils/createTestProjectWithOptions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const fs = require('fs')
const path = require('path')
const execa = require('execa')
const { promisify } = require('util')
const childProcess = require('child_process')
const cliBinPath = require.resolve('@vue/cli/bin/vue')
const { spawn } = childProcess

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const _exec = promisify(childProcess.exec)
const mkdirp = promisify(require('mkdirp'))

const exec = promisify(require('child_process').exec)

module.exports = function createTestProjectWithOptions (name, config, cwd) {
cwd = cwd || path.resolve(__dirname, '../../test')

Expand All @@ -19,45 +17,43 @@ module.exports = function createTestProjectWithOptions (name, config, cwd) {
plugins: {}
}, config)

const projectRoot = path.resolve(cwd, name)

const read = file => {
return readFile(path.resolve(cwd, name, file), 'utf-8')
return readFile(path.resolve(projectRoot, file), 'utf-8')
}

const write = (file, content) => {
const targetPath = path.resolve(cwd, name, file)
const targetPath = path.resolve(projectRoot, file)
const dir = path.dirname(targetPath)
return mkdirp(dir).then(() => writeFile(targetPath, content))
}

const exec = command => {
return _exec(command, { cwd: path.resolve(cwd, name) })
const run = (command, args) => {
if (!args) { [command, ...args] = command.split(/\s+/) }
return execa(command, args, { cwd: projectRoot }).then(({ stderr }) => {
if (stderr) console.error(stderr)
})
}

return new Promise((resolve, reject) => {
const child = spawn(cliBinPath, [
'create',
name,
'--force',
'--config',
JSON.stringify(config)
], {
cwd,
env: process.env,
stdio: 'inherit'
})
const cliBinPath = require.resolve('@vue/cli/bin/vue')

child.on('exit', code => {
if (code !== 0) {
reject(`cli creation failed with code ${code}`)
} else {
resolve({
read,
write,
exec
})
}
})
const args = [
'create',
name,
'--force',
'--config',
JSON.stringify(config)
]

const options = {
cwd,
stdio: 'inherit'
}

child.on('error', reject)
})
return execa(cliBinPath, args, options).then(() => ({
read,
write,
run
}))
}
3 changes: 3 additions & 0 deletions packages/@vue/cli-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"homepage": "https://github.com/vuejs/vue-cli/packages/@vue/cli-test-utils#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {
"execa": "^0.8.0"
}
}
11 changes: 7 additions & 4 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const chalk = require('chalk')
const debug = require('debug')
const execa = require('execa')
const resolve = require('resolve')
const inquirer = require('inquirer')
const Generator = require('./Generator')
Expand All @@ -10,7 +11,6 @@ const PromptModuleAPI = require('./PromptModuleAPI')
const writeFileTree = require('./util/writeFileTree')
const formatFeatures = require('./util/formatFeatures')
const setupDevProject = require('./util/setupDevProject')
const exec = require('util').promisify(require('child_process').exec)

const {
defaults,
Expand Down Expand Up @@ -48,7 +48,10 @@ module.exports = class Creator {
async create (cliOptions = {}) {
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
const { name, context, createCompleteCbs } = this
const run = command => exec(command, { cwd: context })
const run = (command, args) => {
if (!args) { [command, ...args] = command.split(/\s+/) }
return execa(command, args, { cwd: context })
}

let options
if (cliOptions.saved) {
Expand Down Expand Up @@ -132,8 +135,8 @@ module.exports = class Creator {
if (hasGit) {
await run('git add -A')
if (isTestOrDebug) {
await run('git config user.name "test"')
await run('git config user.email "test@test.com"')
await run('git', ['config', 'user.name', 'test'])
await run('git', ['config', 'user.email', 'test@test.com'])
}
await run(`git commit -m init`)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/__tests__/Creator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jest.mock('inquirer')
const { defaults } = require('../options')
const assertPromptModule = require('@vue/cli-test-utils/assertPromptModule')

it('default', async () => {
test('default', async () => {
const epxectedPrompts = [
{
message: 'project creation mode',
Expand All @@ -18,7 +18,7 @@ it('default', async () => {
assertPromptModule([], epxectedPrompts, defaults)
})

it('manual + PromptModuleAPI', async () => {
test('manual + PromptModuleAPI', async () => {
const mockModule = api => {
api.injectFeature({
name: 'Foo',
Expand Down
Loading

0 comments on commit 4e6024b

Please sign in to comment.