|
| 1 | +import { GeneratorAPI, PromptModuleAPI } from '@vue/cli' |
| 2 | + |
| 3 | +const testPromptAPI = (cli: PromptModuleAPI) => { |
| 4 | + cli.injectFeature({ |
| 5 | + name: 'Babel', |
| 6 | + value: 'babel', |
| 7 | + short: 'Babel', |
| 8 | + // descriptions: 'Transpile modern JavaScript to older versions (for compatibility)', |
| 9 | + // link: 'https://babeljs.io/', |
| 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 | + // description: 'Add support for CSS pre-processors like Sass, Less or Stylus', |
| 32 | + // link: 'https://cli.vuejs.org/guide/css.html' |
| 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 | + // description: `${notice}.`, |
| 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 | +export = (api: GeneratorAPI) => { |
| 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( |
| 94 | + { |
| 95 | + fooConfig: { |
| 96 | + bar: 42 |
| 97 | + }, |
| 98 | + dependencies: { |
| 99 | + 'vue-router-layout': '^0.1.2' |
| 100 | + } |
| 101 | + }, |
| 102 | + true |
| 103 | + ) |
| 104 | + api.extendPackage( |
| 105 | + { |
| 106 | + fooConfig: { |
| 107 | + bar: 42 |
| 108 | + }, |
| 109 | + dependencies: { |
| 110 | + 'vue-router-layout': '^0.1.2' |
| 111 | + } |
| 112 | + }, |
| 113 | + { |
| 114 | + merge: true, |
| 115 | + prune: true, |
| 116 | + warnIncompatibleVersions: true |
| 117 | + } |
| 118 | + ) |
| 119 | + |
| 120 | + api.render('./template') |
| 121 | + |
| 122 | + api.render( |
| 123 | + './template', |
| 124 | + { |
| 125 | + hasTS: api.hasPlugin('typescript'), |
| 126 | + hasESLint: api.hasPlugin('eslint') |
| 127 | + }, |
| 128 | + { |
| 129 | + strict: true, |
| 130 | + rmWhitespace: false |
| 131 | + } |
| 132 | + ) |
| 133 | + |
| 134 | + api.render((files, render) => { |
| 135 | + files['foo2.js'] = render('foo(<%- n %>)', { n: 3 }) |
| 136 | + files['bar/bar2.js'] = render('bar(<%- n %>)', { n: 3 }, { rmWhitespace: false }) |
| 137 | + }) |
| 138 | + |
| 139 | + api.postProcessFiles(files => { |
| 140 | + delete files['src/test.js'] |
| 141 | + }) |
| 142 | + |
| 143 | + api.onCreateComplete(() => { |
| 144 | + console.log('complete') |
| 145 | + }) |
| 146 | + |
| 147 | + api.afterInvoke(() => { |
| 148 | + console.log('after invoke') |
| 149 | + }) |
| 150 | + |
| 151 | + api.afterAnyInvoke(() => { |
| 152 | + console.log('after any invoke') |
| 153 | + }) |
| 154 | + |
| 155 | + api.exitLog('msg') |
| 156 | + api.exitLog('msg', 'error') |
| 157 | + api.genJSConfig({ foo: 1 }) |
| 158 | + |
| 159 | + api.extendPackage({ |
| 160 | + vue: { |
| 161 | + publicPath: api.makeJSOnlyValue(`process.env.VUE_CONTEXT`) |
| 162 | + } |
| 163 | + }) |
| 164 | + api.transformScript( |
| 165 | + 'src/test.js', |
| 166 | + (fileInfo, api, { additionalData }) => { |
| 167 | + const j = api.jscodeshift |
| 168 | + const root = j(fileInfo.source) |
| 169 | + return root.toSource() |
| 170 | + }, |
| 171 | + { |
| 172 | + additionalData: [] |
| 173 | + } |
| 174 | + ) |
| 175 | + |
| 176 | + api.injectImports('main.js', `import bar from 'bar'`) |
| 177 | + |
| 178 | + api.injectRootOptions('main.js', ['foo', 'bar']) |
| 179 | + |
| 180 | + api.resolve(api.entryFile) |
| 181 | + |
| 182 | + const isInvoking = api.invoking |
| 183 | +} |
0 commit comments