UNPKG

8.69 kBTypeScriptView Raw
1// Type definitions for yeoman-generator 2.0
2// Project: https://github.com/yeoman/generator
3// Definitions by: Kentaro Okuno <https://github.com/armorik83>
4// Jay Anslow <https://github.com/janslow>
5// Ika <https://github.com/ikatyang>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.3
8
9import { EventEmitter } from 'events';
10import * as inquirer from 'inquirer';
11
12type Callback = (err: any) => void;
13
14declare namespace Generator {
15 interface Question extends inquirer.Question {
16 /**
17 * whether to store the user's previous answer
18 */
19 store?: boolean;
20 }
21 type Questions = Question | Question[] | Rx.Observable<Question>;
22 type Answers = inquirer.Answers;
23
24 class Storage {
25 constructor(name: string, fs: MemFsEditor, configPath: string);
26
27 defaults(defaults: {}): {};
28 delete(key: string): void;
29 get(key: string): any;
30 getAll(): { [key: string]: any };
31 save(): void;
32 set(key: string, value: any): any;
33 }
34 interface InstallOptions {
35 /**
36 * whether to run `npm install` or can be options to pass to `dargs` as arguments
37 */
38 npm?: boolean | object;
39 /**
40 * whether to run `bower install` or can be options to pass to `dargs` as arguments
41 */
42 bower?: boolean | object;
43 /**
44 * whether to run `yarn install` or can be options to pass to `dargs` as arguments
45 */
46 yarn?: boolean | object;
47 /**
48 * whether to log the used commands
49 */
50 skipMessage?: boolean;
51 }
52 interface ArgumentConfig {
53 description?: string;
54 required?: boolean;
55 optional?: boolean;
56 type?: typeof String|typeof Number|typeof Array|typeof Object;
57 default?: any;
58 }
59 interface OptionConfig {
60 alias?: string;
61 default?: any;
62 description?: string;
63 hide?: boolean;
64 type?: typeof Boolean|typeof String|typeof Number;
65 }
66 interface MemFsEditor {
67 read(filepath: string, options?: {}): string;
68 readJSON(filepath: string, defaults?: {}): any;
69 write(filepath: string, contents: string): void;
70 writeJSON(filepath: string, contents: {}, replacer?: (key: string, value: any) => any, space?: number): void;
71 extendJSON(filepath: string, contents: {}, replacer?: (key: string, value: any) => any, space?: number): void;
72 delete(filepath: string, options?: {}): void;
73 copy(from: string, to: string, options?: {}): void;
74 copyTpl(from: string, to: string, context: {}, templateOptions?: {}, copyOptions?: {}): void;
75 move(from: string, to: string, options?: {}): void;
76 exists(filepath: string): boolean;
77 commit(callback: Callback): void;
78 commit(filters: any[], callback: Callback): void;
79 }
80}
81
82declare class Generator extends EventEmitter {
83 constructor(args: string|string[], options: {});
84
85 env: {
86 error(...e: Error[]): void
87 };
88 args: {};
89 resolved: string;
90 description: string;
91 appname: string;
92 config: Generator.Storage;
93 fs: Generator.MemFsEditor;
94 options: {};
95 log(message?: string, context?: any): void;
96
97 argument(name: string, config: Generator.ArgumentConfig): this;
98 composeWith(namespace: string, options: { [name: string]: any }, settings?: { local: string, link: 'weak'|'strong' }): this;
99 destinationPath(...path: string[]): string;
100 destinationRoot(rootPath?: string): string;
101 determineAppname(): string;
102 option(name: string, config: Generator.OptionConfig): this;
103 prompt(questions: Generator.Questions): Promise<Generator.Answers>;
104 registerTransformStream(stream: {}|Array<{}>): this;
105 rootGeneratorName(): string;
106 rootGeneratorVersion(): string;
107 run(cb?: Callback): this;
108 sourceRoot(rootPath?: string): string;
109 templatePath(...path: string[]): string;
110
111 // actions/help mixin
112 argumentsHelp(): string;
113 desc(description: string): this;
114 help(): string;
115 optionsHelp(): string;
116 usage(): string;
117
118 // actions/spawn_command mixin
119 spawnCommand(command: string, args: string[], opt?: {}): any;
120 spawnCommandSync(command: string, args: string[], opt?: {}): any;
121
122 // actions/install mixin
123 /**
124 * Receives a list of `components` and an `options` object to install through bower.
125 *
126 * The installation will automatically run during the run loop `install` phase.
127 *
128 * @param component Components to install
129 * @param options Options to pass to `dargs` as arguments
130 * @param spawnOptions Options to pass `child_process.spawn`.
131 * @return Resolved if install successful, rejected otherwise
132 */
133 bowerInstall(component?: string|string[], options?: object, spawnOptions?: object): Promise<void>;
134 /**
135 * Runs `npm` and `bower`, in sequence, in the generated directory and prints a
136 * message to let the user know.
137 *
138 * @example
139 * this.installDependencies({
140 * bower: true,
141 * npm: true
142 * }).then(() => console.log('Everything is ready!'));
143 *
144 * @example
145 * this.installDependencies({
146 * yarn: {force: true},
147 * npm: false
148 * }).then(() => console.log('Everything is ready!'));
149 *
150 * @return Resolved if install successful, rejected otherwise
151 */
152 installDependencies(options?: Generator.InstallOptions): Promise<void>;
153 /**
154 * Receives a list of `packages` and an `options` object to install through npm.
155 *
156 * The installation will automatically run during the run loop `install` phase.
157 *
158 * @param pkgs Packages to install
159 * @param options Options to pass to `dargs` as arguments
160 * @param spawnOptions Options to pass `child_process.spawn`.
161 * @return Resolved if install successful, rejected otherwise
162 */
163 npmInstall(pkgs?: string|string[], options?: object, spawnOptions?: object): Promise<void>;
164 /**
165 * Combine package manager cmd line arguments and run the `install` command.
166 *
167 * During the `install` step, every command will be scheduled to run once, on the
168 * run loop. This means you can use `Promise.then` to log information, but don't
169 * return it or mix it with `this.async` as it'll dead lock the process.
170 *
171 * @param installer Which package manager to use
172 * @param paths Packages to install. Use an empty string for `npm install`
173 * @param options Options to pass to `dargs` as arguments
174 * @param spawnOptions Options to pass `child_process.spawn`. ref
175 * https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
176 * @return Resolved if install successful, rejected otherwise
177 */
178 runInstall(installer: string, paths?: string|string[], options?: object, spawnOptions?: object): Promise<void>;
179 /**
180 * Receives a list of `packages` and an `options` object to install through npm.
181 *
182 * The installation will automatically run during the run loop `install` phase.
183 *
184 * @param pkgs Packages to install
185 * @param options Options to pass to `dargs` as arguments
186 * @param spawnOptions Options to pass `child_process.spawn`.
187 * @return Resolved if install successful, rejected otherwise
188 */
189 yarnInstall(pkgs?: string|string[], options?: object, spawnOptions?: object): Promise<void>;
190
191 // actions/user mixin
192 readonly user: {
193 readonly git: {
194 /**
195 * Retrieves user's email from Git in the global scope or the project scope
196 * (it'll take what Git will use in the current context)
197 * @return configured git email or undefined
198 */
199 email(): string;
200 /**
201 * Retrieves user's name from Git in the global scope or the project scope
202 * (it'll take what Git will use in the current context)
203 * @return configured git name or undefined
204 */
205 name(): string;
206 };
207 readonly github: {
208 /**
209 * Retrieves GitHub's username from the GitHub API
210 * @return Resolved with the GitHub username or rejected if unable to
211 * get the information
212 */
213 username(): Promise<string>;
214 }
215 };
216}
217export = Generator;