UNPKG

3.74 kBJavaScriptView Raw
1var program = require('commander');
2var appRoot = require('app-root-path');
3var fs = require('fs');
4
5var componentTemplate = require('./templates/component');
6var specComponentTemplate = require('./templates/spec-component');
7var classComponentTemplate = require('./templates/class-component');
8var containerTemplate = require('./templates/container');
9var storeTemplate = require('./templates/store');
10var specStoreTemplate = require('./templates/spec-store');
11var componentMobxTemplate = require('./templates/component-mobx');
12var specComponentMobxTemplate = require('./templates/spec-component-mobx');
13var classComponentMobxTemplate = require('./templates/class-component-mobx');
14var containerMobxTemplate = require('./templates/container-mobx');
15
16var utils = require('./utils');
17
18/**
19 * Grab type and name, also provide --help functionality
20 */
21program
22 .option('-g, --generate <type>, generate a new application element')
23 .option('-n, --name <name>, name for new file')
24 .parse(process.argv)
25
26const LOWER = utils.snakeToCamelCase(program.name.toLowerCase());
27const CAPITAL = utils.toCapitalCase(LOWER);
28
29switch (program.generate) {
30 case 'component':
31 createAsset('components', CAPITAL, componentTemplate, specComponentTemplate);
32 break;
33 case 'component:mobx':
34 createAsset('components', CAPITAL, componentMobxTemplate, specComponentMobxTemplate);
35 break;
36 case 'class-component':
37 createAsset('components', CAPITAL, classComponentTemplate, specComponentTemplate);
38 break;
39 case 'class-component:mobx':
40 createAsset('components', CAPITAL, classComponentMobxTemplate, specComponentMobxTemplate);
41 break;
42 case 'container':
43 createAsset('containers', CAPITAL, containerTemplate, specComponentTemplate);
44 break;
45 case 'container:mobx':
46 createAsset('containers', CAPITAL, containerMobxTemplate, specComponentMobxTemplate);
47 break;
48 case 'store':
49 createAsset('stores', LOWER + 'Store', storeTemplate, specStoreTemplate);
50 break;
51 default:
52 console.error(program.generate + ' is not a valid element that can be created');
53 break;
54}
55
56/**
57 * @param { string } type type of asset being created
58 * @param { string } fileName formatted file name for asset
59 * @param { function } fileTemplate template for asset that returns a string template
60 * @param { function } specTemplate spec template for asset that returns a string template
61 */
62function createAsset(type, fileName, fileTemplate, specTemplate){
63 const assetPath = appRoot + '/src/' + type + '/' + fileName + '/';
64
65 // Only writes to pre-existing
66 try {
67 if(!fs.existsSync(appRoot + '/src/')) {
68 fs.mkdirSync(appRoot + '/src/');
69 };
70
71 if(!fs.existsSync(appRoot + '/src/' + type + '/')){
72 fs.mkdirSync(appRoot + '/src/' + type + '/');
73 }
74
75 if(fs.existsSync(assetPath)){
76 throw `${type.toUpperCase()} PATH COLLISION`;
77 } else {
78 fs.mkdirSync(assetPath);
79 console.info(fileName + ' was successfully created!');
80 }
81 } catch(err){
82 console.error(err);
83 console.error(`That ${type} already exists. Delete existing file or choose another name.`);
84 }
85
86 fs.writeFile(assetPath + fileName + '.js', fileTemplate(LOWER, CAPITAL), function(err){
87 if(err){
88 console.error(err);
89 }
90
91 console.info(`${fileName}.js was successfully created!`)
92 });
93
94 fs.writeFile(assetPath + 'spec.js', specTemplate(LOWER, CAPITAL), function(err){
95 if(err){
96 console.error(err);
97 }
98
99 console.info(`${CAPITAL} test file was successfully created!`);
100 });
101
102
103}
\No newline at end of file