0
# File System Operations
1
2
Template rendering, file copying, and destination management with support for EJS templating and conflict resolution through mem-fs-editor integration.
3
4
## Capabilities
5
6
### Template Operations
7
8
Methods for reading, copying, and rendering template files with EJS processing.
9
10
```typescript { .api }
11
/**
12
* Read file from templates folder
13
* Shortcut for this.fs.read(this.templatePath(filepath))
14
*/
15
readTemplate(...args: Parameters<MemFsEditor['read']>): ReturnType<MemFsEditor['read']>;
16
17
/**
18
* Copy file from templates folder to destination folder
19
* Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to))
20
*/
21
copyTemplate(...args: Parameters<MemFsEditor['copy']>): ReturnType<MemFsEditor['copy']>;
22
23
/**
24
* Async copy file from templates folder to destination folder
25
*/
26
async copyTemplateAsync(...args: Parameters<MemFsEditor['copyAsync']>): ReturnType<MemFsEditor['copyAsync']>;
27
```
28
29
**Usage Examples:**
30
31
```typescript
32
export default class MyGenerator extends Generator {
33
writing() {
34
// Read template content
35
const content = this.readTemplate('config.json');
36
37
// Copy template file
38
this.copyTemplate('static-file.js', 'src/static-file.js');
39
40
// Copy multiple files with options
41
this.copyTemplate(
42
'assets/**/*',
43
'public/',
44
{ globOptions: { dot: true } }
45
);
46
}
47
48
async install() {
49
// Async copy for large files
50
await this.copyTemplateAsync('large-asset.bin', 'assets/large-asset.bin');
51
}
52
}
53
```
54
55
### Template Rendering
56
57
Render EJS templates with data substitution and processing.
58
59
```typescript { .api }
60
/**
61
* Copy a template from templates folder to the destination with EJS processing
62
* @param source - Template file, absolute or relative to templatePath()
63
* @param destination - Destination, absolute or relative to destinationPath()
64
* @param templateData - EJS data for template processing
65
* @param templateOptions - EJS processing options
66
* @param copyOptions - mem-fs-editor copy options
67
*/
68
renderTemplate<D extends TemplateData = TemplateData>(
69
source: string | string[],
70
destination: string | string[],
71
templateData?: string | D,
72
templateOptions?: TemplateOptions,
73
copyOptions?: CopyOptions
74
): void;
75
76
/**
77
* Async version of renderTemplate
78
*/
79
async renderTemplateAsync<D extends TemplateData = TemplateData>(
80
source: string | string[],
81
destination: string | string[],
82
templateData?: string | D,
83
templateOptions?: TemplateOptions,
84
copyOptions?: CopyOptions
85
): Promise<void>;
86
```
87
88
**Usage Examples:**
89
90
```typescript
91
export default class MyGenerator extends Generator {
92
async prompting() {
93
this.answers = await this.prompt([
94
{ name: 'name', message: 'Project name?' },
95
{ name: 'author', message: 'Author name?' }
96
]);
97
}
98
99
writing() {
100
// Render template with data
101
this.renderTemplate(
102
'package.json.ejs',
103
'package.json',
104
{
105
name: this.answers.name,
106
author: this.answers.author,
107
version: '1.0.0'
108
}
109
);
110
111
// Render with custom EJS options
112
this.renderTemplate(
113
'config.js.ejs',
114
'config/config.js',
115
this.answers,
116
{
117
delimiter: '?', // Use <?= %> instead of <%= %>
118
openDelimiter: '[',
119
closeDelimiter: ']'
120
}
121
);
122
}
123
}
124
```
125
126
### Batch Template Rendering
127
128
Render multiple templates with conditional logic.
129
130
```typescript { .api }
131
/**
132
* Copy multiple templates from templates folder to the destination
133
* @param templates - Array of template configurations
134
* @param templateData - Default EJS data for all templates
135
*/
136
renderTemplates<D extends TemplateData = TemplateData>(
137
templates: Templates<D, typeof this>,
138
templateData?: string | D
139
): void;
140
141
/**
142
* Async version of renderTemplates
143
*/
144
async renderTemplatesAsync<D extends TemplateData = TemplateData>(
145
templates: Templates<D, typeof this>,
146
templateData?: string | D
147
): Promise<void>;
148
149
// Template configuration type
150
interface Template<D extends TemplateData, G> {
151
source: string;
152
destination?: string;
153
when?: (data: D, generator: G) => boolean;
154
copyOptions?: CopyOptions;
155
templateData?: TemplateData;
156
templateOptions?: TemplateOptions;
157
}
158
159
type Templates<D extends TemplateData, G> = Array<Template<D, G>>;
160
```
161
162
**Usage Examples:**
163
164
```typescript
165
export default class MyGenerator extends Generator {
166
writing() {
167
const templates = [
168
{
169
source: 'src/index.js.ejs',
170
destination: 'src/index.js'
171
},
172
{
173
source: 'test/test.js.ejs',
174
destination: 'test/index.test.js',
175
when: (data) => data.includeTests
176
},
177
{
178
source: 'docker/Dockerfile.ejs',
179
destination: 'Dockerfile',
180
when: (data) => data.useDocker,
181
templateData: { baseImage: 'node:18' }
182
}
183
];
184
185
this.renderTemplates(templates, this.answers);
186
}
187
}
188
```
189
190
### Destination File Operations
191
192
Methods for reading, writing, and manipulating files in the destination directory.
193
194
```typescript { .api }
195
/**
196
* Read file from destination folder
197
* Shortcut for this.fs.read(this.destinationPath(filepath))
198
*/
199
readDestination(...args: Parameters<MemFsEditor['read']>): ReturnType<MemFsEditor['read']>;
200
201
/**
202
* Read JSON file from destination folder
203
* Shortcut for this.fs.readJSON(this.destinationPath(filepath))
204
*/
205
readDestinationJSON(...args: Parameters<MemFsEditor['readJSON']>): ReturnType<MemFsEditor['readJSON']>;
206
207
/**
208
* Write file to destination folder
209
* Shortcut for this.fs.write(this.destinationPath(filepath))
210
*/
211
writeDestination(...args: Parameters<MemFsEditor['write']>): ReturnType<MemFsEditor['write']>;
212
213
/**
214
* Write JSON file to destination folder
215
* Shortcut for this.fs.writeJSON(this.destinationPath(filepath))
216
*/
217
writeDestinationJSON(...args: Parameters<MemFsEditor['writeJSON']>): ReturnType<MemFsEditor['writeJSON']>;
218
219
/**
220
* Delete file from destination folder
221
* Shortcut for this.fs.delete(this.destinationPath(filepath))
222
*/
223
deleteDestination(...args: Parameters<MemFsEditor['delete']>): ReturnType<MemFsEditor['delete']>;
224
225
/**
226
* Check if file exists in destination folder
227
* Shortcut for this.fs.exists(this.destinationPath(filepath))
228
*/
229
existsDestination(...args: Parameters<MemFsEditor['exists']>): ReturnType<MemFsEditor['exists']>;
230
231
/**
232
* Copy file within destination folder
233
* Shortcut for this.fs.copy(this.destinationPath(from), this.destinationPath(to))
234
*/
235
copyDestination(...args: Parameters<MemFsEditor['copy']>): ReturnType<MemFsEditor['copy']>;
236
237
/**
238
* Move file within destination folder
239
* Shortcut for this.fs.move(this.destinationPath(from), this.destinationPath(to))
240
*/
241
moveDestination(...args: Parameters<MemFsEditor['move']>): ReturnType<MemFsEditor['move']>;
242
```
243
244
**Usage Examples:**
245
246
```typescript
247
export default class MyGenerator extends Generator {
248
writing() {
249
// Check if file exists before overwriting
250
if (!this.existsDestination('src/config.js')) {
251
this.writeDestination('src/config.js', 'module.exports = {};');
252
}
253
254
// Read existing package.json and modify
255
const pkg = this.readDestinationJSON('package.json', {});
256
pkg.scripts = pkg.scripts || {};
257
pkg.scripts.start = 'node src/index.js';
258
this.writeDestinationJSON('package.json', pkg);
259
260
// Backup existing file
261
if (this.existsDestination('config.json')) {
262
this.copyDestination('config.json', 'config.json.bak');
263
}
264
}
265
266
end() {
267
// Clean up temporary files
268
this.deleteDestination('temp-config.json');
269
}
270
}
271
```
272
273
### Template Data Utilities
274
275
Utility methods for preparing template data from configuration.
276
277
```typescript { .api }
278
/**
279
* Utility method to get formatted data for templates
280
* @param path - Path to the storage key, or undefined for all config
281
* @returns Data to be passed to the templates
282
*/
283
_templateData<D extends TemplateData = TemplateData>(path?: string): D;
284
```
285
286
**Usage Example:**
287
288
```typescript
289
export default class MyGenerator extends Generator {
290
writing() {
291
// Get all configuration as template data
292
const allData = this._templateData();
293
294
// Get specific configuration path
295
const userPrefs = this._templateData('user.preferences');
296
297
this.renderTemplate('app.js.ejs', 'src/app.js', allData);
298
}
299
}
300
```
301
302
## Types
303
304
```typescript { .api }
305
// Template data (EJS data)
306
type TemplateData = Record<string, any>;
307
308
// Template options (EJS options)
309
interface TemplateOptions {
310
context?: any;
311
delimiter?: string;
312
openDelimiter?: string;
313
closeDelimiter?: string;
314
debug?: boolean;
315
compileDebug?: boolean;
316
client?: boolean;
317
escape?: (markup: string) => string;
318
filename?: string;
319
root?: string | string[];
320
views?: string[];
321
cache?: boolean;
322
outputFunctionName?: string;
323
localsName?: string;
324
rmWhitespace?: boolean;
325
async?: boolean;
326
}
327
328
// Copy options from mem-fs-editor
329
interface CopyOptions {
330
fromBasePath?: string;
331
ignoreNoMatch?: boolean;
332
globOptions?: any;
333
processDestinationPath?: (dest: string) => string;
334
}
335
336
// Template configuration for batch rendering
337
interface Template<D extends TemplateData, G> {
338
source: string;
339
destination?: string;
340
when?: (data: D, generator: G) => boolean;
341
copyOptions?: CopyOptions;
342
templateData?: TemplateData;
343
templateOptions?: TemplateOptions;
344
}
345
346
type Templates<D extends TemplateData, G> = Array<Template<D, G>>;
347
```