/
index.js
173 lines (148 loc) · 3.79 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
var fs = require('fs');
var join = require('path').join;
var prompt = require('promptly').prompt;
var chalk = require('chalk');
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 = join(this.templates, name);
this.contentPath = this.path + '/content';
this.mod = require(this.path);
this.values = {
year: new Date().getFullYear()
};
this.directories = {};
}
/**
* 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);
self.values.project = dest;
self.values.description = self.description;
self.dest = dest;
// print new line for pretties.
self.logger.log();
function parseLocal() {
var desc;
var key = keys.shift();
function done(err, value) {
if (err) {
return callback(err);
}
self.values[key] = String(value).trim();
parseLocal();
}
if (key && !self.values[key]) {
desc = vars[key];
if ('function' === typeof desc) {
desc(self.values, done);
} else {
prompt(chalk.gray(' ' + desc.trim()), done);
}
} else if (key === undefined) {
if (!self.dest) {
self.dest = self.values.project;
}
self.create();
if (callback) callback();
} else {
done(null, self.values[key]);
}
}
parseLocal();
};
/**
* Return the files for this template.
*
* @return {Array}
* @api private
*/
Template.prototype.files = function() {
var self = this;
var files = [];
(function readdirs(dir) {
fs.readdirSync(dir).forEach(function(file){
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
}
self.logger.log();
self.files().forEach(function(file){
var uri = self.parse(file);
var out = join(self.dest, uri.replace(self.contentPath, ''));
out = out.replace(".Xignore", ".gitignore");
// directory
if (self.directories[file]) {
try {
fs.mkdirSync(out, 0775);
self.logger.log(
chalk.gray(' create :'),
chalk.cyan(out));
} catch (err) {
// ignore
}
// file
} else {
if (!fs.existsSync(out)) {
var str = self.parse(fs.readFileSync(file, 'utf8'));
fs.writeFileSync(out, str);
self.logger.log(
chalk.gray(' create :'),
chalk.cyan(out));
}
}
});
self.logger.log();
};
/**
* Parse `str`.
*
* @return {String}
* @api private
*/
Template.prototype.parse = function(str){
var self = this;
return str
.replace(/\{\{([^}]+)\}\}/g, function(_, key){
return self.values[key];
});
};
module.exports = Template;