Skip to content

fix: support generator/index.js in local presets #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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