Skip to content

Commit b19864a

Browse files
Adding ComponentFixture
1 parent 54f3afc commit b19864a

File tree

3 files changed

+113
-63
lines changed

3 files changed

+113
-63
lines changed

generator/index.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { renameFiles, updateFile } = require('./fileHelper')
2+
const { updateExample } = ('./updateTemplateWithCompnentFixture')
23
const readmeUpdater = require('./readmeUpdater');
34
const licenseList = require('spdx-license-list/full');
45

@@ -22,11 +23,11 @@ function replaceInLicense(licenseTextTemplate, sourceText, newText) {
2223
.replace(new RegExp(`\\[${sourceText}\\]`), newText)
2324
}
2425

25-
module.exports = (api, { addBadges, addLicense, componentName, copyrightHolders, licenseName, useVueDoc, useVueStyleguidist }) => {
26+
module.exports = (api, { addBadges, addLicense, componentName, copyrightHolders, licenseName, useComponentFixture, useVueDoc, useVueStyleguidist }) => {
2627

2728
const useLint = api.hasPlugin('eslint')
2829
const packageName = api.generator.pkg.name
29-
const context = { addBadges, addLicense, componentName, licenseName, packageName, useLint, useVueDoc, useVueStyleguidist }
30+
const context = { addBadges, addLicense, componentName, licenseName, packageName, useComponentFixture, useLint, useVueDoc, useVueStyleguidist }
3031

3132
api.extendPackage({
3233
main: `dist/${packageName}.umd.js`,
@@ -72,33 +73,45 @@ module.exports = (api, { addBadges, addLicense, componentName, copyrightHolders,
7273
})
7374
}
7475

76+
if (useComponentFixture) {
77+
api.extendPackage({
78+
devDependencies: {
79+
'component-fixture': "^0.3.0"
80+
}
81+
})
82+
}
83+
7584
if (addLicense) {
7685
api.extendPackage({
77-
license : licenseName
86+
license: licenseName
7887
})
7988
}
8089

8190
api.render('./template')
8291

8392
api.postProcessFiles(files => {
84-
const hasTest = api.hasPlugin('unit-mocha') || api.hasPlugin('unit-jest')
93+
const hasTest = api.hasPlugin('unit-mocha') || api.hasPlugin('unit-jest');
8594
if (hasTest) {
86-
updateFile(files, 'tests/unit/HelloWorld.spec.js', content => content.replace(/HelloWorld/g, componentName))
95+
updateFile(files, 'tests/unit/HelloWorld.spec.js', content => content.replace(/HelloWorld/g, componentName));
8796
}
8897

89-
updateFile(files, 'README.md', content => readmeUpdater(content, context))
98+
updateFile(files, 'README.md', content => readmeUpdater(content, context));
99+
100+
if (useComponentFixture) {
101+
updateFile(files, 'src/App.vue', updateExample);
102+
}
90103

91104
const immutableFiles = ['src/components/HelloWorld.vue', 'src/index.js']
92105
renameFiles(files, /^src\//, 'example/', (file) => immutableFiles.indexOf(file) !== -1)
93106
renameFiles(files, /\/HelloWorld\./, `/${componentName}.`)
94107

95108
if (!addLicense) {
96-
return
109+
return;
97110
}
98111

99112
const licenseTextTemplate = licenseList[licenseName].licenseText;
100-
const year = new Date().getFullYear()
101-
const licenseText = replaceInLicense(licenseTextTemplate, 'year', year)
102-
files['LICENSE'] = replaceInLicense(licenseText, 'copyright holders', copyrightHolders)
113+
const year = new Date().getFullYear();
114+
const licenseText = replaceInLicense(licenseTextTemplate, 'year', year);
115+
files['LICENSE'] = replaceInLicense(licenseText, 'copyright holders', copyrightHolders);
103116
})
104117
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const templateUpdater = (m) => `<component-fixture>
2+
3+
${m}
4+
5+
<Editor slot="control" slot-scope="scope" v-bind="scope"/>
6+
7+
</component-fixture>`;
8+
9+
const componentTemplateRegex = /<HelloWorld (.)*\/>/g;
10+
11+
12+
const script = '<script>';
13+
const scriptUpdater = `<script>
14+
import { ComponentFixture, Editor } from 'component-fixture'`;
15+
16+
const exportComponents = ' components: {';
17+
const exportUpdater = ` components: {
18+
ComponentFixture,
19+
Editor,`;
20+
21+
function updateExample(content) {
22+
const updatedTemplate = content.replace(componentTemplateRegex, templateUpdater);
23+
const updatedScript = updatedTemplate.replace(script, scriptUpdater);
24+
const updatedExport = updatedScript.replace(exportComponents, exportUpdater);
25+
return updatedExport;
26+
}
27+
28+
module.exports = {
29+
updateExample
30+
};

prompts.js

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,70 @@
11

22
const licensesInformation = require('spdx-license-list/spdx-simple.json')
33
const licenses = licensesInformation.map(name => ({
4-
name,
5-
value: name
4+
name,
5+
value: name
66
}));
77

88
const prompts = module.exports = [
9-
{
10-
type: 'input',
11-
name: 'componentName',
12-
message: 'Enter the component name:'
13-
},
14-
{
15-
type: 'confirm',
16-
name: 'useVueStyleguidist',
17-
message: 'Use vue-styleguidist to generate documentation?',
18-
default: true,
19-
group: 'Documentation',
20-
},
21-
{
22-
type: 'confirm',
23-
name: 'useVueDoc',
24-
message: 'Use vuedoc.md to automatically generate README API section?',
25-
default: true,
26-
group: 'Documentation'
27-
},
28-
{
29-
type: 'confirm',
30-
name: 'addBadges',
31-
message: 'Add project badges to README.md?',
32-
default: true,
33-
group: 'Documentation'
34-
},
35-
{
36-
type: 'confirm',
37-
name: 'addLicense',
38-
message: 'Add license?',
39-
group: 'License',
40-
default: false
41-
},
42-
{
43-
type: 'list',
44-
name: 'licenseName',
45-
when: answer => answer.addLicense,
46-
message: 'Choose a license:',
47-
group: 'License',
48-
choices: licenses,
49-
default: 'MIT'
50-
},
51-
{
52-
type: 'input',
53-
name: 'copyrightHolders',
54-
when: answer => answer.addLicense,
55-
message: 'Enter copyright holders:',
56-
group: 'License',
57-
},
9+
{
10+
type: 'input',
11+
name: 'componentName',
12+
message: 'Enter the component name:'
13+
},
14+
{
15+
type: 'confirm',
16+
name: 'useComponentFixture',
17+
message: 'Use componentFixture to build example?',
18+
default: false,
19+
group: 'Example',
20+
},
21+
{
22+
type: 'confirm',
23+
name: 'useVueStyleguidist',
24+
message: 'Use vue-styleguidist to generate documentation?',
25+
default: false,
26+
group: 'Documentation',
27+
},
28+
{
29+
type: 'confirm',
30+
name: 'useVueDoc',
31+
message: 'Use vuedoc.md to automatically generate README API section?',
32+
default: true,
33+
group: 'Documentation'
34+
},
35+
{
36+
type: 'confirm',
37+
name: 'addBadges',
38+
message: 'Add project badges to README.md?',
39+
default: true,
40+
group: 'Documentation'
41+
},
42+
{
43+
type: 'confirm',
44+
name: 'addLicense',
45+
message: 'Add license?',
46+
group: 'License',
47+
default: false
48+
},
49+
{
50+
type: 'list',
51+
name: 'licenseName',
52+
when: answer => answer.addLicense,
53+
message: 'Choose a license:',
54+
group: 'License',
55+
choices: licenses,
56+
default: 'MIT'
57+
},
58+
{
59+
type: 'input',
60+
name: 'copyrightHolders',
61+
when: answer => answer.addLicense,
62+
message: 'Enter copyright holders:',
63+
group: 'License',
64+
},
5865
]
5966

6067
module.exports.getPrompts = pkg => {
61-
prompts[0].default = pkg.name
62-
return prompts
68+
prompts[0].default = pkg.name
69+
return prompts
6370
}

0 commit comments

Comments
 (0)