-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreact-generate.js
More file actions
executable file
·215 lines (207 loc) · 7.59 KB
/
Copy pathreact-generate.js
File metadata and controls
executable file
·215 lines (207 loc) · 7.59 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
#! /usr/bin/env node
const shell = require('shelljs');
const fs = require('fs');
const childProcess = require('child_process');
const process = require('process');
const _ = require('lodash');
const path = require('path');
const COMPONENT = 'component';
const CONTAINER = 'container';
const WEBPACK_BASE_BABEL = 'webpackBaseBabel';
const TEST_UTIL = 'tUtil';
const LOADABLE = 'loadable';
const INJECT_SAGA = 'injectSaga';
const generator = path.join(__dirname, '../generators/react/index.js');
const plopGen = ['--plopfile', generator];
const [, , ...args] = process.argv;
let commandLineArgs = args.toString().split(',');
const stdioInherit = { stdio: 'inherit' };
function execShell(commandArray) {
childProcess.execFileSync(
path.join(__dirname, '../node_modules/.bin/plop'),
commandArray,
{ ...stdioInherit },
);
}
// validate input
if (!commandLineArgs[0]) {
shell.exec(
`echo Sorry! react-generate requires an argument to be passed. Run react-generate --help for more details`,
);
return;
}
// get the type of generator
shell.env.GENERATOR_TYPE = _.includes(commandLineArgs[0], 't')
? 'existing'
: 'new';
let directoryName = 'react-template';
switch (commandLineArgs[0]) {
case 'init':
shell.exec(
`git clone https://github.com/wednesday-solutions/react-template`,
);
shell.cd(process.cwd());
if (commandLineArgs[1]) {
shell.exec(`mkdir ${commandLineArgs[1]}`);
fs.rename(`react-template`, commandLineArgs[1], err => {
if (err) {
throw new Error('Error while renaming');
}
});
directoryName = commandLineArgs[1];
const json = path.join(__dirname, '../node_modules/.bin/json');
shell.exec(
`${json} -I -f ${directoryName}/package.json -e "this.name='${directoryName}'"`,
);
shell.exec(
`${json} -I -f ${directoryName}/package.json -e "this.homepage='""'"`,
);
shell.exec(
`${json} -I -f ${directoryName}/package.json -e "this.author='""'"`,
);
shell.exec(
`${json} -I -f ${directoryName}/package.json -e "this.repository='{}'"`,
);
commandLineArgs = _.drop(commandLineArgs);
}
shell.cd(directoryName);
shell.exec(`git remote remove origin`);
execShell([
'-f',
...plopGen,
WEBPACK_BASE_BABEL,
..._.drop(commandLineArgs),
]);
shell.exec(`npm run initialize`);
break;
case 'gt':
execShell(plopGen);
break;
case 'gtcom':
execShell([...plopGen, COMPONENT, ..._.drop(commandLineArgs)]);
break;
case 'gtcon':
execShell([...plopGen, CONTAINER, ..._.drop(commandLineArgs)]);
break;
case 'gtf':
execShell(['-f', ...plopGen, ..._.drop(commandLineArgs)]);
break;
case 'gtcomf':
execShell(['-f', ...plopGen, COMPONENT, ..._.drop(commandLineArgs)]);
break;
case 'gtconf':
execShell(['-f', ...plopGen, CONTAINER, ..._.drop(commandLineArgs)]);
break;
case 'g':
execShell([...plopGen, ..._.drop(commandLineArgs)]);
break;
case 'gcom':
execShell([...plopGen, COMPONENT, ..._.drop(commandLineArgs)]);
break;
case 'gcon':
execShell([...plopGen, CONTAINER, ..._.drop(commandLineArgs)]);
break;
case 'gf':
execShell(['-f', ...plopGen, ..._.drop(commandLineArgs)]);
break;
case 'gcomf':
execShell(['-f', ...plopGen, COMPONENT, ..._.drop(commandLineArgs)]);
break;
case 'gconf':
execShell(['-f', ...plopGen, CONTAINER, ..._.drop(commandLineArgs)]);
break;
case 'gtutil':
execShell(['-f', ...plopGen, TEST_UTIL, ..._.drop(commandLineArgs)]);
break;
case 'gloadable':
execShell(['-f', ...plopGen, LOADABLE, ..._.drop(commandLineArgs)]);
break;
case 'ginjectsaga':
execShell(['-f', ...plopGen, INJECT_SAGA, ..._.drop(commandLineArgs)]);
break;
case '--help':
shell.echo(
`Generate tests for existing and new react components\n\n` +
`init: Create a new react application\n` +
`gt: Creating a test for a container or component\n` +
`gtf: Forcefully creating a test for a container or component\n` +
`gtcom: Creating a test for an existing component\n` +
`gtcomf: Forcefully creating a test for an existing component\n` +
`gtcon: Creating a test for an existing container\n` +
`gtconf : Forcefully creating a test for an existing component\n` +
`g: Creating a container or component\n` +
`gf: Forcefully creating a container or component\n` +
`gcom: Creating a component\n` +
`gcomf: Forcefully creating a component\n` +
`gcon: Creating a container\n` +
`gconf: Forcefully creating a container\n` +
`--all: Adding tests for all existing containers or components.\n` +
`gtutil: Create a test util file with some test utility functions.\n` +
`gloadable: Create a loadable utility file that uses lazy and Suspense from React to lazyload your containers.\n` +
`ginjectsaga: Create an injector for sagas that work with hooks.\n\n` +
`-------\n\n` +
`Creating a test by specifying type, path and name: react-generate gt component src/app Button\n` +
`Creating a test for an existing component by specifying path and name: react-generate gtcon src/app Button\n` +
`Creating a test for an existing container by specifying path and name: react-generate gtcom src/app HomePage\n` +
`Creating a component/container by specifying type, path and name: react-generate g component src/app Button\n` +
`Creating a component by specifying path and name: react-generate gcon src/app Button\n` +
`Creating a container by specifying path and name: react-generate gcom src/app HomePage\n` +
`Generate test for all components in directory: react-generate --all component src/app/components\n` +
`Generate test for all containers in directory: react-generate --all container src/app/containers`,
);
break;
case '--all':
{
commandLineArgs = _.drop(commandLineArgs);
if (!commandLineArgs[0] || !commandLineArgs[1] || commandLineArgs[2]) {
shell.exec(
`echo Sorry! react-generate --all requires 2 commandLineArgs to be passed. Run react-generate --help for more details`,
);
return;
}
let cwd;
let directories;
switch (commandLineArgs[0]) {
case COMPONENT:
cwd = shell.exec('pwd').stdout;
shell.cd(`./${commandLineArgs[1]}`);
directories = shell.ls();
shell.cd(cwd);
directories.forEach(component => {
if (!_.includes(component, '.')) {
shell.echo(`Component name: ${component}`);
childProcess.execSync(
`react-generate gtcomf ${_.drop(commandLineArgs)} ${component}`,
{
...stdioInherit,
},
);
}
});
break;
case CONTAINER:
cwd = shell.exec('pwd').stdout;
shell.cd(`./${commandLineArgs[1]}`);
directories = shell.ls();
shell.cd(cwd);
directories.forEach(component => {
if (!_.includes(component, '.')) {
shell.echo(`Container name: ${component}`);
childProcess.execSync(
`react-generate gtconf ${_.drop(commandLineArgs)} ${component}`,
{
...stdioInherit,
},
);
}
});
break;
default:
shell.exec(`echo ${commandLineArgs[0]} is not a valid argument`);
}
}
break;
default:
shell.exec(`echo Sorry ${commandLineArgs[0]} is not a valid command`);
break;
}