-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
483 lines (483 loc) · 17 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env node
'use strict';
const Mustache = require('mustache');
const File_System = require('fs');
const Path = require('path');
const Os = require('os');
const View = require('../dataView.json');
/**
* Trims last element (directory or file) from path
* @function parentDir
* @param {string} directory_or_file_path - Path to trim last element from
* @return {string}
* @example
* ```JavaScript
* parent_directory = parentDir('/tmp/some/where/else');
*
* console.log(parent_directory);
* //> "/tmp/some/where"
* ```
*/
const parentDir = (directory_or_file_path) => {
return directory_or_file_path.replace(/Path.sep$/, '')
.split(Path.sep)
.slice(0, -1)
.join(Path.sep);
};
;
;
/**
* Object that contains helper functions to enhance `View`
* @namespace Helpers
*/
const Helpers = {
/**
* Returns current four digit year as string
* @function getFullYear
* @return {string}
* @memberof Helpers
* @example <caption>Mustache file snip</caption>
* ```Mustache
* Copyright (C) {{{ getFullYear }}} {{ author }}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* Copyright (C) 2020 S0AndS0
* ```
*/
getFullYear: () => {
return new Date().getFullYear().toString();
},
/**
* Includes `include.mustache` file within rendered output
* @throws {ReferenceError} - When license `include.mustache` file cannot be found
* @return {string} - Results from `Mustache.render` on found license `include.mustache` file
* @memberof Helpers
* @this {View|View.license} - AKA the `license` value from `dataView.json` configurations
* @example <caption>Mustache file snip</caption>
* ```Mustache
* {{{ includeLicense }}}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* Quick statement on what this Repository aims to accomplish
* Copyright (C) 2020 your-name
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* ```
*/
includeLicense: function () {
let license_include_path;
if (typeof this === 'object') {
license_include_path = Path.join(parentDir(__dirname), '.mustache', 'licenses', this.license, 'include.mustache');
}
else {
license_include_path = Path.join(parentDir(__dirname), '.mustache', 'licenses', this, 'include.mustache');
}
if (!File_System.existsSync(license_include_path)) {
throw new ReferenceError(`Cannot find license -> ${license_include_path}`);
}
const license_data = File_System.readFileSync(license_include_path);
if (typeof this === 'object') {
return Mustache.render(license_data.toString(), this.view);
}
else {
return Mustache.render(license_data.toString(), View);
}
},
/**
* Strips and/or replaces known bad characters for heading id/name linking
* @extends renderCallback
* @function stripForID
* @return {renderCallback}
* @memberof Helpers
* @example <caption>Mustache file snip</caption>
* ```Mustache
* # {{# toTitleCase }}{{ repository }}{{/ toTitleCase }}
* [heading__top]:
* #{{#stripForID}}{{ repository }}{{/stripForID}}
* "{{ description }}"
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* # Repository Name
* [heading__top]:
* #repository-name
* "Quick statement on what this Repository aims to accomplish"
* ```
*/
stripForID: () => {
const renderCallback = (text, render) => {
if (!!render.gfm) {
return render(text).trim()
.toLowerCase()
.replace(/<[^>]*>?/gm, '--')
.replace(/[^a-z0-9_\- ]/g, '')
.replace(/ +/g, '-');
}
return render(text).trim()
.toLowerCase()
.replace(/[^a-z0-9_\- ]/g, '')
.replace(/ +/g, '-');
};
return renderCallback;
},
/**
*
* @function stripForHeading
* @return {renderCallback}
* @memberof Helpers
* @example <caption>Mustache file snip</caption>
* ```Mustache
* [commits__{{#stripForHeading}}{{ repository }}{{/stripForHeading}}__master]:
* https://github.com/{{ organization }}/{{ repository }}/commits/master
* "📝 History of changes on this branch"
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* [commits__repository_name__master]:
* https://github.com/your-name__or__organization-name/repository-name/commits/master
* "📝 History of changes on this branch"
* ```
*/
stripForHeading: () => {
const renderCallback = (text, render) => {
return render(text).trim().toLowerCase().replace(/ +|\-+/g, '_');
};
return renderCallback;
},
/**
*
* @function stripNewLines
* @return {renderCallback}
* @memberof Helpers
* @example <caption>Mustache file snip</caption>
* ```Mustache
* [badge__gh_pages__{{#stripForHeading}}{{ repository }}{{/stripForHeading}}]:
* https://img.shields.io/website/{{#stripNewLines}}
* {{#gh_pages.url}}{{ gh_pages.url }}{{/gh_pages.url}}
* {{^gh_pages.url}}https/{{ organization }}.github.io/{{ repository }}/index.html{{/gh_pages.url}}
* .svg?down_color=darkorange&down_message=Offline&label=Demo&logo=Demo%20Site&up_color=success&up_message=Online
* {{/stripNewLines}}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* [badge__gh_pages__repository_name]:
* https://img.shields.io/website/https/your-name__or__organization-name.github.io/repository-name/index.html.svg?down_color=darkorange&down_message=Offline&label=Demo&logo=Demo%20Site&up_color=success&up_message=Online
* ```
*/
stripNewLines: () => {
const renderCallback = (text, render) => {
return render(text).replace(/\r?\n|\r/g, '');
};
return renderCallback;
},
/**
*
* @function trimLeadingSpaces
* @return {renderCallback}
* @memberof Helpers
* @todo Unused code
* @example <caption>Mustache file snip</caption>
* ```Mustache
* {{# trimLeadingSpaces }}
* Something to dedent
* {{/ trimLeadingSpaces }}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* Something to dedent
* ```
*/
trimLeadingSpaces: () => {
const renderCallback = (text, render) => {
return render(text).replace(/^\s+/g, '');
};
return renderCallback;
},
/**
*
* @function trimTrailingSpaces
* @return {renderCallback}
* @memberof Helpers
* @todo Unused code
* @example <caption>Mustache file snip</caption>
* ```Mustache
* {{# trimTrailingSpaces }}Something that once had trailing spaces {{/ trimTrailingSpaces }}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* Something that once had trailing spaces
* ```
*/
trimTrailingSpaces: () => {
const renderCallback = (text, render) => {
return render(text).replace(/\s+$/g, '');
};
return renderCallback;
},
/**
*
* @function toTitleCase
* @return {renderCallback}
* @memberof Helpers
* @this {View} - AKA `dataView.json` configurations
* @example <caption>Mustache file snip</caption>
* ```Mustache
* # {{# toTitleCase }}{{ repository }}{{/ toTitleCase }}
* [heading__top]:
* #{{#stripForID}}{{ repository }}{{/stripForID}}
* "{{ description }}"
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* # Repository Name
* [heading__top]:
* #repository-name
* "Quick statement on what this Repository aims to accomplish"
* ```
*/
toTitleCase: () => {
const renderCallback = (text, render) => {
return render(text).trim()
.replace(/<[^>]*>?/gm, '--')
.replace(/[^a-zA-Z0-9_\- ]/g, '')
.replace(/ +/g, '-')
.replace(/-/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase());
};
return renderCallback;
},
/**
*
* @function toLowerCase
* @return {renderCallback}
* @memberof Helpers
* @todo Unused code
* @example <caption>Mustache file snip</caption>
* ```Mustache
* {{# toLowerCase }}String Stripped of{{/ toLowerCase }} Capitalization
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* string stripped of Capitalization
* ```
*/
toLowerCase: () => {
const renderCallback = (text, render) => {
return render(text).trim().toLowerCase();
};
return renderCallback;
},
/**
*
* @function renderPartialsPath
* @return {renderCallback}
* @memberof Helpers
* @todo Unused code
* @example <caption>Mustache file snip</caption>
* ```Mustache
* {{# renderPartialsPath }}quick_start/clone.md.mst{{/ renderPartialsPath }}
* ```
* @example <caption>rendered output snip</caption>
* ```MarkDown
* Clone this project...
* ```
*/
renderPartialsPath: function () {
const renderCallback = (text, render) => {
//@ts-ignore
const partials_path_list = render(text).replace(/\//g, Path.sep);
const partials_abs_path = Path.join(parentDir(__dirname), '.mustache', 'partials', partials_path_list);
if (File_System.existsSync(partials_abs_path)) {
//@ts-ignore
return render(File_System.readFileSync(partials_abs_path).toString());
}
else {
console.error(`Cannot find file -> ${partials_abs_path}`);
}
};
return renderCallback;
}
};
/**
* Main class for this file
*/
class App {
/**
* Constructs internal state for `new` instances of class
* @param {any} view
* @param {Helpers|object} helpers
* @example <caption></caption>
* ```JavaScript
* // Load configurations
* const View = require('../dataView.json');
* // Define helper functions within an object
* const Helpers = {...};
*
* const app = new App(View, Helpers);
* ```
*/
constructor(view, helpers) {
this.view = view;
Object.keys(helpers).forEach((key) => {
if (!(key in view)) {
this.view[key] = helpers[key];
}
});
this.output_directory = view.output_directory ? view.output_directory : parentDir(__dirname);
}
/**
* Make directory path including any intervening directories that do not already exist
* @param {string} directory_path Absolute path to make directories for
* @param {boolean} verbose Output directories that are made when `true`
* @example
* ```JavaScript
* const app = new App(View, Helpers);
*
* app._makeDirectories('/tmp/some/where/else', true);
* ```
*/
_makeDirectories(directory_path, verbose) {
if (!!verbose) {
console.log(`directory_path -> ${directory_path}`);
}
let checked_directory_path = '';
directory_path.split(Path.sep).forEach((directory) => {
if (directory !== '/' && !!directory) {
checked_directory_path += `${Path.sep}${directory}`;
if (!File_System.existsSync(checked_directory_path)) {
if (!!verbose) {
console.log(`Making directory -> ${checked_directory_path}`);
}
File_System.mkdirSync(checked_directory_path);
}
}
});
}
/**
* Translates list of files to object/dict data structure
* @param {string[]} partials_paths List of file paths relative to root directory of this repository
* @param {boolean} verbose Output partial file(s) parsed when `true`
* @return {{key: string, value: string}}
* @throws {Error} when file does not exist or cannot be read
* @example <caption></caption>
* ```JavaScript
* const app = new App(View, Helpers);
*
* const objectified_partials = app._objectifyPartials(
* [
* ".mustache/partials/quick_start/clone.md.mst",
* ".mustache/partials/quick_start/is_submodule.md.mst",
* ".mustache/partials/quick_start/utilizes_submodules.md.mst",
* ".mustache/partials/readme/notes.md.mst"
* ],
* true
* );
*
*
* console.log(objectified_partials);
* // {
* // "clone.md": ".mustache/partials/quick_start/clone.md.mst",
* // "is_submodule.md": ".mustache/partials/quick_start/is_submodule.md.mst",
* // "utilizes_submodules.md": ".mustache/partials/quick_start/utilizes_submodules.md.mst",
* // "notes.md": ".mustache/partials/readme/notes.md.mst"
* // }
* ```
* @example <caption></caption>
* ```Mustache
* {{> notes.md}}
* ```
*/
_objectifyPartials(partials_paths, verbose) {
const objectified_partials = {};
if (!partials_paths) {
return objectified_partials;
}
partials_paths.forEach((partials_path) => {
const { name } = Path.parse(partials_path);
if (!!verbose) {
console.log(`Reading ${name} from -> ${partials_path}`);
}
File_System.readFile(partials_path, (err, data) => {
if (err) {
throw err;
}
objectified_partials[name] = data.toString();
});
});
return objectified_partials;
}
/**
* Returns absolute path
* @param {string} input
* @returns {string}
*/
absPath(input) {
if (input.indexOf('~/') === 0) {
return input.replace(/^~\//, `${Os.homedir()}/`);
}
return input.replace(new RegExp(`^(?!${Path.sep})`), `${__dirname}${Path.sep}`);
}
;
/**
* Writes rendered files to directory configured by `View`
* @throws {Error} when `clobber` is `false` and files would be overwritten
* @example
* ```JavaScript
* app.renderFiles();
* ```
*/
renderFiles() {
this.view.files.forEach(({ in_path, out_path, partials, tags }) => {
const objectified_partials = this._objectifyPartials(partials, this.view.verbose);
File_System.readFile(in_path, (err, data) => {
if (err) {
throw err;
}
const rendered_mustache = Mustache.render(data.toString(), this.view, objectified_partials, tags);
const full_out_path = Path.join(this.absPath(this.output_directory), out_path);
this._makeDirectories(Path.dirname(full_out_path), this.view.verbose);
if (File_System.existsSync(full_out_path) && !this.view.clobber) {
throw new Error(`clobber -> ${this.view.clobber} and file already exists at -> ${full_out_path}`);
}
File_System.writeFile(full_out_path, rendered_mustache, { 'encoding': 'utf8' }, (err) => {
if (err) {
throw err;
}
if (!!this.view.verbose) {
console.log(`Wrote file -> ${out_path}`);
}
});
});
});
if (this.view.license) {
const src_path = Path.join(parentDir(__dirname), '.mustache', 'licenses', this.view.license, 'LICENSE');
const dest_path = Path.join(this.absPath(this.output_directory), 'LICENSE');
if (File_System.existsSync(dest_path) && !this.view.clobber) {
throw new Error(`clobber -> ${this.view.clobber} and file already exists at -> ${dest_path}`);
}
File_System.copyFile(src_path, dest_path, (err) => {
if (err) {
throw err;
}
if (!!this.view.verbose) {
console.log(`Copied file to -> ${dest_path}`);
}
});
}
}
}
const app = new App(View, Helpers);
app.renderFiles();
//# sourceMappingURL=index.js.map