Skip to content

Commit

Permalink
feat: allow saving multiple presets
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 28, 2018
1 parent 9856549 commit f372f55
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 191 deletions.
2 changes: 2 additions & 0 deletions __mocks__/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ exports.prompt = prompts => {
const a = pendingAssertions[i - skipped]
if (!a) {
console.error(`no matching assertion for prompt:`, prompt)
console.log(prompts)
console.log(pendingAssertions)
}

if (a.message) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@vue/cli-plugin-pwa/__tests__/pwaPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ jest.setTimeout(30000)
const path = require('path')
const portfinder = require('portfinder')
const { createServer } = require('http-server')
const { defaults } = require('@vue/cli/lib/options')
const { defaultPreset } = require('@vue/cli/lib/options')
const create = require('@vue/cli-test-utils/createTestProject')
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')

let server, browser
test('pwa', async () => {
// it's ok to mutate here since jest loads each test in a separate vm
defaults.plugins['@vue/cli-plugin-pwa'] = {}
const project = await create('pwa-build', defaults)
defaultPreset.plugins['@vue/cli-plugin-pwa'] = {}
const project = await create('pwa-build', defaultPreset)
expect(project.has('src/registerServiceWorker.js')).toBe(true)

const { stdout } = await project.run('vue-cli-service build')
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-service/__tests__/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ jest.setTimeout(30000)
const path = require('path')
const portfinder = require('portfinder')
const { createServer } = require('http-server')
const { defaults } = require('@vue/cli/lib/options')
const { defaultPreset } = require('@vue/cli/lib/options')
const create = require('@vue/cli-test-utils/createTestProject')
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')

let server, browser, page
test('build', async () => {
const project = await create('e2e-build', defaults)
const project = await create('e2e-build', defaultPreset)

// test public copy
project.write('public/foo.js', '1')
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-service/__tests__/buildDLL.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ jest.setTimeout(30000)
const path = require('path')
const portfinder = require('portfinder')
const { createServer } = require('http-server')
const { defaults } = require('@vue/cli/lib/options')
const { defaultPreset } = require('@vue/cli/lib/options')
const create = require('@vue/cli-test-utils/createTestProject')
const launchPuppeteer = require('@vue/cli-test-utils/launchPuppeteer')

let server, browser, page
test('build with DLL', async () => {
const project = await create('e2e-build-dll', Object.assign({}, defaults, {
const project = await create('e2e-build-dll', Object.assign({}, defaultPreset, {
router: true,
vuex: true
}))
Expand Down
6 changes: 3 additions & 3 deletions packages/@vue/cli-service/__tests__/serve.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
jest.setTimeout(30000)

const { defaults } = require('@vue/cli/lib/options')
const { defaultPreset } = require('@vue/cli/lib/options')
const create = require('@vue/cli-test-utils/createTestProject')
const serve = require('@vue/cli-test-utils/serveWithPuppeteer')

const sleep = n => new Promise(resolve => setTimeout(resolve, n))

test('serve', async () => {
const project = await create('e2e-serve', defaults)
const project = await create('e2e-serve', defaultPreset)

await serve(
() => project.run('vue-cli-service serve'),
Expand All @@ -26,7 +26,7 @@ test('serve', async () => {
})

test('serve with router', async () => {
const project = await create('e2e-serve-router', Object.assign({}, defaults, {
const project = await create('e2e-serve-router', Object.assign({}, defaultPreset, {
router: true
}))

Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-shared-utils/lib/_silence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function silence (exports) {
module.exports = function silence (logName, exports) {
const logs = {}
Object.keys(exports).forEach(key => {
if (key !== 'error') {
Expand All @@ -8,5 +8,5 @@ module.exports = function silence (exports) {
}
}
})
exports.logs = logs
exports[logName] = logs
}
2 changes: 1 addition & 1 deletion packages/@vue/cli-shared-utils/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ exports.clearConsole = title => {

// silent all logs except errors during tests and keep record
if (process.env.VUE_CLI_TEST) {
require('./_silence')(exports)
require('./_silence')('logs', exports)
}
2 changes: 1 addition & 1 deletion packages/@vue/cli-shared-utils/lib/spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ exports.resumeSpinner = () => {

// silent all logs except errors during tests and keep record
if (process.env.VUE_CLI_TEST) {
require('./_silence')(exports)
require('./_silence')('spinner', exports)
}
8 changes: 6 additions & 2 deletions packages/@vue/cli-shared-utils/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ const { error } = require('./logger')
// proxy to joi for option validation
exports.createSchema = fn => fn(joi)

exports.validate = (obj, schema, options = {}) => {
exports.validate = (obj, schema, options = {}, noExit) => {
joi.validate(obj, schema, options, err => {
if (err) {
error(`vue-cli options validation failed:\n` + err.message)
process.exit(1)
if (!noExit) {
process.exit(1)
} else {
throw err
}
}
})
}
22 changes: 12 additions & 10 deletions packages/@vue/cli-test-utils/assertPromptModule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// using this requires mocking fs & inquirer

const Creator = require('@vue/cli/lib/Creator')
const { loadOptions } = require('@vue/cli/lib/options')
const { expectPrompts } = require('inquirer') // from mock

module.exports = async function assertPromptModule (
Expand All @@ -13,7 +14,7 @@ module.exports = async function assertPromptModule (
if (opts.plguinsOnly) {
expectedPrompts.unshift(
{
message: 'project creation mode',
message: 'Please pick a preset',
choose: 1
}
)
Expand All @@ -23,23 +24,24 @@ module.exports = async function assertPromptModule (
choose: 1 // package.json
},
{
message: 'package manager',
choose: 0 // yarn
},
{
message: 'Save the preferences',
message: 'Save this as a preset',
confirm: false
}
)
if (!loadOptions().packageManager) {
expectedPrompts.push({
message: 'package manager',
choose: 0 // yarn
})
}
}

expectPrompts(expectedPrompts)
const creator = new Creator('test', '/', [].concat(module))
const options = await creator.promptAndResolveOptions()
const preset = await creator.promptAndResolvePreset()

if (opts.plguinsOnly) {
delete options.packageManager
delete options.useConfigFiles
delete preset.useConfigFiles
}
expect(options).toEqual(expectedOptions)
expect(preset).toEqual(expectedOptions)
}
12 changes: 3 additions & 9 deletions packages/@vue/cli-test-utils/createTestProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const mkdirp = promisify(require('mkdirp'))

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

config = Object.assign({
packageManager: 'yarn',
useTaobaoRegistry: false,
plugins: {}
}, config)

const projectRoot = path.resolve(cwd, name)

const read = file => {
Expand Down Expand Up @@ -46,8 +40,8 @@ module.exports = function createTestProject (name, config, cwd) {
'create',
name,
'--force',
'--config',
JSON.stringify(config)
'--inlinePreset',
JSON.stringify(preset)
]

const options = {
Expand Down
25 changes: 14 additions & 11 deletions packages/@vue/cli/__tests__/Creator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ const assertPromptModule = require('@vue/cli-test-utils/assertPromptModule')
test('default', async () => {
const epxectedPrompts = [
{
message: 'project creation mode',
message: 'pick a preset',
choices: [
'Zero-config',
'default',
'Manually select'
],
choose: 0
},
{
message: 'package manager',
choices: ['Yarn', 'NPM'],
choose: 0
}
]
assertPromptModule([], epxectedPrompts, defaults)
await assertPromptModule([], epxectedPrompts, defaults.presets.default)
})

test('manual + PromptModuleAPI', async () => {
Expand Down Expand Up @@ -73,18 +78,16 @@ test('manual + PromptModuleAPI', async () => {
choose: 0
},
{
message: 'package manager',
choices: ['Yarn', 'NPM'],
choose: 0
message: 'Save this as a preset',
confirm: true
},
{
message: 'Save the preferences',
confirm: true
message: 'Save preset as',
input: 'test'
}
]

const expectedOptions = {
packageManager: 'yarn',
useConfigFiles: true,
plugins: {
bar: {},
Expand All @@ -98,8 +101,8 @@ test('manual + PromptModuleAPI', async () => {
const expectedPromptsForSaved = [
{
choices: [
'Use previously saved',
'Zero-config',
'test',
'default',
'Manually'
],
choose: 0
Expand Down
6 changes: 1 addition & 5 deletions packages/@vue/cli/__tests__/Generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,7 @@ test('extract config files', async () => {
},
jest: {
foo: 'bar'
},
browserslist: [
'>=ie9'
]
}
}

const generator = new Generator('/', {}, [
Expand All @@ -345,6 +342,5 @@ test('extract config files', async () => {
expect(fs.readFileSync('/.babelrc', 'utf-8')).toMatch(json(configs.babel))
expect(fs.readFileSync('/.postcssrc', 'utf-8')).toMatch(json(configs.postcss))
expect(fs.readFileSync('/.eslintrc', 'utf-8')).toMatch(json(configs.eslintConfig))
expect(fs.readFileSync('/.browserslistrc', 'utf-8')).toMatch(configs.browserslist.join('\n'))
expect(fs.readFileSync('/jest.config.js', 'utf-8')).toMatch(`module.exports = {\n foo: 'bar'\n}`)
})
48 changes: 26 additions & 22 deletions packages/@vue/cli/__tests__/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ const fs = require('fs')
const {
rcPath,
loadOptions,
saveOptions
saveOptions,
savePreset
} = require('../lib/options')

test('load options', () => {
expect(loadOptions()).toEqual({})
fs.writeFileSync(rcPath, JSON.stringify({
plugins: {}
presets: {}
}, null, 2))
expect(loadOptions()).toEqual({
plugins: {}
presets: {}
})
})

Expand All @@ -22,49 +23,52 @@ test('should not save unknown fields', () => {
foo: 'bar'
})
expect(loadOptions()).toEqual({
plugins: {}
presets: {}
})
})

test('save options (merge)', () => {
test('save options', () => {
// partial
saveOptions({
packageManager: 'yarn'
})
expect(loadOptions()).toEqual({
packageManager: 'yarn',
plugins: {}
presets: {}
})

// replace
saveOptions({
plugins: {
presets: {
foo: { a: 1 }
}
})
expect(loadOptions()).toEqual({
packageManager: 'yarn',
plugins: {
presets: {
foo: { a: 1 }
}
})
})

// shallow save should replace fields
saveOptions({
plugins: {
bar: { b: 2 }
test('save preset', () => {
savePreset('bar', { a: 2 })
expect(loadOptions()).toEqual({
packageManager: 'yarn',
presets: {
foo: { a: 1 },
bar: { a: 2 }
}
})

// should entirely replace presets
savePreset('foo', { c: 3 })
savePreset('bar', { d: 4 })
expect(loadOptions()).toEqual({
packageManager: 'yarn',
plugins: {
bar: { b: 2 }
presets: {
foo: { c: 3 },
bar: { d: 4 }
}
})
})

test('save options (replace)', () => {
const toSave = {
foo: 'bar'
}
saveOptions(toSave, true)
expect(loadOptions()).toEqual(toSave)
})
4 changes: 2 additions & 2 deletions packages/@vue/cli/bin/vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ program
program
.command('create <app-name>')
.description('create a new project powered by vue-cli-service')
.option('-s, --saved', 'Skip prompts and use saved config')
.option('-p, --preset <presetName>', 'Skip prompts and use saved preset')
.option('-d, --default', 'Skip prompts and use default config')
.option('-c, --config <json>', 'Skip prompts and use inline JSON string as config')
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
.option('-r, --registry <url>', 'Use specified NPM registry when installing dependencies')
.option('-m, --packageManager <command>', 'Use specified NPM client when installing dependencies')
.option('-f, --force', 'Overwrite target directory if it exists')
Expand Down
Loading

0 comments on commit f372f55

Please sign in to comment.