-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
84 lines (62 loc) · 2 KB
/
file.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
/**
* Module dependencies
*/
var path = require('path');
var _ = require('lodash');
var fsx = require('fs-extra');
var reportback = require('reportback')();
/**
* Generate a file using the specified string.
*
* @option {String} rootPath
* @option {String} contents - the string contents to write to disk
* [@option {Boolean} force=false]
* [@option {Boolean} dry=false]
*
* @sb success
* @sb error
* @sb invalid
* @sb alreadyExists
*/
module.exports = function(options, sb) {
// provide default values for switchback
sb = reportback.extend(sb, {
alreadyExists: 'error'
});
// Validate options and provide defaults.
if (_.isUndefined(options.contents)) {
return sb.invalid(new Error('Inconsistencia: `contents` es requerido'));
}
if (_.isUndefined(options.rootPath)) {
return sb.invalid(new Error('Inconsistencia: `rootPath` es requerido'));
}
_.defaults(options, {
force: false
});
// In case we ended up here w/ a relative path,
// resolve it using the process's CWD
var rootPath = path.resolve(process.cwd(), options.rootPath);
// Only override an existing file if `options.force` is true
fsx.exists(rootPath, (exists) => {
if (exists && !options.force) {
return sb.alreadyExists('Un archivo ya existe en ::' + rootPath);
}
// Don't actually write the file if this is a dry run.
if (options.dry) { return sb.success(); }
// Delete existing file if necessary
(function _deleteExistingFileMaybe(proceed){
if (!exists) { return proceed(); }
fsx.remove(rootPath, (err) => {
if (err) { return proceed(err); }
return proceed();
});
})((err) => {
if (err) { return sb(err); }
// console.log('about to generate a file @ `'+rootPath+'`:',options.contents);
fsx.outputFile(rootPath, options.contents, (err) => {
if (err) { return sb(err); }
return sb();
});//</fsx.outputFile()>
});//</self-calling function :: _deleteExistingFileMaybe()>
});//</fsx.exists()>
};