-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
255 lines (215 loc) · 5.04 KB
/
utilities.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
var ejs = require("ejs");
var fs = require("fs");
var mkdirp = require("mkdirp");
var path = require("path");
var readline = require("readline");
var util = require("util");
var MODE_0666 = parseInt("0666", 8);
var MODE_0755 = parseInt("0755", 8);
/**
* Install an around function; AOP.
*/
function around(obj, method, fn) {
var old = obj[method];
obj[method] = function() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) args[i] = arguments[i];
return fn.call(this, old, args);
};
}
/**
* Install a before function; AOP.
*/
function before(obj, method, fn) {
var old = obj[method];
obj[method] = function() {
fn.call(this);
old.apply(this, arguments);
};
}
/**
* Prompt for confirmation on STDOUT/STDIN
*/
function confirm(msg) {
return new Promise(function(resolve, reject) {
try {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(msg, function(input) {
rl.close();
resolve(/^y|yes|ok|true$/i.test(input));
});
} catch (err) {
reject(err);
}
});
}
/**
* Copy file from template directory.
*/
function copyTemplate(from, to) {
from = path.join(__dirname, "templates", from);
write(to, fs.readFileSync(from, "utf-8"));
}
/**
* Create an app name from a directory path, fitting npm naming requirements.
*
* @param {String} pathName
*/
function createAppName(destinationPath) {
let pathName = path.resolve(destinationPath);
return path.basename(pathName).replace(/[^A-Za-z0-9.()!~*'-]+/g, "-").replace(/^[-_.]+|-+$/g, "").toLowerCase();
}
/**
* Check if the given directory `path` is empty.
*
* @param {String} path
* @param {Function} fn
*/
function emptyDirectory(path) {
return new Promise(function(resolve, reject) {
fs.readdir(path, function(err, files) {
if (err && err.code !== "ENOENT") reject(err);
resolve(!files || !files.length);
});
});
}
/**
* Graceful exit for async STDIO
*/
function getNewExit() {
var _exit = process.exit;
function exit(code) {
// flush output for Node.js Windows pipe bug
// https://github.com/joyent/node/issues/6247 is just one bug example
// https://github.com/visionmedia/mocha/issues/333 has a good discussion
function done() {
if (!draining--) _exit(code);
}
var draining = 0;
var streams = [process.stdout, process.stderr];
exit.exited = true;
streams.forEach(function(stream) {
// submit empty write request and wait for completion
draining += 1;
stream.write("", done);
});
done();
}
return exit;
}
/**
* Get user's answer to a question
*/
function getUserInput(questionString) {
return new Promise(function(resolve, reject) {
try {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(questionString, function(answer) {
rl.close();
resolve(answer);
});
} catch (err) {
reject(err);
}
});
}
/**
* Determine if launched from cmd.exe
*/
function launchedFromCmd() {
return process.platform === "win32" && process.env._ === undefined;
}
/**
* Load template file.
*/
function loadTemplate(name) {
var contents = fs.readFileSync(path.join(__dirname, "templates", name + ".ejs"), "utf-8");
var locals = Object.create(null);
function render() {
return ejs.render(contents, locals);
}
return {
locals: locals,
render: render
};
}
/**
* Mkdir -p.
*
* @param {String} path
* @param {Function} fn
*/
function mkdir(path, fn) {
mkdirp(path, MODE_0755, function(err) {
if (err) throw err;
console.log(" \x1b[36mcreate\x1b[0m : " + path);
fn && fn();
});
}
/**
* Generate a callback function for commander to warn about renamed option.
*
* @param {String} originalName
* @param {String} newName
*/
function renamedOption(originalName, newName) {
return function(val) {
warning(util.format("option `%s' has been renamed to `%s'", originalName, newName));
return val;
};
}
/**
* Display a warning similar to how errors are displayed by commander.
*
* @param {String} message
*/
function warning(message) {
console.error();
message.split("\n").forEach(function(line) {
console.error(" warning: %s", line);
});
console.error();
}
/**
* echo str > path.
*
* @param {String} path
* @param {String} str
*/
function write(path, str, mode) {
fs.writeFileSync(path, str, { mode: mode || MODE_0666 });
console.log(" \x1b[36mcreate\x1b[0m : " + path);
}
const isDirectory = function functionName(source) {
return fs.lstatSync(source).isDirectory();
};
const getDirectories = function(source) {
return fs.readdirSync(source).map(name => path.join(source, name)).filter(isDirectory);
};
module.exports = {
around: around,
before: before,
confirm: confirm,
copyTemplate: copyTemplate,
createAppName: createAppName,
emptyDirectory: emptyDirectory,
getNewExit: getNewExit,
getUserInput: getUserInput,
launchedFromCmd: launchedFromCmd,
loadTemplate: loadTemplate,
mkdir: mkdir,
renamedOption: renamedOption,
warning: warning,
write: write,
getDirectories: getDirectories,
constants: {
MODE_0666: MODE_0666,
MODE_0755: MODE_0755
}
};