Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
/*
2
* app.js: Common utility functions for working with directories
3
*
4
* (C) 2011, Nodejitsu Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var utile = require('utile'),
10
async = utile.async,
11
mkdirp = utile.mkdirp,
12
rimraf = utile.rimraf;
13
14
var directories = exports;
15
16
//
17
// ### function create (dirs, callback)
18
// #### @dirs {Object} Directories to create
19
// #### @callback {function} Continuation to respond to when complete
20
// Creates all of the specified `directories` in the current environment.
21
//
22
directories.create = function (dirs, callback) {
23
function createDir(dir, next) {
24
mkdirp(dir, 0755, function () {
25
next(null, dir);
26
});
27
}
28
29
if (!dirs) {
30
return callback();
31
}
32
33
async.mapSeries(Object.keys(dirs).map(function (key) {
34
return dirs[key]
35
}), createDir, callback);
36
};
37
38
//
39
// ### function remove (dirs, callback)
40
// #### @dirs {Object} Directories to remove
41
// #### @callback {function} Continuation to respond to when complete
42
// Removes all of the specified `directories` in the current environment.
43
//
44
directories.remove = function (dirs, callback) {
45
function removeDir (dir, next) {
46
rimraf(dir, function () {
47
next(null, dir);
48
});
49
}
50
51
if (!dirs) {
52
return callback();
53
}
54
55
async.mapSeries(Object.keys(dirs).map(function (key) {
56
return dirs[key]
57
}), removeDir, callback);
58
};
59
60
//
61
// ### function normalize (root, dirs)
62
// #### @keys {Object} Set of keys to normalize upon.
63
// #### @dirs {Object} Set of directories to normalize.
64
// Normalizes the specified `dirs` against the relative
65
// `root` of the application.
66
//
67
directories.normalize = function (keys, dirs) {
68
var normalized = {};
69
70
Object.keys(dirs).forEach(function (key) {
71
normalized[key] = dirs[key];
72
Object.keys(keys).forEach(function (constant) {
73
normalized[key] = normalized[key].replace(constant, keys[constant]);
74
});
75
});
76
77
return normalized;
78
};
79
80