Skip to content

Commit

Permalink
fix: support generator/index.js in local presets (#2263)
Browse files Browse the repository at this point in the history
fixes #2172
  • Loading branch information
haoqunjiang authored and yyx990803 committed Aug 20, 2018
1 parent b81d11e commit ecb8c18
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (api, options) => {
api.render('./template', options)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= ok %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": {
"@vue/cli-plugin-babel": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = [{
type: 'confirm',
name: 'ok',
message: 'Are you ok?'
}]
26 changes: 26 additions & 0 deletions packages/@vue/cli/__tests__/preset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ test('fetching local preset with prompts and generator', async () => {
const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@vue/cli-plugin-babel')
})

test('should recognize generator/index.js in a local preset directory', async () => {
const cwd = path.resolve(__dirname, '../../../test')
const name = 'test-preset-template'

expectPrompts([{
message: 'Are you ok',
confirm: true
}])

await create(
name,
{
force: true,
git: false,
cwd,
preset: path.resolve(__dirname, './mock-preset-with-template')
}
)

const testFile = await fs.readFile(path.resolve(cwd, name, 'test.js'), 'utf-8')
expect(testFile).toBe('true\n')

const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@vue/cli-plugin-babel')
})
6 changes: 3 additions & 3 deletions packages/@vue/cli/lib/util/loadPresetFromDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = async function loadPresetFromDir (dir) {
}
const preset = await fs.readJson(presetPath)

// if the preset dir contains generator.js, we will inject it as a hidden
// if the preset dir contains generator.js or generator/index.js, we will inject it as a hidden
// plugin so it will be invoked by the generator.
const generatorPath = path.join(dir, 'generator.js')
if (fs.existsSync(generatorPath)) {
const hasGenerator = fs.existsSync(path.join(dir, 'generator.js')) || fs.existsSync(path.join(dir, 'generator/index.js'))
if (hasGenerator) {
(preset.plugins || (preset.plugins = {}))[dir.replace(/[\/]$/, '')] = {
_isPreset: true,
prompts: true
Expand Down

0 comments on commit ecb8c18

Please sign in to comment.