This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
254 lines (223 loc) · 6.59 KB
/
index.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
var fs = require('fs');
var path = require('path');
var prompt = require('promptly').prompt;
var extend = require('xtend');
var _ = require('underscore');
function noop() {}
/**
* Initialize a new `Template` with the given `name`.
*
* @param {String} name
* @api private
*/
function Template(name, opts) {
if (!(this instanceof Template)) {
return new Template(name, opts);
}
this.templates = opts.templates;
this.description = opts.description;
this.name = name;
this.logger = opts.logger || {
log: noop
};
this.path = path.join(this.templates, name);
this.contentPath = this.path + '/content';
this.mod = require(this.path + '/index.js');
this.values = extend(opts, {
year: new Date().getFullYear()
});
this.updateJSON = opts['update-json'];
this.directories = {};
this.fromJson = false;
// Allowing overriding of values to be passed in instead of gathering them from user input.
if (Object.keys(opts.jsonValues).length && Object.keys(opts.jsonValues).length > 0) {
this.values = extend(
this.values, opts.jsonValues);
this.fromJson = true;
}
}
/**
* Initialize template at `dest`.
*
* @param {String} dest
* @api private
*/
Template.prototype.init = function(dest, callback) {
var self = this;
var vars = self.mod;
var keys = Object.keys(vars);
if (dest) {
self.values.project = path.basename(dest);
}
self.dest = dest;
// print new line for pretties.
self.logger.log();
function parseLocal() {
var desc;
var key = keys.shift();
self.values.name = self.name;
function done(err, value) {
if (err) {
return callback(err);
}
if (typeof value === 'string') {
value = value.trim();
}
self.values[key] = value;
parseLocal();
}
if (key && !self.values[key]) {
desc = vars[key];
if ('function' === typeof desc) {
desc(self.values, done);
} else {
prompt(' ' + desc.trim(), done);
}
} else if (key === undefined) {
if (!self.dest) {
self.dest = self.values.project;
}
self.variables = Object.keys(vars)
.reduce(function (acc, key2) {
acc[key2] = self.values[key2];
return acc;
}, {});
self.create();
if (callback) callback(null, self.values);
} else {
done(null, self.values[key]);
}
}
if (!self.fromJson) {
parseLocal();
} else {
if (!self.dest) {
self.dest = self.values.project;
}
self.create();
if (callback) callback(null, self.values);
}
};
/**
* Return the files for this template.
*
* @return {Array}
* @api private
*/
Template.prototype.files = function() {
var self = this;
var files = [];
//TODO make this a bit less janky
try {
this.exclusions = require(this.path + '/exclude.js')(this.values);
} catch(e) {
if (e.code !== 'MODULE_NOT_FOUND') { throw e; }
this.exclusions = [];
}
(function readdirs(dir) {
fs.readdirSync(dir).forEach(function(file){
if(self.exclusions.indexOf(file) === -1) {
files.push(file = dir + '/' + file);
var stat = fs.statSync(file);
if (stat.isDirectory()) {
self.directories[file] = true;
readdirs(file);
}
}
});
})(self.contentPath);
return files;
};
/**
* Create the template files.
*
* @api private
*/
Template.prototype.create = function() {
var self = this;
try {
fs.mkdirSync(self.dest, 0775);
} catch (err) {
// ignore
}
var written = false;
self.files().forEach(function(file){
var uri = self.parse(file);
var out = path.join(self.dest,
uri.replace(self.contentPath, ''));
var stat;
out = out
.replace("dotgitignore", ".gitignore")
.replace("dotnpmignore", ".npmignore");
// directory
if (self.directories[file]) {
try {
fs.mkdirSync(out, 0775);
self.logger.log(' create :', out);
} catch (err) {
// ignore
}
// file
} else {
if (!fs.existsSync(out)) {
stat = fs.statSync(file);
var buf = fs.readFileSync(file);
var str = buf.toString();
var parsed = self.parse(str);
// If the parsed content is no different than the original,
// write out the buffer to prevent encoding issues (e.g. images)
var content = str === parsed ? buf : parsed;
fs.writeFileSync(out, content, {
mode: stat.mode
});
if (written === false) {
self.logger.log();
written = true;
}
self.logger.log(' create :', out);
} else if (self.updateJSON && (
out.substr(-5) === '.json' ||
path.basename(out) === '.jshintrc'
)) {
stat = fs.statSync(file);
var srcBuf = fs.readFileSync(out, 'utf8');
var destBuf = self.parse(fs.readFileSync(file, 'utf8'));
if (srcBuf === destBuf) {
return;
}
var srcJSON = JSON.parse(srcBuf);
var destJSON = JSON.parse(destBuf);
var targetJSON = extend(srcJSON, destJSON);
var targetBuf = JSON.stringify(targetJSON, null, ' ');
fs.writeFileSync(out, targetBuf, {
mode: stat.mode
});
if (written === false) {
self.logger.log();
written = true;
}
self.logger.log(' create :', out);
}
}
});
if (written) {
self.logger.log();
}
self.logger.log('finished generating',
self.dest, '\n', self.variables);
};
/**
* Parse `str`.
*
* @return {String}
* @api private
*/
Template.prototype.parse = function(str){
var self = this;
var settings = {
interpolate: /\{\{(.+?)\}\}/g,
evaluate: /\{\%(.+?)\%\}/g
};
return _.template(str, settings)(self.values);
};
module.exports = Template;