-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathns-init.js
70 lines (60 loc) · 1.63 KB
/
ns-init.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
#!/usr/bin/env node
var program = require('commander');
var path = require('path');
var fs = require('fs');
var prompt = require('prompt');
var async = require('async');
prompt.message = prompt.delimiter = '';
var yesNoPromptInfo = {
description: 'y/n',
pattern: /[y|n]/,
message: 'Enter "y" or "n"'
};
program.parse(process.argv);
var args = program.args;
var initPath = path.resolve(args.length ? args[0] : '');
console.log('Path: ' + initPath);
if (!fs.existsSync(initPath)) {
console.log('Path not exist. Create directory? (Yes/No)');
prompt.get(yesNoPromptInfo, function(err, result) {
if (result.question === 'n') {
console.log('Init aborted.');
process.exit(0);
}
fs.mkdirSync(initPath);
init();
});
} else {
init();
}
function init() {
var dirs = new Array(97).join().split(',')
.map(function(x, pos) { return path.join(initPath, 'thing_' + ('0' + (pos + 1)).slice(-2)); });
createReadme(initPath);
async.each(dirs, createFolder, function(err) {
if (err) { return console.error(err); }
console.log('Init completed!');
});
}
function createFolder(pth, callback) {
if (!fs.existsSync(pth)) {
fs.mkdir(pth, function(err) {
if (err) { return callback(err); }
createReadme(pth, callback);
});
} else {
createReadme(pth, callback);
}
}
function createReadme(pth, callback) {
var filePath = path.join(pth, 'README.md');
if (!fs.existsSync(filePath)) {
fs.writeFile(filePath, '', function(err) {
if (err) { return callback(err); }
process.stdout.write('.');
callback && callback();
});
} else {
callback && callback();
}
}