-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
fs.ts
358 lines (323 loc) · 12.7 KB
/
fs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* eslint max-params: [1, 5] */
import assert from 'node:assert';
import { type CopyOptions, type MemFsEditor } from 'mem-fs-editor';
import type { Data as TemplateData, Options as TemplateOptions } from 'ejs';
import type { OverloadParameters, OverloadReturnType } from '../types-utils.js';
import type BaseGenerator from '../generator.js';
export type Template<D extends TemplateData, G> = {
/**
* Template file, absolute or relative to templatePath().
*/
source: string;
/**
* Conditional if the template should be written.
* @param TemplateData
* @param Generator
* @returns
*/
when?: (data: D, generator: G) => boolean;
/**
* Destination, absolute or relative to destinationPath().
*/
destination?: string;
/**
* Mem-fs-editor copy options
*/
copyOptions?: CopyOptions;
/**
* Ejs data
*/
templateData?: TemplateData;
/**
* Ejs options
*/
templateOptions?: TemplateOptions;
};
export type Templates<D extends TemplateData, G> = Array<Template<D, G>>;
function applyToFirstStringArg<Type extends [string | string[], ...any]>(
customizer: (arg1: string) => string,
args: Type,
): Type {
args[0] = Array.isArray(args[0]) ? args[0].map(arg => customizer(arg)) : customizer(args[0]);
return args;
}
function applyToFirstAndSecondStringArg<Type extends [string | string[], string, ...any]>(
customizer1: (arg1: string) => string,
customizer2: (arg1: string) => string,
args: Type,
): Type {
args = applyToFirstStringArg(customizer1, args);
args[1] = customizer2(args[1]);
return args;
}
export class FsMixin {
fs!: MemFsEditor;
/**
* Read file from templates folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.read(this.templatePath(filepath))
*/
readTemplate(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['read']>
): OverloadReturnType<MemFsEditor['read']> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return this.fs.read(...applyToFirstStringArg(this.templatePath.bind(this), args));
}
/**
* Copy file from templates folder to destination folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.copy(this.templatePath(from), this.destinationPath(to))
*/
copyTemplate(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['copy']>
): OverloadReturnType<MemFsEditor['copy']> {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return this.fs.copy(
...applyToFirstAndSecondStringArg(this.templatePath.bind(this), this.destinationPath.bind(this), args),
);
}
/**
* Copy file from templates folder to destination folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.copy(this.templatePath(from), this.destinationPath(to))
*/
async copyTemplateAsync(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['copyAsync']>
): OverloadReturnType<MemFsEditor['copyAsync']> {
return this.fs.copyAsync(
...applyToFirstAndSecondStringArg(this.templatePath.bind(this), this.destinationPath.bind(this), args),
);
}
/**
* Read file from destination folder
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.read(this.destinationPath(filepath)).
*/
readDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['read']>
): OverloadReturnType<MemFsEditor['read']> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return this.fs.read(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Read JSON file from destination folder
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.readJSON(this.destinationPath(filepath)).
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
readDestinationJSON(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['readJSON']>
): OverloadReturnType<MemFsEditor['readJSON']> {
return this.fs.readJSON(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Write file to destination folder
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.write(this.destinationPath(filepath)).
*/
writeDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['write']>
): OverloadReturnType<MemFsEditor['write']> {
return this.fs.write(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Write json file to destination folder
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.writeJSON(this.destinationPath(filepath)).
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
writeDestinationJSON(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['writeJSON']>
): OverloadReturnType<MemFsEditor['writeJSON']> {
return this.fs.writeJSON(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Delete file from destination folder
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.delete(this.destinationPath(filepath)).
*/
deleteDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['delete']>
): OverloadReturnType<MemFsEditor['delete']> {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return this.fs.delete(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Copy file from destination folder to another destination folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.copy(this.destinationPath(from), this.destinationPath(to)).
*/
copyDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['copy']>
): OverloadReturnType<MemFsEditor['copy']> {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return this.fs.copy(
...applyToFirstAndSecondStringArg(this.destinationPath.bind(this), this.destinationPath.bind(this), args),
);
}
/**
* Move file from destination folder to another destination folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.move(this.destinationPath(from), this.destinationPath(to)).
*/
moveDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['move']>
): OverloadReturnType<MemFsEditor['move']> {
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return this.fs.move(
...applyToFirstAndSecondStringArg(this.destinationPath.bind(this), this.destinationPath.bind(this), args),
);
}
/**
* Exists file on destination folder.
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
* Shortcut for this.fs!.exists(this.destinationPath(filepath)).
*/
existsDestination(
this: BaseGenerator,
...args: OverloadParameters<MemFsEditor['exists']>
): OverloadReturnType<MemFsEditor['exists']> {
return this.fs.exists(...applyToFirstStringArg(this.destinationPath.bind(this), args));
}
/**
* Copy a template from templates folder to the destination.
*
* @param source - template file, absolute or relative to templatePath().
* @param destination - destination, absolute or relative to destinationPath().
* @param templateData - ejs data
* @param templateOptions - ejs options
* @param copyOptions - mem-fs-editor copy options
*/
renderTemplate<D extends TemplateData = TemplateData>(
this: BaseGenerator,
source: string | string[] = '',
destination: string | string[] = source,
templateData?: string | D,
templateOptions?: TemplateOptions,
copyOptions?: CopyOptions,
) {
if (templateData === undefined || typeof templateData === 'string') {
templateData = this._templateData<D>(templateData);
}
templateOptions = { context: this, ...templateOptions };
source = Array.isArray(source) ? source : [source];
const templatePath = this.templatePath(...source);
destination = Array.isArray(destination) ? destination : [destination];
const destinationPath = this.destinationPath(...destination);
this.fs.copyTpl(templatePath, destinationPath, templateData as TemplateData, templateOptions, copyOptions);
}
/**
* Copy a template from templates folder to the destination.
*
* @param source - template file, absolute or relative to templatePath().
* @param destination - destination, absolute or relative to destinationPath().
* @param templateData - ejs data
* @param templateOptions - ejs options
* @param copyOptions - mem-fs-editor copy options
*/
async renderTemplateAsync<D extends TemplateData = TemplateData>(
this: BaseGenerator,
source: string | string[] = '',
destination: string | string[] = source,
templateData?: string | D,
templateOptions?: TemplateOptions,
copyOptions?: CopyOptions,
) {
if (templateData === undefined || typeof templateData === 'string') {
templateData = this._templateData<D>(templateData);
}
templateOptions = { context: this, ...templateOptions };
source = Array.isArray(source) ? source : [source];
const templatePath = this.templatePath(...source);
destination = Array.isArray(destination) ? destination : [destination];
const destinationPath = this.destinationPath(...destination);
return this.fs.copyTplAsync(
templatePath,
destinationPath,
templateData as TemplateData,
templateOptions,
copyOptions,
);
}
/**
* Copy templates from templates folder to the destination.
*/
renderTemplates<D extends TemplateData = TemplateData>(
this: BaseGenerator,
templates: Templates<D, typeof this>,
templateData?: string | D,
) {
assert(Array.isArray(templates), 'Templates must an array');
if (templateData === undefined || typeof templateData === 'string') {
templateData = this._templateData<D>(templateData);
}
for (const template of templates) {
const { templateData: eachData = templateData, source, destination } = template;
if (!template.when || template.when(eachData as D, this)) {
this.renderTemplate(source, destination, eachData, template.templateOptions, template.copyOptions);
}
}
}
/**
* Copy templates from templates folder to the destination.
*
* @param templates - template file, absolute or relative to templatePath().
* @param templateData - ejs data
*/
async renderTemplatesAsync<D extends TemplateData = TemplateData>(
this: BaseGenerator,
templates: Templates<D, typeof this>,
templateData?: string | D,
) {
assert(Array.isArray(templates), 'Templates must an array');
if (templateData === undefined || typeof templateData === 'string') {
templateData = this._templateData<D>(templateData);
}
return Promise.all(
templates.map(async template => {
const { templateData: eachData = templateData, source, destination } = template;
if (!template.when || template.when(eachData as D, this)) {
return this.renderTemplateAsync(
source,
destination,
eachData,
template.templateOptions,
template.copyOptions,
);
}
return undefined;
}),
);
}
/**
* Utility method to get a formatted data for templates.
*
* @param path - path to the storage key.
* @return data to be passed to the templates.
*/
_templateData<D extends TemplateData = TemplateData>(this: BaseGenerator, path?: string): D {
if (path) {
return this.config.getPath(path);
}
const allConfig: D = this.config.getAll() as D;
if (this.generatorConfig) {
Object.assign(allConfig as any, this.generatorConfig.getAll());
}
if (this.instanceConfig) {
Object.assign(allConfig as any, this.instanceConfig.getAll());
}
return allConfig;
}
}