1 | var program = require('commander');
|
2 | var appRoot = require('app-root-path');
|
3 | var fs = require('fs');
|
4 |
|
5 | var componentTemplate = require('./templates/component');
|
6 | var specComponentTemplate = require('./templates/spec-component');
|
7 | var classComponentTemplate = require('./templates/class-component');
|
8 | var containerTemplate = require('./templates/container');
|
9 | var storeTemplate = require('./templates/store');
|
10 | var specStoreTemplate = require('./templates/spec-store');
|
11 | var componentMobxTemplate = require('./templates/component-mobx');
|
12 | var specComponentMobxTemplate = require('./templates/spec-component-mobx');
|
13 | var classComponentMobxTemplate = require('./templates/class-component-mobx');
|
14 | var containerMobxTemplate = require('./templates/container-mobx');
|
15 |
|
16 | var utils = require('./utils');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | program
|
22 | .option('-g, --generate <type>, generate a new application element')
|
23 | .option('-n, --name <name>, name for new file')
|
24 | .parse(process.argv)
|
25 |
|
26 | const LOWER = utils.snakeToCamelCase(program.name.toLowerCase());
|
27 | const CAPITAL = utils.toCapitalCase(LOWER);
|
28 |
|
29 | switch (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 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function createAsset(type, fileName, fileTemplate, specTemplate){
|
63 | const assetPath = appRoot + '/src/' + type + '/' + fileName + '/';
|
64 |
|
65 |
|
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 |