-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathutils.js
More file actions
153 lines (127 loc) · 3.67 KB
/
utils.js
File metadata and controls
153 lines (127 loc) · 3.67 KB
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
const fse = require("fs-extra");
const walk = require("walk");
const fs = require("fs");
const path = require("path");
// Make a locally bound path joiner, (bound to generate).
var local = path.join.bind(null, __dirname, "../");
var util = {
pointerRegex: /\s*\*\s*/,
doublePointerRegex: /\s*\*\*\s*/,
readLocalFile: function(filePath) {
return util.readFile(local(filePath));
},
readFile: function(filePath) {
try {
return fs.readFileSync(filePath).toString();
}
catch (unhandledException) {
return "";
}
},
writeLocalFile: function(filePath, content, header) {
return util.writeFile(local(filePath), content, header);
},
writeFile: function(filePath, content, header) {
try {
if (typeof content == "object") {
content = JSON.stringify(content, null, 2)
}
if (header) {
var commentPrefix = ~header.indexOf('.gyp') ? '#' : '//'
content = commentPrefix +
" This is a generated file, modify: generate/templates/templates/" +
header +
"\n\n" +
content;
}
fse.ensureFileSync(filePath);
fse.writeFileSync(filePath, content);
return true;
}
catch (exception) {
return false;
}
},
titleCase: function(str) {
return str.split(/_|\//).map(function(val, index) {
if (val.length) {
return val[0].toUpperCase() + val.slice(1);
}
return val;
}).join("");
},
camelCase: function(str) {
return str.split(/_|\//).map(function(val, index) {
return (index >= 1
? val[0].toUpperCase() + val.slice(1)
: val[0].toLowerCase() + val.slice(1));
}).join("");
},
getFilePathsRelativeToDir: function(dir) {
const files = [];
const walker = walk.walk(dir, { followLinks: false });
if (!util.isDirectory(dir)) {
return Promise.resolve([]);
}
return new Promise(function(resolve, reject) {
walker.on('file', function(root, stat, next) {
files.push(path.relative(dir, path.join(root, stat.name)));
next();
});
walker.on('end', function() {
resolve(files);
});
walker.on('errors', function() {
reject();
});
});
},
isFile: function(path) {
var isFile;
try {
isFile = fse.statSync(path).isFile();
} catch(e) {
isFile = false;
}
return isFile;
},
isDirectory: function(path) {
var isDirectory;
try {
isDirectory = fse.statSync(path).isDirectory();
} catch(e) {
isDirectory = false;
}
return isDirectory;
},
isPointer: function(type) {
return util.pointerRegex.test(type) || util.doublePointerRegex.test(type);
},
isDoublePointer: function(type) {
return util.doublePointerRegex.test(type);
},
syncDirs: function(fromDir, toDir) {
return Promise.all([
util.getFilePathsRelativeToDir(toDir),
util.getFilePathsRelativeToDir(fromDir)
]).then(function(filePaths) {
const toFilePaths = filePaths[0];
const fromFilePaths = filePaths[1];
// Delete files that aren't in fromDir
toFilePaths.forEach(function(filePath) {
if (!util.isFile(path.join(fromDir, filePath))) {
fse.remove(path.join(toDir, filePath));
}
});
// Copy files that don't exist in toDir or have different contents
fromFilePaths.forEach(function(filePath) {
const toFilePath = path.join(toDir, filePath);
const fromFilePath = path.join(fromDir, filePath);
if (!util.isFile(toFilePath) || util.readFile(toFilePath) !== util.readFile(fromFilePath)) {
fse.copy(fromFilePath, toFilePath);
}
});
});
}
};
module.exports = util;