-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildUtils.js
156 lines (122 loc) · 4.3 KB
/
buildUtils.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
//
// Authored by: Ryan Lindgren
// Date Created: Jan. 13, 2014
//
var fs = fs || require('fs');
var util = util || require('util');
var path = path || require('path');
var uu = require('lodash');
// --------------------------------
// arity: 2 || 3
// --------------------------------
// dirList : list :
// - [ basePathA, basePathB, ... ]
// stringsList : matrix : list
// - [ [ currentA, replacementA ], [ currentB, replacementB ], ... ]
// - OR
// - [ actualA, actualB, ... ]
// replacementList : list (optional)
// - [ replacementA, replacementB, ... ]
//
module.exports.recursiveReplace = function recursiveReplace (dirList, stringsMatrix, replacementList) {
for (var i = 0; i < dirList.length; i++) {
var startDir = path.resolve(dirList[i]);
(function walk (start) {
fs.readdirSync(start).forEach(function (f) {
var filePath = path.join(start, f);
for (var y = 0; y < stringsMatrix.length; y++) {
var oldString = stringsMatrix[y][0];
var newString;
if (!replacementList) newString = stringsMatrix[y][1]
else newString = replacementList[y][0];
if (!fs.lstatSync(filePath).isDirectory())
fs.writeFileSync(filePath, fs.readFileSync(filePath).toString().replace(oldString, newString));
else walk(filePath);
}
});
})(startDir);
}
};
// --------------------------------
// arity: 1 || 5
// --------------------------------
// takes 5 parameters || takes one list || takes one object with 5 named properties
//
// filePath : string : list : object
// - path of the file to read
// newPath : string : bool
// - if truthy, should be a path string
// start : string
// - block begins
// end : string
// - block ends
// content : string
// - replacement content
//
module.exports.replaceBlock = function replaceBlock (filePath, newPath, start, end, content) {
if (Array.isArray(filePath))
uu.extend(this, uu.zipObject(['filePath', 'newPath', 'start', 'end', 'content'], filePath));
if (typeof filePath === 'object')
uu.extend(this, filePath);
var fileContentString = fs.readFileSync(this['filePath']).toString();
if (this['newPath']) this['filePath'] = this['newPath'];
var block = fileContentString.substring(fileContentString.indexOf(this['start']) + 1, fileContentString.indexOf(this['end']));
fs.writeFileSync(this['filePath'], fileContentString.replace(block, this['content']));
};
// --------------------------------
// arity: 2 || 3 || 4
// --------------------------------
// basePath : string
// - base directory
// exts : string : list
// - file extensions to capture
// relativeBase : string (optional)
// - relative base directory
// leadingSlash : bool (optional)
// - has leading slash
//
module.exports.recursiveCollectPaths = function recursiveCollectPaths (basePath, exts, relativeBase, leadingSlash, ignore) {
basePath = path.resolve(basePath);
return uu.flatten(Array.prototype.concat(exts).map(function (ext) {
var pathList = [];
(function walk (start) {
fs.readdirSync(start).forEach(function (f) {
var filePath = path.join(start, f);
if (fs.lstatSync(filePath).isFile()) {
if (!exts || path.extname(filePath).split('.').pop() === ext) {
pathList.push(filePath.slice(
filePath.indexOf(relativeBase || filePath) + (relativeBase ? relativeBase.length : 0) + (leadingSlash ? 0 : 1),
filePath.length
));
}
}
else if (fs.lstatSync(filePath).isDirectory()) {
console.log('isDirectory');
if (ignore) {
if (Array.prototype.concat(ignore).reduce(function (a, b) { return a && (filePath.indexOf(b) < 0) }, true)) walk(filePath);
}
else walk(filePath);
}
});
})(basePath);
return pathList;
}));
};
module.exports.setRelativeTo = function setRelativeTo (paths, relDir, leadingSlash) {
var res = [].concat(replacements).map(function (f) {
return f.replace(f.index(relDir) + relDir.length + (leadingSlash ? 0 : 1), f.length);
});
if (typeof paths === 'string') return res.join('');
return res;
};
// --------------------------------
// arity: 2
// --------------------------------
// target : string
// - target string
// replacements : string : list
// - replacements string or list
//
module.exports.format = function format (target, replacements) {
return [].concat(replacements).map(function (f) { return util.format(target, f) }).join('');
};