-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·445 lines (369 loc) · 10.1 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
#! /usr/bin/env node
const path = require('path');
const fs = require('fs-extra');
const log = require('fancy-log');
const packageDirectory = __dirname + path.sep;
const config = require('./lib/config');
const jsdocDefs = require('./lib/jsdocDefs');
const templateFile = 'template.js';
const statelessFile = 'template.stateless.js';
const indexFile = 'index.js';
const backtracker = {};
const initializeArgs = (defaultsArgs) => {
const version = require(`${packageDirectory}package.json`).version;
const ArgumentParser = require('argparse').ArgumentParser;
const parseBool = (value) => {
if(['yes', 'true', 't', 'y', '1'].indexOf(value.toLowerCase()) > -1)
return true;
else if(['no', 'false', 'f', 'n', '0'].indexOf(value.toLowerCase()) > -1)
return false;
else {
log.error(`Could not read value given as boolean: '${value}'`);
process.exit(1);
}
};
const parser = new ArgumentParser({
version,
addHelp: true,
description: 'Compcreate is a command line tool for creating React components easily.',
});
parser.addArgument(['names'], {
help: 'A list of desired component names.',
nargs: '*',
});
parser.addArgument(['-d', '--directory'], {
help: `Creates a new directory for component files.`,
defaultValue: defaultsArgs.createDirectory,
nargs: '?',
const: true,
type: parseBool,
});
parser.addArgument(['-i', '--index'], {
help: `Creates an index.js file for easier importing from directory.`,
defaultValue: defaultsArgs.createIndex,
nargs: '?',
const: true,
type: parseBool,
});
parser.addArgument(['-s', '--stateless'], {
help: `Creates a stateless component that is included inside main component.`,
defaultValue: defaultsArgs.createStateless,
nargs: '?',
const: true,
type: parseBool,
});
parser.addArgument(['-c', '--scss'], {
help: `Creates a SCSS file for custom component styles.`,
defaultValue: defaultsArgs.createScss,
nargs: '?',
const: true,
type: parseBool,
});
parser.addArgument(['-j', '--jsdoc'], {
help: `Inserts JSDoc comments into generated component.`,
defaultValue: defaultsArgs.insertJsdoc,
nargs: '?',
const: true,
type: parseBool,
});
parser.addArgument(['--save-config'], {
help: `Saves configuration used in command as new default.`,
action: 'storeTrue',
});
const args = parser.parseArgs();
// If flags were set but given no value
if(args.directory === null)
args.directory = true;
if(args.index === null)
args.index = true;
if(args.stateless === null)
args.stateless = true;
if(args.scss === null)
args.scss = true;
if(!args.names.length) {
if(!args['save_config'])
parser.printHelp();
process.exit(1);
}
return args;
};
const getFileNameFromPath = (dirPath) => {
const splitPath = dirPath.split(path.sep);
if(splitPath.length > 0)
return splitPath[splitPath.length - 1];
else
return '';
};
const getBaseDirectory = (dirPath) => {
const pwd = process.cwd() + path.sep;
const splitPath = dirPath.split(path.sep);
if(splitPath.length > 0) {
const baseChunks = splitPath.slice(0, splitPath.length - 1);
return pwd + baseChunks.join(path.sep);
}
else
return pwd;
};
const directoryIsEmpty = (dir) => {
return new Promise((resolve, reject) => {
fs.readdir(dir, (err, items) => {
if(err)
reject(err);
else
resolve(items.length === 0);
});
});
};
const replaceInFile = (filePath, params) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if(err)
reject(err);
else {
params.forEach((replaceParams) => {
const reg = new RegExp(replaceParams[0], 'g');
data = data.replace(reg, replaceParams[1]);
});
fs.writeFile(filePath, data, (err) => {
if(err)
reject(err);
else
resolve();
});
}
});
});
};
const cleanupAndExit = () => {
for(const key in backtracker) {
if(Object.hasOwnProperty.call(backtracker, key)) {
const tracker = backtracker[key];
tracker.filesCreated.forEach((file) => {
fs.removeSync(file);
});
tracker.directoriesCreated.forEach((file) => {
fs.removeSync(file);
});
}
}
process.exit(1);
};
const setupDirectory = (dirPath) => {
return new Promise(async (resolve, reject) => {
try {
const directoryExists = await fs.pathExists(dirPath);
const componentName = getFileNameFromPath(dirPath);
const baseDirectory = getBaseDirectory(dirPath);
if(!directoryExists) {
try {
await fs.ensureDir(dirPath);
backtracker[dirPath].directoriesCreated.push(dirPath);
resolve(baseDirectory + path.sep + componentName);
}
catch(err) {
reject(`Could not create directory ${dirPath}: ${err}`);
}
}
else {
try {
const empty = await directoryIsEmpty(dirPath);
if(!empty)
reject(`Directory ${dirPath} not empty.`);
else
resolve(baseDirectory + path.sep + componentName);
}
catch(err) {
reject(`Could not verify that directory ${dirPath} is empty: ${err}`);
}
}
}
catch(err) {
reject(`Could not determine if directory exists: ${err}`);
}
});
};
const setupMainTemplate = (baseDirectory, componentName, args) => {
return new Promise(async (resolve, reject) => {
const mainFilePath = `${baseDirectory + path.sep + componentName}.js`;
const template = `${packageDirectory}template${path.sep + templateFile}`;
try {
await fs.copy(template, mainFilePath);
let replacements = [
['CLASSNAME', componentName],
[
'STATELESS',
args.stateless ?
`import ${componentName}Stateless from './${componentName}.stateless';` : '\n',
],
[
'RETURNCONTENTS',
args.stateless ?
`<${componentName}Stateless />` : '<div></div>',
],
];
for(const key in jsdocDefs) {
if(Object.hasOwnProperty.call(jsdocDefs, key)) {
if(args.jsdoc) {
/*
* JSDoc changes must come before others
* because they may include template variables.
*/
replacements = [
[key, jsdocDefs[key]],
...replacements,
];
}
else {
replacements.push([key, '']);
}
}
}
replacements.push(['\n\n\n', '\n']);
try {
await replaceInFile(mainFilePath, replacements);
resolve(mainFilePath);
}
catch(err) {
reject(`Could not replace contents of template ${mainFilePath}: ${err}`);
}
}
catch(err) {
reject(`Could not copy template ${template}: ${err}`);
}
});
};
const setupIndexTemplate = (baseDirectory, componentName, args) => {
return new Promise(async (resolve, reject) => {
const filePath = `${baseDirectory + path.sep}index.js`;
const template = `${packageDirectory}template${path.sep + indexFile}`;
try {
await fs.copy(template, filePath);
try {
await replaceInFile(filePath, [['CLASSNAME', componentName]]);
resolve(filePath);
}
catch(err) {
reject(`Could not replace contents of template ${filePath}: ${err}`);
}
}
catch(err) {
reject(`Could not copy template ${template}: ${err}`);
}
});
};
const setupStatelessTemplate = (baseDirectory, componentName, args) => {
return new Promise(async (resolve, reject) => {
const filePath = `${baseDirectory + path.sep + componentName}.stateless.js`;
const template = `${packageDirectory}template${path.sep + statelessFile}`;
try {
await fs.copy(template, filePath);
let replacements = [
['CLASSNAME', componentName],
];
for(const key in jsdocDefs) {
if(Object.hasOwnProperty.call(jsdocDefs, key)) {
if(args.jsdoc) {
/*
* JSDoc changes must come before others
* because they may include template variables.
*/
replacements = [
[key, jsdocDefs[key]],
...replacements,
];
}
else {
replacements.push([key, '']);
}
}
}
replacements.push(['\n\n\n', '\n']);
try {
await replaceInFile(filePath, replacements);
resolve(filePath);
}
catch(err) {
reject(`Could not replace contents of template ${filePath}: ${err}`);
}
}
catch(err) {
reject(`Could not copy template ${template}: ${err}`);
}
});
};
const setupScssTemplate = (baseDirectory, componentName, args) => {
return new Promise(async (resolve, reject) => {
const filePath = `${baseDirectory + path.sep + componentName}.scss`;
const exists = await fs.pathExists(filePath);
if(!exists) {
try {
await fs.ensureFile(filePath);
resolve(filePath);
}
catch(err) {
reject(`Could not create ${filePath}: ${err}`);
}
}
});
};
const createReactComponentFiles = async (dirPath, args) => {
backtracker[dirPath] = {
filesCreated: [],
directoriesCreated: [],
};
const filename = getFileNameFromPath(dirPath);
let baseDirectory = getBaseDirectory(dirPath);
if(args.directory) {
try {
baseDirectory = await setupDirectory(dirPath);
}
catch(err) {
log.error(`Could not set up directory: ${err}`);
cleanupAndExit();
}
}
const promises = [];
promises.push(setupMainTemplate(baseDirectory, filename, args));
if(args.index) {
if(!args.directory)
log.warn(`Component not being created with directory. Did not create index file.`);
else
promises.push(setupIndexTemplate(baseDirectory, filename, args));
}
if(args.stateless)
promises.push(setupStatelessTemplate(baseDirectory, filename, args));
if(args.scss)
promises.push(setupScssTemplate(baseDirectory, filename, args));
try {
const createdFiles = await Promise.all(promises);
backtracker[dirPath].filesCreated = [...backtracker[dirPath].filesCreated, ...createdFiles];
log('Successfully created files:');
createdFiles.forEach((file) => {
log(file);
});
}
catch(err) {
log.error(`Uh-oh. Encountered errors while creating files: ${err}`);
cleanupAndExit();
}
};
const main = async () => {
const conf = {...config.defaultConfig, ...(await config.getConfig())};
const args = initializeArgs(conf);
if(args['save_config']) {
conf.createDirectory = args.directory;
conf.createIndex = args.index;
conf.createStateless = args.stateless;
conf.createScss = args.scss;
try {
await config.saveConfig(conf);
log('New configuration saved.');
}
catch(err) {
log.error(`Could not save new configuration: ${err}`);
}
}
for(const dirPath of args.names)
createReactComponentFiles(dirPath, args);
};
main();