-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
338 lines (289 loc) · 9.68 KB
/
index.js
File metadata and controls
338 lines (289 loc) · 9.68 KB
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
#!/usr/bin/env node
//@ts-check
'use strict';
//@ts-ignore
const Mustache = require('mustache');
const File_System = require('fs');
const Path = require('path');
const Os = require('os');
/**
* Copy of View configurations that is passed about
* @extends Helpers
* @typedef {Object} View
* @see Helpers - For helper functions that are added to View
* @property {boolean} ghf - GitLab/Hub Flavored MarkDown
* @property {string} name -
* @property {string} author -
* @property {string} description -
* @property {string} license -
* @property {string} code_of_conduct -
* @property {string[]} languages -
* @property {string} funding.key -
* @property {string} funding.url -
* @property {Object[]} files.in_path -
* @property {Object[]} files.out_path -
* @property {Object[]} files.partials -
*/
//@ts-ignore
const View = require('./dataView.json');
/**
* Adds helper functions to `View` configurations
* @typedef {Object} Helpers
* @interface Helpers
*/
const Helpers = {
/**
* @returns {string} - Results from `Mustache.render` on found license `include.mustache` file
* @this {View|View.license} - AKA the `license` value from `dataView.json` configurations
*/
includeLicense: function() {
let license_include_path;
if (typeof this === 'object') {
license_include_path = Path.join(__dirname, '.mustache', 'licenses', this.license, 'include.mustache');
} else {
license_include_path = Path.join(__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);
return Mustache.render(license_data.toString(), View);
},
/**
* @function includeConduct
* @returns {render_callback}
* @this {View} - AKA `dataView.json` configurations
*/
includeConduct: function() {
let conduct_include_path;
if (typeof this === 'object') {
conduct_include_path = Path.join(__dirname,
'.mustache',
'codes_of_conduct',
this.code_of_conduct,
'include.mustache');
} else {
conduct_include_path = Path.join(__dirname,
'.mustache',
'codes_of_conduct',
this,
'include.mustache');
}
if (!File_System.existsSync(conduct_include_path)) {
throw new ReferenceError(`Cannot find conduct file -> ${conduct_include_path}`);
}
const conduct_data = File_System.readFileSync(conduct_include_path);
return Mustache.render(conduct_data.toString(), View);
},
/**
* @function stripForID
* @returns {render_callback}
* @this {View} - AKA `dataView.json` configurations
*/
stripForID: () => {
/**
* @function render_callback
* @param {string} text - Text that is encased within Mustache tag
* @param {View} render - Reference to `View` configurations
*/
const render_callback = (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 render_callback;
},
/**
* @function toTitleCase
* @returns {render_callback}
* @this {View} - AKA `dataView.json` configurations
*/
toTitleCase: () => {
/**
* @function render_callback
* @param {string} text - Text that is encased within Mustache tag
* @param {View} render - Reference to `View` configurations
*/
const render_callback = (text, render) => render(text).trim()
.replace(/<[^>]*>?/gm, '--')
.replace(/[^a-zA-Z0-9_\- ]/g, '')
.replace(/ +/g, '-')
.replace(/-/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase());
return render_callback;
},
/**
* @function toLowerCase
* @returns {render_callback}
* @this {View}
*/
toLowerCase: () => {
/**
* @function render_callback
* @param {string} text - Text that is encased within Mustache tag
* @param {View} render - Reference to `View` configurations
*/
const render_callback = (text, render) => render(text).trim().toLowerCase();
return render_callback;
},
/**
* @function renderPartialsPath
* @returns {render_callback}
* @this {View}
*/
renderPartialsPath: () => {
/**
* @function render_callback
* @param {string} text - Text that is encased within Mustache tag
* @param {View} render - Reference to `View` configurations
*/
const render_callback = (text, render) => {
const partials_path_list = render(text).replace(/\//g, Path.sep);
const partials_abs_path = Path.join(__dirname, '.mustache', 'partials', partials_path_list);
if (File_System.existsSync(partials_abs_path)) {
return render(File_System.readFileSync(partials_abs_path).toString());
} else {
console.error(`Cannot find file -> ${partials_abs_path}`);
}
}
return render_callback;
}
};
/**
* Main class for this file
* @param {View} view
* @param {Helpers} helpers
*/
class App {
/**
* @param {any} view
* @param {Object} 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 : __dirname;
}
/**
*
*/
_renderConduct() {
const in_path = Path.join(__dirname,
'.mustache',
'codes_of_conduct',
this.view.code_of_conduct,
'CODE_OF_CONDUCT.md');
const conduct_data = File_System.readFileSync(in_path);
const out_path = Path.join(this.absPath(this.output_directory), 'CODE_OF_CONDUCT.md');
File_System.writeFile(out_path, Mustache.render(conduct_data.toString(), this.view), {'encoding': 'utf8'}, (err) => {
if (err) {
throw err;
}
console.log(`Wrote file -> ${out_path}`);
});
}
/**
* @param {String} directory_path
*/
_makeDirectories(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)) {
console.log(`Making directory -> ${checked_directory_path}`);
File_System.mkdirSync(checked_directory_path);
}
}
});
}
/**
* @param {string[]} partials_paths
*/
_objectifyPartials(partials_paths) {
const objectified_partials = {};
if (!partials_paths) {
return objectified_partials;
}
partials_paths.forEach((partials_path) => {
const {name} = Path.parse(partials_path);
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}`
);
}
/**
*
*/
renderFiles() {
this.view.files.forEach(({in_path, out_path, partials, tags}) => {
const objectified_partials = this._objectifyPartials(partials);
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));
File_System.writeFile(full_out_path, rendered_mustache, {'encoding': 'utf8'}, (err) => {
if (err) {
throw err;
}
console.log(`Wrote file -> ${out_path}`);
});
});
});
if (this.view.code_of_conduct) {
this._renderConduct();
}
if (this.view.license) {
const src_path = Path.join(__dirname, '.mustache', 'licenses', this.view.license, 'LICENSE');
const dest_path = Path.join(this.absPath(this.output_directory), 'LICENSE');
File_System.copyFile(src_path, dest_path, (err) => {
if (err) {
throw err;
}
console.log(`Copied file to -> ${dest_path}`);
});
}
}
}
const main_app = new App(View, Helpers);
main_app.renderFiles();
/**
* Returns call back function that parses text encapsulated by mustache tags
* @callback render_callback
* @param {string} text - Text that is encased within Mustache tag
* @param {View} render - Reference to `View` configurations
*/