1 | const Metalsmith = require("metalsmith");
|
2 | const Handlebars = require("handlebars");
|
3 | const async = require("async");
|
4 | const render = require("consolidate").handlebars.render;
|
5 | const path = require("path");
|
6 | const multimatch = require("multimatch");
|
7 | const getOptions = require("./options");
|
8 | const ask = require("./ask");
|
9 | const filter = require("./filter");
|
10 |
|
11 |
|
12 | Handlebars.registerHelper("if_eq", function(a, b, opts) {
|
13 | return a === b ?
|
14 | opts.fn(this) :
|
15 | opts.inverse(this);
|
16 | });
|
17 |
|
18 | Handlebars.registerHelper("unless_eq", function(a, b, opts) {
|
19 | return a === b ?
|
20 | opts.inverse(this) :
|
21 | opts.fn(this);
|
22 | });
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | module.exports = function generate(name, src, dest, done) {
|
34 | const opts = getOptions(name, src);
|
35 | const metalsmith = Metalsmith(path.join(src, "template"));
|
36 | const data = Object.assign(metalsmith.metadata(), {
|
37 | destDirName: name,
|
38 |
|
39 | inPlace: dest === process.cwd(),
|
40 | noEscape: true,
|
41 | });
|
42 | opts.helpers && Object.keys(opts.helpers).map((key) => {
|
43 | Handlebars.registerHelper(key, opts.helpers[key]);
|
44 | });
|
45 | metalsmith
|
46 | .metadata({
|
47 | createDate: DateFormater.get("yyyy-mm-dd"),
|
48 | year: DateFormater.get("yyyy"),
|
49 | month: DateFormater.get("mm"),
|
50 | })
|
51 | .use(askQuestions(opts.prompts))
|
52 | .use(filterFiles(opts.filters))
|
53 | .use(renderTemplateFiles(opts.skipInterpolation))
|
54 | .clean(false)
|
55 | .source(".")
|
56 | .destination(dest)
|
57 | .build((err) => {
|
58 | done(err);
|
59 | logMessage(opts.completeMessage, data);
|
60 | });
|
61 |
|
62 | return data;
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function askQuestions(prompts) {
|
73 | return function(files, metalsmith, done) {
|
74 | ask(prompts, metalsmith.metadata(), done);
|
75 | };
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function filterFiles(filters) {
|
86 | return function(files, metalsmith, done) {
|
87 | filter(files, filters, metalsmith.metadata(), done);
|
88 | };
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function renderTemplateFiles(skipInterpolation) {
|
100 | skipInterpolation = typeof skipInterpolation === "string" ? [skipInterpolation] :
|
101 | skipInterpolation;
|
102 | return function(files, metalsmith, done) {
|
103 | const keys = Object.keys(files);
|
104 | const metalsmithMetadata = metalsmith.metadata();
|
105 | async.each(keys, (file, next) => {
|
106 |
|
107 | if (skipInterpolation && multimatch([file], skipInterpolation, {
|
108 | dot: true
|
109 | }).length) {
|
110 | return next();
|
111 | }
|
112 | const str = files[file].contents.toString();
|
113 |
|
114 | if (!/{{([^{}]+)}}/g.test(str)) {
|
115 | return next();
|
116 | }
|
117 | render(str, metalsmithMetadata, (err, res) => {
|
118 | if (err) return next(err);
|
119 | files[file].contents = new Buffer(res);
|
120 | next();
|
121 | });
|
122 | }, done);
|
123 | };
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function logMessage(message, data) {
|
134 | if (!message) return;
|
135 | render(message, data, (err, res) => {
|
136 | if (err) {
|
137 | console.error(`\n Error when rendering template complete message: ${err.message.trim()}`);
|
138 | } else {
|
139 | console.log(`\n${res.split(/\r?\n/g).map(line => ` ${line}`).join("\n")}`);
|
140 | }
|
141 | });
|
142 | }
|
143 |
|
144 |
|
145 | var DateFormater = (function () {
|
146 |
|
147 | const text = [];
|
148 | const repAllText = function (t) {
|
149 | const treg = t.match(/('[^']*')|("[^"]*")/ig);
|
150 | if (treg) {
|
151 | for (let i in treg) {
|
152 | t = t.replace(treg[i], `\${${i}}`);
|
153 | text.push(treg[i]);
|
154 | }
|
155 | } else {
|
156 | for (let i in text) {
|
157 | t = t.replace(`\${${i}}`, text[i].replace(/'|"/g, ""));
|
158 | }
|
159 | }
|
160 | return t;
|
161 | };
|
162 | const getDate = function (who, date) {
|
163 | let check = who.toLowerCase(),
|
164 | add = check.match(/[\+-]\d+/),
|
165 | result = "";
|
166 | if (add) add = parseInt(add, 10);
|
167 | else add = 0;
|
168 | switch (check.replace(/[\+-]\d+/, "")) {
|
169 | case "yyyy":
|
170 | date.setFullYear(date.getFullYear() + add);
|
171 | result = date.getFullYear();
|
172 | break;
|
173 | case "yy":
|
174 | result = date.getFullYear().toString().substring(2) + add;
|
175 | break;
|
176 | case "mm":
|
177 | date.setMonth(date.getMonth() + add);
|
178 | result = dn(date.getMonth() + 1);
|
179 | break;
|
180 | case "dd":
|
181 | date.setDate(date.getDate() + add);
|
182 | result = dn(date.getDate());
|
183 | break;
|
184 | case "h":
|
185 | date.setHours(date.getHours() + add);
|
186 | result = dn(date.getHours());
|
187 | break;
|
188 | case "m":
|
189 | date.setMinutes(date.getMinutes() + add);
|
190 | result = dn(date.getMinutes());
|
191 | break;
|
192 | case "s":
|
193 | result = dn(date.getSeconds());
|
194 | break;
|
195 | case "ms":
|
196 | result = tn(date.getMilliseconds());
|
197 | break;
|
198 | }
|
199 | return result;
|
200 | };
|
201 | var dn = function (n) {
|
202 | if (n < 10) return `0${n}`;
|
203 | return n;
|
204 | };
|
205 | var tn = function (n) {
|
206 | if (n < 100 && n >= 10) return `0${n}`;
|
207 | else if (n < 10) return `00${n}`;
|
208 | return n;
|
209 | };
|
210 |
|
211 | return {
|
212 | get(f) {
|
213 | const reg = /(y{4}|y{2}|m{2}|d{2}|H|ms|M|S)([\+-]\d+)?|(?:\'[^\']*\')/g;
|
214 | let vals = f.match(reg),
|
215 | now = new Date();
|
216 | f = repAllText(f);
|
217 | if (vals) {
|
218 | for (const step1 in vals) {
|
219 | if (/[a-zA-Z][\+-]\d+/.test(vals[step1])) getDate(vals[step1], now);
|
220 | }
|
221 | f = f.replace(/(y{4}|y{2}|m{2}|d{2}|H|ms|M|S)[\+-]\d+/ig, "$1");
|
222 | vals = f.match(reg);
|
223 | for (const step2 in vals) f = f.replace(vals[step2], `${getDate(vals[step2], now)}`);
|
224 | }
|
225 | f = repAllText(f);
|
226 | return f;
|
227 | },
|
228 | };
|
229 | }()); |
\ | No newline at end of file |