UNPKG

6.96 kBTypeScriptView Raw
1import { DistinctQuestion, CheckboxChoiceOptions, Answers, ChoiceOptions } from 'inquirer'
2import { Parser, Transform } from 'jscodeshift'
3import * as ejs from 'ejs'
4
5interface RenderFile {
6 [path: string]: string | Buffer
7}
8
9type FileMiddleware = (files: RenderFile, render: typeof ejs.render) => void
10type PostProcessFilesCallback = (files: RenderFile) => void
11
12type RenderSource = string | RenderFile
13
14type TransformModule = Transform & {
15 default?: Transform
16 parser?: string | Parser
17}
18interface TransformOptions {
19 [prop: string]: any
20 parser?: string | Parser
21}
22interface __expressionFn {
23 (): void
24 __expression: string
25}
26
27interface OnPromptCompleteCb<T> {
28 (
29 answers: T,
30 options: {
31 useConfigFiles: boolean
32 plugins: Record<string, any>
33 }
34 ): void
35}
36type ExtendPackageOptions =
37 | {
38 prune?: boolean
39 merge?: boolean
40 warnIncompatibleVersions?: boolean
41 }
42 | boolean
43
44type Preset = Partial<{
45 [props: string]: any
46 bare: boolean
47 projectName: string
48 useConfigFiles: boolean
49 plugins: Record<string, any>
50 configs: Record<string, any>
51 cssPreprocessor: 'sass' | 'dart-sass' | 'node-sass' | 'less' | 'stylus'
52}>
53
54declare class PromptModuleAPI {
55 /** inject checkbox choice for feature prompt. */
56 injectFeature<T = Answers>(feature: CheckboxChoiceOptions<T>): void
57
58 injectPrompt<T = Answers>(prompt: DistinctQuestion<T>): void
59
60 injectOptionForPrompt(name: string, option: ChoiceOptions): void
61
62 /** run cb registered by prompt modules to finalize the preset. */
63 onPromptComplete<T = Answers>(cb: OnPromptCompleteCb<T>): void
64}
65
66declare class GeneratorAPI {
67 /**
68 * Resolve path for a project.
69 *
70 * @param _paths - A sequence of relative paths or path segments
71 * @return The resolved absolute path, calculated based on the current project root.
72 */
73 resolve(..._paths: string[]): string
74
75 readonly cliVersion: string
76
77 assertCliVersion(range: number | string): void
78
79 readonly cliServiceVersion: string
80
81 assertCliServiceVersion(range: number | string): void
82
83 /**
84 * Check if the project has a given plugin.
85 *
86 * @param id - Plugin id, can omit the (@vue/|vue-|@scope/vue)-cli-plugin- prefix
87 * @param version - Plugin version. Defaults to ''
88 * @return `boolean`
89 */
90 hasPlugin(id: string, version?: string): boolean
91
92 /**
93 * Configure how config files are extracted.
94 *
95 * @param key - Config key in package.json
96 * @param options - Options
97 * @param options.file - File descriptor
98 * Used to search for existing file.
99 * Each key is a file type (possible values: ['js', 'json', 'yaml', 'lines']).
100 * The value is a list of filenames.
101 * Example:
102 * {
103 * js: ['.eslintrc.js'],
104 * json: ['.eslintrc.json', '.eslintrc']
105 * }
106 * By default, the first filename will be used to create the config file.
107 */
108 addConfigTransform(key: string, options: { file: { [type: string]: string[] } }): void
109
110 /**
111 * Extend the package.json of the project.
112 * Also resolves dependency conflicts between plugins.
113 * Tool configuration fields may be extracted into standalone files before
114 * files are written to disk.
115 *
116 * @param fields - Fields to merge.
117 * @param [options] - Options for extending / merging fields.
118 * @param [options.prune=false] - Remove null or undefined fields
119 * from the object after merging.
120 * @param [options.merge=true] deep-merge nested fields, note
121 * that dependency fields are always deep merged regardless of this option.
122 * @param [options.warnIncompatibleVersions=true] Output warning
123 * if two dependency version ranges don't intersect.
124 */
125 extendPackage(
126 fields: (pkg: Record<string, any>) => object,
127 options?: ExtendPackageOptions
128 ): void
129 extendPackage<T extends object>(
130 fields: T extends Function ? never : T,
131 options?: ExtendPackageOptions
132 ): void
133
134 /**
135 * Render template files into the virtual files tree object.
136 *
137 * @param source -
138 * Can be one of:
139 * - relative path to a directory;
140 * - Object hash of { sourceTemplate: targetFile } mappings;
141 * - a custom file middleware function.
142 * @param [additionalData] - additional data available to templates.
143 * @param [ejsOptions] - options for ejs.
144 */
145 render(source: RenderSource, additionalData?: object, ejsOptions?: ejs.Options): void
146 render(source: FileMiddleware): void
147
148 /**
149 * Push a file middleware that will be applied after all normal file
150 * middlewares have been applied.
151 *
152 * @param cb
153 */
154 postProcessFiles(cb: PostProcessFilesCallback): void
155
156 /**
157 * Push a callback to be called when the files have been written to disk.
158 *
159 * @param cb
160 */
161 onCreateComplete(cb: (...args: any[]) => any): void
162
163 /**
164 * same to `onCreateComplete`.
165 *
166 * @param cb
167 */
168 afterInvoke(cb: (...args: any[]) => any): void
169
170 /**
171 * Push a callback to be called when the files have been written to disk
172 * from non invoked plugins
173 *
174 * @param cb
175 */
176 afterAnyInvoke(cb: (...args: any[]) => any): void
177
178 /**
179 * Add a message to be printed when the generator exits (after any other standard messages).
180 *
181 * @param msg String or value to print after the generation is completed
182 * @param [type='log'] Type of message
183 */
184 exitLog(msg: any, type?: 'log' | 'info' | 'done' | 'warn' | 'error'): void
185
186 /**
187 * convenience method for generating a js config file from json
188 */
189 genJSConfig(value: any): string
190
191 /**
192 * Turns a string expression into executable JS for JS configs.
193 * @param str JS expression as a string
194 */
195 makeJSOnlyValue(str: string): __expressionFn
196
197 /**
198 * Run codemod on a script file or the script part of a .vue file
199 * @param file the path to the file to transform
200 * @param codemod the codemod module to run
201 * @param options additional options for the codemod
202 */
203 transformScript(file: string, codemod: TransformModule, options?: TransformOptions): void
204
205 /**
206 * Add import statements to a file.
207 */
208 injectImports(file: string, imports: string | string[]): void
209
210 /**
211 * Add options to the root Vue instance (detected by `new Vue`).
212 */
213 injectRootOptions(file: string, options: string | string[]): void
214
215 /**
216 * Get the entry file taking into account typescript.
217 *
218 */
219 readonly entryFile: 'src/main.ts' | 'src/main.js'
220
221 /**
222 * Is the plugin being invoked?
223 *
224 */
225 readonly invoking: boolean
226}
227
228/**
229 * function exported by a generator
230 * @param api - A GeneratorAPI instance
231 * @param options - These options are resolved during the prompt phase of project creation,
232 * or loaded from a saved preset in ~/.vuerc
233 * @param rootOptions - The entire preset will be passed
234 * @param invoking - Is the plugin being invoked
235 */
236type GeneratorPlugin = (
237 api: GeneratorAPI,
238 options: any,
239 rootOptions: Preset,
240 invoking: boolean
241) => any
242
243export { PromptModuleAPI, GeneratorAPI, Preset, GeneratorPlugin }