1 | import { GeneratorPlugin, PromptModuleAPI } from '@vue/cli'
|
2 |
|
3 | const testPromptAPI = (cli: PromptModuleAPI) => {
|
4 | cli.injectFeature({
|
5 | name: 'Babel',
|
6 | value: 'babel',
|
7 | short: 'Babel',
|
8 |
|
9 |
|
10 | checked: true
|
11 | })
|
12 |
|
13 | cli.injectOptionForPrompt('customBar', {
|
14 | name: 'barChoice',
|
15 | value: 'barChoice'
|
16 | })
|
17 | cli.onPromptComplete<{ features: string[]; useTsWithBabel: boolean }>((answers, options) => {
|
18 | if (answers.features.includes('ts')) {
|
19 | if (!answers.useTsWithBabel) {
|
20 | return
|
21 | }
|
22 | } else if (!answers.features.includes('babel')) {
|
23 | return
|
24 | }
|
25 | options.plugins['@vue/cli-plugin-babel'] = {}
|
26 | })
|
27 |
|
28 | cli.injectFeature({
|
29 | name: 'CSS Pre-processors',
|
30 | value: 'css-preprocessor'
|
31 |
|
32 |
|
33 | })
|
34 |
|
35 | const notice = 'PostCSS, Autoprefixer and CSS Modules are supported by default'
|
36 | cli.injectPrompt<{ features: string[] }>({
|
37 | name: 'cssPreprocessor',
|
38 | when: answers => answers.features.includes('css-preprocessor'),
|
39 | type: 'list',
|
40 | message: `Pick a CSS pre-processor${process.env.VUE_CLI_API_MODE ? '' : ` (${notice})`}:`,
|
41 |
|
42 | choices: [
|
43 | {
|
44 | name: 'Sass/SCSS (with dart-sass)',
|
45 | value: 'dart-sass'
|
46 | },
|
47 | {
|
48 | name: 'Sass/SCSS (with node-sass)',
|
49 | value: 'node-sass'
|
50 | },
|
51 | {
|
52 | name: 'Less',
|
53 | value: 'less'
|
54 | },
|
55 | {
|
56 | name: 'Stylus',
|
57 | value: 'stylus'
|
58 | }
|
59 | ]
|
60 | })
|
61 | }
|
62 |
|
63 | const generator: GeneratorPlugin = (api, options, rootOptions, invoking) => {
|
64 | const version = api.cliVersion
|
65 | const cliServiceVersion = api.cliServiceVersion
|
66 | api.assertCliServiceVersion(4)
|
67 | api.assertCliServiceVersion('^100')
|
68 | api.hasPlugin('eslint')
|
69 | api.hasPlugin('eslint', '^6.0.0')
|
70 |
|
71 | api.addConfigTransform('fooConfig', {
|
72 | file: {
|
73 | json: ['foo.config.json']
|
74 | }
|
75 | })
|
76 |
|
77 | api.extendPackage({
|
78 | fooConfig: {
|
79 | bar: 42
|
80 | },
|
81 | dependencies: {
|
82 | 'vue-router-layout': '^0.1.2'
|
83 | }
|
84 | })
|
85 | api.extendPackage(() => ({
|
86 | fooConfig: {
|
87 | bar: 42
|
88 | },
|
89 | dependencies: {
|
90 | 'vue-router-layout': '^0.1.2'
|
91 | }
|
92 | }))
|
93 | api.extendPackage(pkg => ({
|
94 | foo: pkg.foo + 1
|
95 | }))
|
96 | api.extendPackage(
|
97 | {
|
98 | fooConfig: {
|
99 | bar: 42
|
100 | },
|
101 | dependencies: {
|
102 | 'vue-router-layout': '^0.1.2'
|
103 | }
|
104 | },
|
105 | true
|
106 | )
|
107 | api.extendPackage(
|
108 | {
|
109 | fooConfig: {
|
110 | bar: 42
|
111 | },
|
112 | dependencies: {
|
113 | 'vue-router-layout': '^0.1.2'
|
114 | }
|
115 | },
|
116 | {
|
117 | merge: true,
|
118 | prune: true,
|
119 | warnIncompatibleVersions: true
|
120 | }
|
121 | )
|
122 |
|
123 | api.render('./template')
|
124 |
|
125 | api.render(
|
126 | './template',
|
127 | {
|
128 | hasTS: api.hasPlugin('typescript'),
|
129 | hasESLint: api.hasPlugin('eslint')
|
130 | },
|
131 | {
|
132 | strict: true,
|
133 | rmWhitespace: false
|
134 | }
|
135 | )
|
136 |
|
137 | api.render((files, render) => {
|
138 | files['foo2.js'] = render('foo(<%- n %>)', { n: 3 })
|
139 | files['bar/bar2.js'] = render('bar(<%- n %>)', { n: 3 }, { rmWhitespace: false })
|
140 | })
|
141 |
|
142 | api.postProcessFiles(files => {
|
143 | delete files['src/test.js']
|
144 | })
|
145 |
|
146 | api.onCreateComplete(() => {
|
147 | console.log('complete')
|
148 | })
|
149 |
|
150 | api.afterInvoke(() => {
|
151 | console.log('after invoke')
|
152 | })
|
153 |
|
154 | api.afterAnyInvoke(() => {
|
155 | console.log('after any invoke')
|
156 | })
|
157 |
|
158 | api.exitLog('msg')
|
159 | api.exitLog('msg', 'error')
|
160 | api.genJSConfig({ foo: 1 })
|
161 |
|
162 | api.extendPackage({
|
163 | vue: {
|
164 | publicPath: api.makeJSOnlyValue(`process.env.VUE_CONTEXT`)
|
165 | }
|
166 | })
|
167 | api.transformScript(
|
168 | 'src/test.js',
|
169 | (fileInfo, api, { additionalData }) => {
|
170 | const j = api.jscodeshift
|
171 | const root = j(fileInfo.source)
|
172 | return root.toSource()
|
173 | },
|
174 | {
|
175 | additionalData: []
|
176 | }
|
177 | )
|
178 |
|
179 | api.injectImports('main.js', `import bar from 'bar'`)
|
180 |
|
181 | api.injectRootOptions('main.js', ['foo', 'bar'])
|
182 |
|
183 | api.resolve(api.entryFile)
|
184 |
|
185 | const isInvoking = api.invoking
|
186 | }
|
187 |
|
188 | export = generator
|