-
Notifications
You must be signed in to change notification settings - Fork 82
/
file_system.js
508 lines (430 loc) · 15.8 KB
/
file_system.js
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
var path = require('path');
var fs = require('fs');
var ejs = require('ejs');
var _ = require('underscore');
/**
* Relies on tools/logging.
*/
module.exports = {};
/**
* Create a file at the given filepath and write the given data
* to the file. Recursively creates the directory structure if
* needed.
*/
module.exports.createFile = function createFile(filepath, data, opts) {
this.logDebug('createFile', filepath, opts);
opts = opts || {};
try {
var pathParts = path.normalize(filepath).split(path.sep);
var filename = pathParts.pop();
var dirpath = pathParts.join(path.sep);
// make sure the directory exists before we create the
// file.
this.createDirectory(dirpath, opts);
// if the file exists let's confirm with the user if we
// should override it
if (this.isFile(filepath)) {
if (!this.confirm(filepath + ' already exists. Do you want to overwrite it?'))
return false;
}
fs.writeFileSync(filepath, data || '');
this.logSuccess('created ' + path.relative(process.cwd(), filepath));
return true;
} catch (e) {
this.logError('Error creating file ' + path.relative(process.cwd(), filepath) + '. ' + String(e));
throw e;
}
};
/**
* Recursively creates a directory structure from the given
* dirpath.
*/
module.exports.createDirectory = function createDirectory(dirpath, opts) {
opts = opts || {};
try {
if (this.isDirectory(dirpath))
return true;
else if (this.isFile(dirpath))
return false;
// try to build the directory structure recursively and
// bail out if it fails anywhere up the stack.
var pathParts = path.normalize(dirpath).split(path.sep);
if (pathParts.length > 1) {
if (!this.createDirectory(pathParts.slice(0,-1).join(path.sep), opts))
return false;
}
// make the final directory
fs.mkdirSync(dirpath, opts.mode);
// double check we exist now
if (!this.isDirectory(dirpath))
return false;
this.logSuccess('created ' + path.relative(process.cwd(), dirpath));
return true;
} catch (e) {
this.logError("Error creating directory " + path.relative(process.cwd(), dirpath) + ". " + String(e));
return false;
}
};
/**
* Given a startPath or process.cwd() search upwards calling the predicate
* function for each directory. If the predication function returns true,
* return the current path, otherwise keep searching up until we can't go
* any further. Returns false if no directory is found.
*
*/
module.exports.findDirectoryUp = function findDirectoryUp(predicate, startPath) {
var testDir = startPath || process.cwd();
while (testDir) {
if (predicate(testDir)) {
break;
}
var newDir = path.dirname(testDir);
if (newDir === testDir) {
testDir = false;
} else {
testDir = newDir;
}
}
return testDir;
};
/**
* Given a starting filepath (process.pwd by default) returns the
* absolute path to the root of the project directory, which contains
* the .iron folder.
*/
module.exports.findProjectDirectory = function findProjectDirectory(filepath) {
var self = this;
return this.findDirectoryUp(function predicate(curpath) {
var testPath = path.join(curpath, '.iron');
var isFound = self.isDirectory(path.join(curpath, '.iron'));
return isFound;
}, filepath || process.cwd());
};
/**
* Given a starting filepath, returns the absolute path to
* the app directory of the project. If there is no project
* it returns false.
*/
module.exports.findAppDirectory = function findAppDirectory(filepath) {
var projectDirectory = this.findProjectDirectory(filepath);
return projectDirectory ? path.join(projectDirectory, 'app') : false;
};
/**
* Get a path relative to the currently executing 'iron'
* script.
*/
module.exports.pathFromNpmModule = function pathFromNpmModule(/* path parts */) {
var filepath = _.toArray(arguments).join(path.sep);
// IRON_COMMAND_PATH is set in bin/iron.js. It's the absolute
// path to the iron command executable. From there we can get
// the npm module folder.
return path.join(IRON_COMMAND_PATH, '../', filepath);
};
/**
* Returns an object with the type and engine given a srcpath.
* For example, given foo.bar.baz, returns { type: 'bar', engine: 'baz' }
*/
module.exports.engineAndTypeFromFilepath = function engineAndTypeFromFilepath(srcpath) {
var re = /\.([A-Za-z0-9.]+)$/g;
var matches = srcpath.match(re);
matches = matches && matches[0].split('.', 3);
if (!matches) return { type: undefined, engine: undefined };
// matches can be an array of length 2. I'll assume the last entry is the
// engine, and the one before that the type. If there's only one entry I'll
// assume the engine and type are the same. For example, template.js.js is
// equivalent to template.js
var engine = matches.pop();
var type = matches.pop() || engine;
if (engine)
engine = engine.replace('.', '');
if (type)
type = type.replace('.', '');
this.logDebug('engineAndTypeFromFilepath', matches, srcpath, engine, type);
return {
type: type,
engine: engine
};
};
/**
* If we have a srcpath of foo/bar.js and an engine for js that maps to coffee,
* then rewrite the path to foo/bar.js.coffee.
*
* The schema for templates looks like this:
*
* <template_name>.<type>[.<engine>]
*
* You should omit the engine in the template file name if it's the same as the
* type. So instead of mytemplate.js.js you would just create mytemplate.js and
* it's assumed that the engine is js.
*
* The path paramter to this function is automatically adjusted to look for the
* template file for the configured engine. For example, if your js engine is
* 'coffee' then a src path of '/path/to/template.js' will be translated to
* '/path/to/template.js.coffee'.
*
*/
module.exports.rewriteSourcePathForEngine = function rewriteSourcePathForEngine(/* path parts */) {
var config = CurrentConfig.get();
var srcpath = _.toArray(arguments).join(path.sep);
var engineAndType = this.engineAndTypeFromFilepath(srcpath);
var fileType = engineAndType.type;
if (!config || !config.engines || !config.engines[fileType])
return srcpath;
var engine = config.engines[engineAndType.type];
var findTypeRe = new RegExp('\\.' + fileType + '\\S*$');
this.logDebug('rewriteSourcePathForEngine', engineAndType, findTypeRe, engine, fileType);
if (engine === fileType)
return srcpath.replace(findTypeRe, '.' + fileType);
else
return srcpath.replace(findTypeRe, '.' + fileType + '.' + engine);
return ret;
};
/**
* Given a path of ./foo.bar.baz of the schema ./foo.<type>.<engine>, rewrite
* the destination path to only use the engine extension. For example,
*
* foo.css.less would be rewritten to be foo.less.
*/
module.exports.rewriteDestinationPathForEngine = function rewriteDestinationPathForEngine(/* path parts */) {
var config = CurrentConfig.get();
var destpath = _.toArray(arguments).join(path.sep);
var fileType = this.engineAndTypeFromFilepath(destpath).type;
// get the engine from config for the given type
var engine = config.engines[fileType];
// replace everything after the file type with the new
// engine extensions
var replaceTypeWithEngineRe = new RegExp('\\.' + fileType + '\\S*$');
this.logDebug('rewriteDestinationPathForEngine', destpath, fileType, engine, replaceTypeWithEngineRe);
if (!fileType || !engine)
return destpath;
else
return destpath.replace(replaceTypeWithEngineRe, '.' + engine);
};
/**
* Given a source file with a name that follows this schema:
*
* <template_name>.<type>[.engine]
*
* Iff there is an engine defined, make sure this template file is the right one
* for the given engine. If there is no engine defined, assume the file is okay
* and return true.
*/
module.exports.isTemplateForEngine = function isTemplateForEngine(/* path parts */) {
var config = CurrentConfig.get();
if (!config) {
throw new Error("No configuration so can't determine engines.");
}
var srcpath = _.toArray(arguments).join(path.sep);
var engineAndType = this.engineAndTypeFromFilepath(srcpath);
// if the file doesn't have an extension return true
if (!engineAndType.type || !engineAndType.engine)
return true;
var configuredEngine = config.engines[engineAndType.type];
// if we haven't explicitly declared an engine, assume it's okay
if (!configuredEngine)
return true;
// ok we have a configured engine and a file engine so
// let's see if they match.
return configuredEngine === engineAndType.engine;
}
/**
* Returns a path relative to lib/templates, with the source path
* rewritten to use the appropriate engine. See the rewriteSourcePathForEngine
* method above.
*/
module.exports.pathFromTemplates = function pathFromTemplates(/* path parts */) {
var srcpath = this.rewriteSourcePathForEngine.apply(this, arguments);
// returns the path to the template with the extension
// possibly rewritten for the currently configured engine.
return this.pathFromNpmModule('lib/templates', srcpath);
};
/**
* Returns the absolute path to the given filepath relative to the project
* directory (e.g. my-project/filepath). Returns false if no project
* directory is found.
*/
module.exports.pathFromProject = function pathFromProject(/* path parts */) {
var filepath = _.toArray(arguments).join(path.sep);
var projectDirectory = this.findProjectDirectory();
return projectDirectory ? path.join(projectDirectory, filepath) : false;
};
/**
* Returns the absolute path to the given filepath relative to the app
* directory (e.g. my-project/app/filepath). If no app directory is
* found the function returns false. Rewrites the source path to use
* the appropriate engine. See the rewriteSourcePathForEngine method above.
*/
module.exports.pathFromApp = function pathFromApp(/* path parts */) {
var srcpath = this.rewriteSourcePathForEngine.apply(this, arguments);
var appDirectory = this.findAppDirectory();
this.logDebug('pathFromApp', appDirectory, srcpath);
return appDirectory ? path.join(appDirectory, srcpath) : false;
};
/**
* Returns true if the filepath is a file.
*/
module.exports.isFile = function isFile(filepath) {
try {
return fs.statSync(filepath).isFile();
} catch (e) {
return false;
}
};
/**
* Returns true if the dirpath is a directory.
*/
module.exports.isDirectory = function isDirectory(dirpath) {
try {
return fs.statSync(dirpath).isDirectory();
} catch (e) {
return false;
}
};
/**
* Given a file return an array of each line in the file.
*/
module.exports.readFileLines = function readFileLines(filepath) {
var raw = fs.readFileSync(filepath, 'utf8');
var lines = raw.split(/\r*\n\r*/);
while (lines.length) {
var line = lines[lines.length - 1];
if (line.match(/\S/))
break;
lines.pop();
}
return lines;
};
/**
* Returns a compiled template from srcpath using the ejs templating.
* A data context can be passed as the last paramter. This function
* let's us get a compiled template result without writing to a file. It
* can be useful when you want to inject some template content into an
* existing file, for example.
*/
module.exports.templateContent = function templateContent(srcpath, context) {
var tmplpath = this.pathFromTemplates(srcpath);
if (!this.isFile(tmplpath)) {
throw new Error("Couldn't find a source template in " + JSON.stringify(tmplpath));
}
var contents = fs.readFileSync(tmplpath, 'utf8');
return ejs.render(contents, context || {}).trim();
};
/**
* Compile and write an ejs template from the srcpath to the destpath. The
* destpath is rewritten for the given engine. For example, if your js
* engine is 'coffee' then '/path/to/dest.js' will be rewritten to
* 'path/to/dest.coffee'
*/
module.exports.template = function template(srcpath, destpath, context) {
var renderedTemplate = this.templateContent(srcpath, context) + '\n';
destpath = this.rewriteDestinationPathForEngine(destpath);
var ret = this.createFile(destpath, renderedTemplate);
this.logDebug('template', destpath);
return ret;
};
/**
* Returns an array of all directory entries resursively.
*/
module.exports.directoryEntries = function directoryEntries(srcpath) {
var self = this;
var entries = [];
fs.readdirSync(srcpath);
_.each(fs.readdirSync(srcpath), function(entry) {
var fullEntryPath = path.join(srcpath, entry);
entries.push(fullEntryPath)
if (self.isDirectory(fullEntryPath))
entries = entries.concat(self.directoryEntries(fullEntryPath));
});
return entries;
};
/**
* Recursively copies a template directory (from the lib/templates directory) to
* the dest path following these rules:
*
* 1. All folders in the srcpath are copied to destpath, building the folder
* hierarchy as needed.
* 2. Files matching the current engines (e.g. html/jade, js/coffee,
* css/scss/less) are copied. Files not matching any current engines are
* ignored.
* 3. All files are compiled through ejs with the data context optionally
* provided as a last parameter.
*/
module.exports.copyTemplateDirectory = function copyTemplateDirectory(srcpath, destpath, context) {
var self = this;
var fullSourcePath = this.pathFromTemplates(srcpath);
function withoutTemplatePath(fullpath) {
return fullpath.replace(self.pathFromTemplates(), '');
}
function toDestPath(src) {
return src.replace(fullSourcePath, destpath);
}
// first create the destination directory
if(!self.createDirectory(destpath, context)) {
this.logError("Unable to create destination directory " + JSON.stringify(destpath));
return false;
}
_.each(fs.readdirSync(fullSourcePath), function(srcEntryName) {
var fullEntryPath = path.join(fullSourcePath, srcEntryName);
var relSrcPath = withoutTemplatePath(fullEntryPath);
var destPath = toDestPath(fullEntryPath);
if (self.isDirectory(fullEntryPath)) {
// recurse into the directory but we have to remove the
// templates path prefix. So 'lib/templates/app/both' becomes 'app/both'
self.copyTemplateDirectory(relSrcPath, destPath, context);
} else if (self.isFile(fullEntryPath) && self.isTemplateForEngine(fullEntryPath)) {
// copy over the file
self.template(relSrcPath, destPath, context);
}
});
return true;
};
/**
* Inject content at the end of the file. If the file
* doesn't exist yet, create it.
*/
module.exports.injectAtEndOfFile = function injectAtEndOfFile(filepath, content) {
var fileContent;
if (!this.isFile(filepath)) {
return this.createFile(filepath, content);
} else {
fileContent = fs.readFileSync(filepath, 'utf8');
fileContent = fileContent + '\n' + content;
fs.writeFileSync(filepath, fileContent);
this.logSuccess('updated ' + filepath);
return true;
}
};
/**
* Inject content at the end of the section which begins with the 'begin'
* parameter and ends with 'end' parameter. The begin and end parameters
* should be regular expressions.
*/
module.exports.injectIntoFile = function injectIntoFile(filepath, content, begin, end) {
if (!this.isFile(filepath)) {
this.logError("No file found to inject content into at path " + JSON.stringify(filepath));
return false;
}
var raw = fs.readFileSync(filepath, 'utf8');
// matches a beginning, anything in the middle, and the end
var anything = '[\\s\\S]*';
var group = function (exp) {
return '(' + exp + ')';
};
var re = new RegExp(
group(anything) +
group(begin.source) +
group(anything) +
group(end.source) +
group(anything),
'i'
);
// [0:before, 1:begin, 2:middle, 3:end, 4:after]
var allParts = re.exec(raw);
if (!allParts)
throw new Error("injectIntoFile didn't find a match: " + re.source);
var parts = allParts.slice(1);
parts.splice(3, 0, content);
fs.writeFileSync(filepath, parts.join(''));
this.logSuccess('updated ' + filepath);
return true;
};