-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.js
134 lines (117 loc) · 2.88 KB
/
setup.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
var fs = require('fs');
var prompt = require('prompt');
var template = require('lodash.template');
var merge = require('lodash.merge');
var rmraf = require('rimraf');
var exec = require('child_process').exec;
var argv = process.argv;
var defaults = {
version: '0.0.1',
yourName: 'Fractal',
yourEmail: '[email protected]',
yourDomain: 'http://wearefractal.com',
gitUserName: 'wearefractal'
};
var questions = {
properties: {
projectName: {
pattern: /^[a-zA-Z\-]+$/,
message: 'name must not contain spaces',
required: true
},
description: {},
version: {
default: defaults.version
},
yourName: {
required: true,
default: defaults.yourName
},
yourEmail: {
required: true,
default: defaults.yourEmail
},
yourDomain: {
required: true,
default: defaults.yourDomain
},
gitUserName: {
required: true,
default: defaults.gitUserName
}
}
};
prompt.start();
if (argv[2] == '--use-defaults') {
console.log('Using defaults \n');
var defaultQuestions = {
properties: {
projectName: {
pattern: /^[a-zA-Z\-]+$/,
message: 'name must not contain spaces',
required: true
},
description: {},
}
};
return prompt.get(defaultQuestions, function(err, data) {
data.gitUrl = getGitUrl(data);
data = merge(data, defaults);
save(data);
});
}
prompt.get(questions, function(err, data) {
data.gitUrl = getGitUrl(data);
save(data);
});
function save(data) {
if (data === null || data === undefined) {
return console.log('\n Error: inputs are undefined');
}
data.year = new Date().getFullYear();
var files = [
'./package.json.bk',
'./README.md.bk',
'./LICENSE.bk',
'./test/index.js.bk'
];
files.forEach(function(v, k, a) {
templateFile(v, data);
});
cleanSetup(function(err) {
if (err) return console.error(err);
gitInit(data);
});
}
function getGitUrl(data) {
return 'https://github.com/' + data.gitUserName + '/' + data.projectName;
}
function templateFile(oldName, data) {
var newName = oldName.replace('.bk', '');
fs.writeFileSync(newName, template(fs.readFileSync(oldName))(data));
fs.unlinkSync(oldName);
}
function cleanSetup(cb) {
rmraf('node_modules', function (err) {
if (err) return cb(err);
rmraf('.git', function(err) {
if (err) return cb(err);
fs.unlink('./setup.js', function(err) {
if (err) return cb(err);
cb();
});
});
});
}
function gitInit(data) {
var cmd = 'git init';
cmd += ' && git remote add origin ' + data.gitUrl;
cmd += ' && git add . -A';
cmd += ' && git commit -m \'init\'';
cmd += ' && git push origin master';
cmd += ' && git branch --set-upstream-to=origin/master master';
exec(cmd, function(err) {
if (err) return console.log(err);
console.log('\n Completed successfully');
});
}