This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
inquirer.js
358 lines (311 loc) · 10.9 KB
/
inquirer.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
var gulp = require('gulp'),
conflict = require('gulp-conflict'),
template = require('gulp-template'),
rename = require('gulp-rename'),
str = require('underscore.string'),
inquirer = require('inquirer'),
fs = require('fs'),
join = require('path').join,
_ = require('underscore');
var exec = require('child_process').exec;
var colors = require('colors');
var inq = {};
module.exports = inq;
inq.ask = function ask(prompts, cb) {
inquirer.prompt(prompts,
function(answers) {
if (!answers.appName) {
// empty space so that everyone can see this error msg
console.log();
console.warn("WARNING: you must enter a package name. try it again ;-)");
console.log();
return cb(answers, true);
}
if (!answers.moveon) {
return cb(answers, true);
}
// defaults
answers.tests = answers.tests || false;
if (answers.phantomjs === undefined) {
answers.phantomjs = false;
}
answers.appNameSlug = str.slugify(answers.appName);
// some chars are not valid chars for a variable
answers.appNameVar = answers.appNameSlug.split("-").join("");
answers.appNameVar = answers.appNameVar.split("_").join("");
answers.appNameShort = answers.appNameSlug;
// try to make the name shorter
["-", "_", "."].forEach(function(item) {
if (answers.appNameShort.indexOf(item) >= 0) {
var splitted = answers.appNameShort.split(item);
answers.appNameShort = splitted[splitted.length - 1];
}
});
var d = new Date();
answers.year = d.getFullYear();
answers.date = d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate();
var files = [__dirname + '/templates/**'];
if (answers.license === 'MIT') {
files.push('!' + __dirname + '/templates/LICENSE_BSD');
files.push('!' + __dirname + '/templates/LICENSE_APACHE');
} else if (answers.license === 'Apache-2.0') {
files.push('!' + __dirname + '/templates/LICENSE_BSD');
files.push('!' + __dirname + '/templates/LICENSE_MIT');
} else {
files.push('!' + __dirname + '/templates/LICENSE_MIT');
files.push('!' + __dirname + '/templates/LICENSE_APACHE');
}
if (!answers.vis) {
files.push('!' + __dirname + '/templates/{examples,examples/**}');
files.push('!' + __dirname + '/templates/test/index.html');
}
// check when PhantomJS should be automatically ignored
if (!(answers.vis && answers.tests)) {
answers.phantomjs = false;
}
// only call phantomjs when we have vis + tests
if (!answers.phantomjs) {
files.push('!' + __dirname + '/templates/test/{dom,dom/**}');
}
if (!answers.jshint) {
files.push('!' + __dirname + '/templates/_jshintrc');
}
if (!answers.tests) {
files.push('!' + __dirname + '/templates/{test,test/**}');
}
if (!answers.css) {
files.push('!' + __dirname + '/templates/{css,css/**}');
answers.css = false;
}
//TODO - coverage is not working
answers.coverage = false;
if (!answers.coverage) {
files.push('!' + __dirname + '/templates/{coverage,coverage/**}');
}
answers.keywords = answers.keywords.split(",");
// toLower
answers.keywords = answers.keywords.join('|').toLowerCase().split('|');
// no keywords entered
if (answers.keywords.length === 1 && answers.keywords[0].length === 0) {
answers.keywords = [];
}
// ensure that the package has a biojs keyword
if (!("biojs" in answers.keywords)) {
answers.keywords.push("biojs");
}
for (var key in answers.keywords) {
answers.keywords[key] = answers.keywords[key].trim();
}
answers.keywordList = JSON.stringify(answers.keywords);
answers._scripts = inq.getCommands(answers, files);
answers.scripts = inq.dictToJSON(answers._scripts, files);
answers.devDependencies = inq.dictToJSON(inq.getDevDependencies(answers));
// ignore the example.html by default
files.push('!' + __dirname + '/templates/example.html');
var cbFire = function() {
inq.showHelp(answers);
cb(answers, false);
};
gulp.src(files)
.pipe(template(answers))
.pipe(rename(function(file) {
var appReplace = file.basename.replace(new RegExp('appNameShort', 'g'), answers.appNameShort);
file.basename = appReplace;
// choose the correct license
if (answers.license === 'MIT') {
var mit = file.basename.replace('LICENSE_MIT', 'LICENSE');
file.basename = mit;
} else if (answers.license === 'Apache-2.0') {
var apache = file.basename.replace('LICENSE_APACHE', 'LICENSE');
file.basename = apache;
} else {
var bsd = file.basename.replace('LICENSE_BSD', 'LICENSE');
file.basename = bsd;
}
// hidden files
if (file.basename[0] === '_') {
file.basename = '.' + file.basename.slice(1);
}
// we don't need special test folders if there is no phantomjs
if (answers.tests && !answers.phantomjs) {
if (file.dirname.substring(0, 9) === "test/unit") {
file.dirname = file.dirname.replace("unit", "");
}
}
}))
.pipe(conflict('./'))
.pipe(gulp.dest('./'))
// .pipe(install())
.on('finish', function() {
// do some cleanup on end
var prepub = function() {
var spawn = require('child_process').spawn;
var proc = spawn('npm', ['install']);
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on("close", function(){
cbFire();
});
};
if (answers.tests && !answers.phantomjs) {
fs.rmdir(join(process.cwd(), "test", "unit"), prepub);
} else {
prepub();
}
})
});
};
inq.showHelp = function(answers) {
var scripts = answers._scripts;
var show = function() {
console.log.apply(console, arguments);
};
show();
show("Congrats - these commands are now available");
show();
if (scripts.build !== undefined) {
show("npm run build".green);
show("* will bundle all your files and dependencies into one file (using browserify).");
show();
}
if (scripts.sniper !== undefined) {
show("npm run sniper".green);
show("* runs a local web server");
show("* allows you to test your examples (for the BioJS registry)");
show("* open http://0.0.0.0:9090/examples");
show();
}
if (answers.tests !== undefined) {
show("npm test".green);
show("* will run your unit tests");
show();
if (scripts["test-watch"] !== undefined) {
show("npm test-watch)".green);
show("* will run your unit tests");
show();
}
}
if (scripts.watch !== undefined) {
show("npm run watch".green);
show("* will watch your files and run browserify on file changes");
show();
}
if (scripts.css !== undefined) {
show("npm run css".green);
show("* will bundle all your css files of your package and your dependencies (supports CSS transforms)");
show();
show("npm run watch-css".green);
show("* will listen to file changes and hence rerun the css pipeline)");
show();
}
if (scripts.w !== undefined) {
show("npm run w".green);
show("Runs all these commands in one shell");
show("* `npm run sniper`");
show("* `npm run watch`");
if (scripts.css !== undefined) {
show("* `npm run watch-css`");
}
show();
}
show("For more details see: https://github.com/biojs/slush-biojs");
show();
};
inq.getCommands = function(answers, files) {
var commands = {};
commands.test = "echo 'Error: no test specified' && exit 1";
if (!answers.gulp) {
files.push('!' + __dirname + '/templates/gulpfile.js');
files.push('!' + __dirname + '/templates/test/index.html');
commands.build = "mkdirp build && browserify -r ./:" + answers.appNameSlug + " -o build/" + answers.appNameShort + ".js";
commands["build-browser"] = "npm run build";
commands.prepublish = "npm run build";
if (answers.vis) {
commands.watch = "watchify -r ./:" + answers.appNameSlug + " -v -o build/" + answers.appNameShort + ".js";
}
if (answers.tests) {
commands.test = "mocha";
}
if (answers.jshint) {
commands.lint = "jshint -c .jshintrc lib --verbose";
}
} else {
commands.build = "gulp build";
commands["build-browser"] = "gulp build-browser";
commands["build-browser-min"] = "gulp build-browser-gzip";
commands.install = "gulp build";
commands.watch = "gulp watch";
commands["test-watch"] = "gulp test-watch";
if (answers.tests) {
commands.test = "gulp test";
}
}
if (answers.vis) {
// single quotes don't work on windows
commands.w = 'prunner \\"npm run sniper\\" \\"npm run watch\\"';
commands.sniper = "sniper .";
if (answers.css) {
commands.install += " && npm run css";
commands.w += ' \\"npm run watch-css\\"';
commands.css = "parcelify ./ -c build/bundle.css";
commands["watch-css"] = "parcelify -w ./ -c build/bundle.css --loglevel verbose";
}
}
return commands;
};
inq.getDevDependencies = function(a) {
var devDependencies = {};
// default dependencies
devDependencies["mkdirp"] = "^0.5.0";
devDependencies["browserify"] = "6.x";
if (a.coverage) {
devDependencies["blanket"] = "^1.1.6";
devDependencies["coveralls"] = "^2.11.1";
devDependencies["mocha-lcov-reporter"] = "0.x";
if (a.gulp) {
devDependencies["gulp-coveralls"] = "^0.1.3";
}
}
if (a.vis) {
devDependencies["sniper"] = "0.x";
devDependencies["watchify"] = "^1.0.6";
devDependencies["prunner"] = "1.x";
}
if (a.css) {
devDependencies["parcelify"] = "0.x";
}
if (a.jshint) {
if (a.gulp) {
devDependencies["gulp-jshint"] = "1.x";
} else {
devDependencies["jshint"] = "^2.5.10";
}
}
if (a.tests) {
devDependencies["chai"] = "1.x";
devDependencies["mocha"] = "1.x";
if (a.gulp) {
devDependencies["gulp-mocha"] = "1.x";
}
if (a.phantomjs) {
devDependencies["gulp-mocha-phantomjs"] = "0.x";
}
}
if (a.gulp) {
devDependencies["del"] = "^0.1.3";
devDependencies["gulp"] = "^3.8.8";
devDependencies["gulp-chmod"] = "^1.1.1";
devDependencies["gulp-gzip"] = "^0.0.8";
devDependencies["gulp-rename"] = "^1.2.0";
devDependencies["gulp-streamify"] = "^0.0.5";
devDependencies["gulp-uglify"] = "^1.0.1";
devDependencies["gulp-util"] = "^3.0.1";
devDependencies["vinyl-source-stream"] = "^1.0.0";
}
return devDependencies;
};
inq.dictToJSON = function(dict) {
return _.map(dict, function(val, key) {
return '\t\t"' + key + '": "' + val + '"';
}).join(",\n");
};