1 | import { EventEmitter } from 'node:events';
|
2 | import { mock } from 'node:test';
|
3 | import { type Store } from 'mem-fs';
|
4 | import type { BaseEnvironmentOptions, BaseGenerator, GetGeneratorConstructor, GetGeneratorOptions, LookupOptions, PromptAnswers } from '@yeoman/types';
|
5 | import { type MemFsEditor, type MemFsEditorFile } from 'mem-fs-editor';
|
6 | import type { DefaultEnvironmentApi, DefaultGeneratorApi } from '../types/type-helpers.js';
|
7 | import RunResult, { type RunResultOptions } from './run-result.js';
|
8 | import { type CreateEnv as CreateEnvironment, type Dependency, type YeomanTest } from './helpers.js';
|
9 | import { type AskedQuestions, type DummyPromptOptions, type TestAdapterOptions } from './adapter.js';
|
10 |
|
11 |
|
12 |
|
13 | export type RunContextSettings = {
|
14 | |
15 |
|
16 |
|
17 |
|
18 | tmpdir?: boolean;
|
19 | cwd?: string;
|
20 | oldCwd?: string;
|
21 | forwardCwd?: boolean;
|
22 | autoCleanup?: boolean;
|
23 | memFs?: Store<MemFsEditorFile>;
|
24 | |
25 |
|
26 |
|
27 | resolved?: string;
|
28 | |
29 |
|
30 |
|
31 |
|
32 | namespace?: string;
|
33 | };
|
34 | type PromiseRunResult<GeneratorType extends BaseGenerator> = Promise<RunResult<GeneratorType>>;
|
35 | type MockedGeneratorFactory<GenParameter extends BaseGenerator = DefaultGeneratorApi> = (GeneratorClass?: GetGeneratorConstructor<GenParameter>) => GetGeneratorConstructor<GenParameter>;
|
36 | type EnvironmentOptions = BaseEnvironmentOptions & {
|
37 | createEnv?: CreateEnvironment;
|
38 | };
|
39 | export declare class RunContextBase<GeneratorType extends BaseGenerator = DefaultGeneratorApi> extends EventEmitter {
|
40 | readonly mockedGenerators: Record<string, BaseGenerator>;
|
41 | env: DefaultEnvironmentApi;
|
42 | generator: GeneratorType;
|
43 | readonly settings: RunContextSettings;
|
44 | readonly envOptions: EnvironmentOptions;
|
45 | completed: boolean;
|
46 | targetDirectory?: string;
|
47 | editor: MemFsEditor;
|
48 | memFs: Store<MemFsEditorFile>;
|
49 | spawnStub?: any;
|
50 | mockedGeneratorFactory: MockedGeneratorFactory;
|
51 | readonly askedQuestions: AskedQuestions;
|
52 | protected environmentPromise?: PromiseRunResult<GeneratorType>;
|
53 | private args;
|
54 | private options;
|
55 | private answers?;
|
56 | private readonly adapterOptions?;
|
57 | private keepFsState?;
|
58 | private readonly onGeneratorCallbacks;
|
59 | private readonly onTargetDirectoryCallbacks;
|
60 | private readonly onEnvironmentCallbacks;
|
61 | private readonly inDirCallbacks;
|
62 | private readonly Generator?;
|
63 | private readonly helpers;
|
64 | private readonly temporaryDir;
|
65 | private oldCwd?;
|
66 | private eventListenersSet;
|
67 | private envCB;
|
68 | private built;
|
69 | private ran;
|
70 | private errored;
|
71 | private readonly beforePrepareCallbacks;
|
72 | private environmentRun?;
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | constructor(generatorType?: string | GetGeneratorConstructor<GeneratorType>, settings?: RunContextSettings, environmentOptions?: EnvironmentOptions, helpers?: YeomanTest);
|
84 | /**
|
85 | * Run the generator on the environment and promises a RunResult instance.
|
86 | * @return {PromiseRunResult} Promise a RunResult instance.
|
87 | */
|
88 | run(): PromiseRunResult<GeneratorType>;
|
89 | on(eventName: string | symbol, listener: (...arguments_: any[]) => void): this;
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | inDir(dirPath: string, callback?: (folderPath: string) => void): this;
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 | doInDir(callback: (folderPath: string) => void): this;
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | cd(dirPath: string): this;
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | inTmpDir(callback?: (folderPath: string) => void): this;
|
123 | |
124 |
|
125 |
|
126 |
|
127 | restore(): this;
|
128 | |
129 |
|
130 |
|
131 |
|
132 | cleanup(): void;
|
133 | |
134 |
|
135 |
|
136 |
|
137 | cleanupTemporaryDir(): void;
|
138 | |
139 |
|
140 |
|
141 |
|
142 | cleanTestDirectory(force?: boolean): void;
|
143 | |
144 |
|
145 |
|
146 | withAdapterOptions(options: Omit<TestAdapterOptions, 'mockedAnswers'>): this;
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | withEnvironment(callback: any): this;
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | withEnvironmentRun(callback: (this: this, env: DefaultEnvironmentApi, gen: GeneratorType) => void): this;
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 | withLookups(lookups: LookupOptions | LookupOptions[]): this;
|
170 | |
171 |
|
172 |
|
173 |
|
174 | withArguments(arguments_: string | string[]): this;
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 | withOptions(options: Partial<Omit<GetGeneratorOptions<GeneratorType>, 'env' | 'namespace' | 'resolved'>>): this;
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | withPrompts(answers: PromptAnswers, options?: Omit<DummyPromptOptions, 'mockedAnswers'>): this;
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | withAnswers(answers: PromptAnswers, options?: Omit<DummyPromptOptions, 'mockedAnswers'>): this;
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | withGenerators(dependencies: Dependency[]): this;
|
215 | withSpawnMock<StubType = ReturnType<typeof mock.fn>>(options?: ((...arguments_: any[]) => any) | {
|
216 | stub?: (...arguments_: any[]) => any;
|
217 | registerNodeMockDefaults?: boolean;
|
218 | callback?: ({ stub, implementation }: {
|
219 | stub: StubType;
|
220 | implementation: any;
|
221 | }) => void | Promise<void>;
|
222 | }): this;
|
223 | withMockedGeneratorFactory(mockedGeneratorFactory: MockedGeneratorFactory): this;
|
224 | /**
|
225 | * Create mocked generators
|
226 | * @param namespaces - namespaces of mocked generators
|
227 | * @return this
|
228 | * @example
|
229 | * var angular = helpers
|
230 | * .create('../../app')
|
231 | * .withMockedGenerators([
|
232 | * 'foo:app',
|
233 | * 'foo:bar',
|
234 | * ])
|
235 | * .run()
|
236 | * .then(runResult => assert(runResult
|
237 | * .mockedGenerators['foo:app']
|
238 | .calledOnce));
|
239 | */
|
240 | withMockedGenerators(namespaces: string[]): this;
|
241 | /**
|
242 | * Mock the local configuration with the provided config
|
243 | * @param localConfig - should look just like if called config.getAll()
|
244 | */
|
245 | withLocalConfig(localConfig: any): this;
|
246 | /**
|
247 | * Don't reset mem-fs state cleared to aggregate snapshots from multiple runs.
|
248 | */
|
249 | withKeepFsState(): this;
|
250 | /**
|
251 | * Add files to mem-fs.
|
252 | * Files will be resolved relative to targetDir.
|
253 | *
|
254 | * Files with Object content will be merged to existing content.
|
255 | * To avoid merging, `JSON.stringify` the content.
|
256 | */
|
257 | withFiles(files: Record<string, string | Record<string, unknown>>): this;
|
258 | withFiles(relativePath: string, files: Record<string, string | Record<string, unknown>>): this;
|
259 | /**
|
260 | * Add .yo-rc.json to mem-fs.
|
261 | *
|
262 | * @param content
|
263 | * @returns
|
264 | */
|
265 | withYoRc(content: string | Record<string, unknown>): this;
|
266 | /**
|
267 | * Add a generator config to .yo-rc.json
|
268 | */
|
269 | withYoRcConfig(key: string, content: Record<string, unknown>): this;
|
270 | /**
|
271 | * Commit mem-fs files.
|
272 | */
|
273 | commitFiles(): this;
|
274 | /**
|
275 | * Execute callback after targetDirectory is set
|
276 | * @param callback
|
277 | * @returns
|
278 | */
|
279 | onTargetDirectory(callback: (this: this, targetDirectory: string) => any): this;
|
280 | /**
|
281 | * Execute callback after generator is ready
|
282 | * @param callback
|
283 | * @returns
|
284 | */
|
285 | onGenerator(callback: (this: this, generator: GeneratorType) => any): this;
|
286 | /**
|
287 | * Execute callback prefore parepare
|
288 | * @param callback
|
289 | * @returns
|
290 | */
|
291 | onBeforePrepare(callback: (this: this) => void | Promise<void>): this;
|
292 | /**
|
293 | * Execute callback after environment is ready
|
294 | * @param callback
|
295 | * @returns
|
296 | */
|
297 | onEnvironment(callback: (this: this, environment: DefaultEnvironmentApi) => any): this;
|
298 | prepare(): Promise<void>;
|
299 | protected assertNotBuild(): void;
|
300 | /**
|
301 | * Build the generator and the environment.
|
302 | * @return {RunContext|false} this
|
303 | */
|
304 | build(): Promise<void>;
|
305 | /**
|
306 | * Return a promise representing the generator run process
|
307 | * @return Promise resolved on end or rejected on error
|
308 | */
|
309 | protected toPromise(): PromiseRunResult<GeneratorType>;
|
310 | protected _createRunResultOptions(): RunResultOptions<GeneratorType>;
|
311 | /**
|
312 | * Keeps compatibility with events
|
313 | */
|
314 | private setupEventListeners;
|
315 | /**
|
316 | * Set the target directory.
|
317 | * @private
|
318 | * @param {String} dirPath - Directory path (relative to CWD). Prefer passing an absolute
|
319 | * file path for predictable results
|
320 | * @return {this} run context instance
|
321 | */
|
322 | private setDir;
|
323 | }
|
324 | export default class RunContext<GeneratorType extends BaseGenerator = BaseGenerator> extends RunContextBase<GeneratorType> implements Promise<RunResult<GeneratorType>> {
|
325 | then<TResult1 = RunResult<GeneratorType>, TResult2 = never>(onfulfilled?: ((value: RunResult<GeneratorType>) => TResult1 | PromiseLike<TResult1>) | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined): Promise<TResult1 | TResult2>;
|
326 | catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined): Promise<RunResult<GeneratorType> | TResult>;
|
327 | finally(onfinally?: (() => void) | undefined): Promise<RunResult<GeneratorType>>;
|
328 | get [Symbol.toStringTag](): string;
|
329 | }
|
330 | export declare class BasicRunContext<GeneratorType extends BaseGenerator = BaseGenerator> extends RunContext<GeneratorType> {
|
331 | run(): PromiseRunResult<any>;
|
332 | }
|
333 | export {};
|
334 |
|
\ | No newline at end of file |