/
root.js
executable file
·449 lines (449 loc) · 20.3 KB
/
root.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
446
447
448
449
#!/usr/bin/env node
'use strict';
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const functions_1 = require("../utils/functions");
const timers_1 = require("timers");
const chalk_1 = require("chalk");
// const prompt = require('cli-prompt');
const inquirer = require("inquirer");
const ora = require("ora");
const download = require("download-git-repo");
const selectShell = require("select-shell");
const shell = require("shelljs");
const path = require("path");
const program = require("commander");
const childProcess = require("child_process");
const updateNotifier = require("update-notifier");
const boxen = require("boxen");
const pkg = require("../package.json");
const notifier = updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 // 1 day
});
if (notifier.update) {
console.log(chalk_1.default.blueBright(boxen(`Update available: ${notifier.update.latest}`, { padding: 1 })));
}
// const welcome = `
// ______ ______ ______ __ __ __
// /\\ ___\\ /\\ __ \\ /\\ __ \\ /\\ "-.\\ \\ /\\ \\
// \\ \\ \\____ \\ \\ \\/\\ \\ \\ \\ \\/\\ \\ \\ \\ \\-. \\ \\ \\ \\
// \\ \\_____\\ \\ \\_____\\ \\ \\_____\\ \\ \\_\\\\"\\_\\ \\ \\_\\
// \\/_____/ \\/_____/ \\/_____/ \\/_/ \\/_/ \\/_/
// `;
const welcome = `
_| _ _ |_ _ _ | _ |_
(_|(_)(_)|_)(_)(_)|(_||_)
`;
const TYPE_OF_APP = {
'REACT': 1,
'REACT-NATIVE': 2,
'EXPO': 3,
};
/**
* init
*/
program
.version(pkg.version)
.command('init')
.description('init boilerplate of dooboo generated app.')
.action(function () {
// sed -i 's/original/new/g' file.txt
// https://askubuntu.com/questions/20414/find-and-replace-text-within-a-file-using-commands
console.log(chalk_1.default.cyanBright(welcome));
console.log(chalk_1.default.yellow('Select which app you want to generate from dooboo.'));
const list = selectShell({
pointer: ' ▸ ',
pointerColor: 'yellow',
checked: ' ◉ ',
unchecked: ' ◎ ',
checkedColor: 'blue',
msgCancel: 'No selected options!',
msgCancelColor: 'orange',
multiSelect: false,
inverse: true,
prepend: true
});
var stream = process.stdin;
list.option(' React App with typescript ', TYPE_OF_APP['REACT'])
.option(' React Native App with typescript ', TYPE_OF_APP['REACT-NATIVE'])
.option(' Expo App with typescript ', TYPE_OF_APP['EXPO'])
.list();
list.on('select', function (options) {
console.log(chalk_1.default.yellow('select the name of the app.'));
// console.log(options[0].value);
if (options[0].value === TYPE_OF_APP['NODE']) {
console.log(chalk_1.default.red('sorry we currently do not support node express starter.'));
process.exit(0);
}
inquirer.prompt([{
name: 'value',
message: 'name of your app(camel-case): ',
}]).then(answer => {
const nameOfApp = answer.value;
if (!nameOfApp) {
console.log(chalk_1.default.redBright('please provide name of your app.'));
process.exit(0);
}
else if (!functions_1.isCamelCase(nameOfApp)) {
console.log(chalk_1.default.redBright('app name should be camel-case.'));
process.exit(0);
}
let template = '';
// console.log(options[0].value);
if (options[0].value === TYPE_OF_APP['REACT']) {
template = 'github.com:dooboolab/dooboo-frontend';
}
else if (options[0].value === TYPE_OF_APP['REACT-NATIVE']) {
template = 'github.com:dooboolab/dooboo-native';
}
else if (options[0].value === TYPE_OF_APP['EXPO']) {
template = 'github.com:dooboolab/dooboo-expo';
}
if (!template) {
console.log(chalk_1.default.redBright('There is no template for current choice. Please try again.'));
process.exit(0);
}
const spinner = ora('creating app ' + nameOfApp + '...');
spinner.start();
if (options[0].value === TYPE_OF_APP['REACT-NATIVE']) {
shell.exec(`mkdir ${nameOfApp} && cd ${nameOfApp} && react-native init ${nameOfApp}`);
}
else {
shell.exec(`mkdir ${nameOfApp}`);
}
download(template, `./${nameOfApp}`, null, (err) => {
spinner.stop();
if (err) {
console.log(chalk_1.default.redBright('failed to download repo ' + template + ': ' + err.message.trim()));
process.exit(0);
}
timers_1.setTimeout(function () {
shell.sed('-i', 'dooboo-starter', functions_1.camelCaseToDash(`${nameOfApp}`), `./${nameOfApp}/package.json`);
if (options[0].value === TYPE_OF_APP['REACT-NATIVE']) {
shell.rm('-rf', `${nameOfApp}/.git`);
shell.rm('-rf', `${nameOfApp}/android`);
shell.rm('-rf', `${nameOfApp}/ios`);
shell.cp('-R', `${nameOfApp}/${nameOfApp}/ios`, `${nameOfApp}/ios`);
shell.cp('-R', `${nameOfApp}/${nameOfApp}/android`, `${nameOfApp}/android`);
shell.rm('-rf', `${nameOfApp}/${nameOfApp}`);
shell.sed('-i', 'DOOBOO NATIVE', `${nameOfApp}`, `./${nameOfApp}/src/components/screen/Intro.tsx`);
shell.sed('-i', 'dooboo', `${nameOfApp}`, `./${nameOfApp}/index.js`);
childProcess.execSync(`cd ${nameOfApp} && npm install && react-native link`, { stdio: 'inherit' });
spinner.stop();
console.log(chalk_1.default.greenBright(`Created ${nameOfApp} successfully.`));
console.log(chalk_1.default.greenBright(`cd ${nameOfApp} and npm start. Open up another terminal and npm run ios.`));
}
else {
console.log(chalk_1.default.greenBright(answer.value + ' created.'));
console.log(chalk_1.default.greenBright('cd ' + answer.value + ' and dooboo start.'));
}
process.exit(0);
spinner.stop();
}, 2000);
});
});
});
list.on('cancel', function (options) {
console.log('cancel list, ' + options.length + ' options selected');
process.exit(0);
});
});
program
.command('start')
.description('start the project.')
.action(function () {
return __awaiter(this, void 0, void 0, function* () {
const spinner = ora('configuring project...\n');
spinner.start();
try {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
spinner.stop();
process.exit(0);
return;
}
exists = yield functions_1.fsExists('node_modules');
if (!exists) {
console.log(chalk_1.default.cyanBright('installing dependencies...'));
// childProcess.execSync(`npm install`, {stdio: 'inherit'})
shell.exec(`npm install`, function (code) {
if (code === 0) {
console.log(chalk_1.default.cyanBright('running project...\n'));
shell.exec(`npm run dev`);
// childProcess.execSync(`npm run dev`, {stdio: 'inherit'});
return;
}
console.log(chalk_1.default.redBright('failed installing dependencies. Please try again with npm install.'));
});
return;
}
console.log(chalk_1.default.cyanBright('running project...'));
// shell.exec(`npm start`);
shell.exec(`npm run dev`);
// childProcess.execFileSync('npm', ['start'], {stdio: 'inherit'});
}
catch (err) {
console.log(chalk_1.default.red(err));
console.log(chalk_1.default.redBright('failed installing dependencies. Please try again with npm install.'));
}
finally {
spinner.stop();
process.exit(0);
}
});
});
program
.command('test')
.description('run test for your project.')
.action(function () {
return __awaiter(this, void 0, void 0, function* () {
const spinner = ora('configuring project...');
spinner.start();
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
spinner.stop();
process.exit(0);
return;
}
exists = yield functions_1.fsExists('node_modules');
console.log(chalk_1.default.cyanBright('\nchecking packages...'));
if (!exists) {
console.log(chalk_1.default.cyanBright('installing dependencies...'));
shell.exec(`npm install`, function (code) {
if (code === 0) {
console.log(chalk_1.default.cyanBright('running project...'));
shell.exec(`npm test`);
spinner.stop();
// process.exit(0);
return;
}
console.log(chalk_1.default.redBright('failed installing dependencies. Please try again with npm install.'));
});
return;
}
console.log(chalk_1.default.cyanBright('testing project...'));
// shell.exec(`npm start`);
shell.exec(`npm test`);
spinner.stop();
// process.exit(0);
});
});
program
.command('screen <c>')
.description('generate screen component.')
.action(function (c) {
return __awaiter(this, void 0, void 0, function* () {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
process.exit(0);
return;
}
const camel = functions_1.camelize(c); // inside component is camelCase.
const upperCamel = functions_1.upperCamelize(c); // file name is upperCamelCase.
const componentFile = `./src/components/screen/${upperCamel}.tsx`;
const testFile = `./src/components/screen/__tests__/${upperCamel}.test.tsx`;
exists = yield functions_1.fsExists(componentFile);
if (exists) {
console.log(chalk_1.default.redBright(`${upperCamel} screen already exists. Delete or rename existing component first.`));
process.exit(0);
return;
}
exists = yield functions_1.fsExists('.dooboo/react.js');
let tsx, tsxTest;
if (exists) {
tsx = path.resolve(__dirname, '..', 'templates/react/screen/Screen.tsx');
tsxTest = path.resolve(__dirname, '..', 'templates/react/screen/Screen.test.tsx');
console.log(chalk_1.default.cyanBright(`creating screen component...`));
shell.cp(tsx, componentFile);
shell.cp(tsxTest, testFile);
shell.sed('-i', 'Screen', `${camel}`, testFile);
console.log(chalk_1.default.green(`generated: src/components/screen/${upperCamel}.tsx
testFile: src/components/screen/__tests__/${upperCamel}.test.tsx`));
process.exit(0);
return;
}
exists = yield functions_1.fsExists('.dooboo/react-native.js');
if (exists) {
tsx = path.resolve(__dirname, '..', 'templates/react-native/screen/Screen.tsx');
tsxTest = path.resolve(__dirname, '..', 'templates/react-native/screen/Screen.test.tsx');
console.log(chalk_1.default.cyanBright(`creating screen component...`));
shell.cp(tsx, componentFile);
shell.cp(tsxTest, testFile);
shell.sed('-i', 'Screen', `${camel}`, testFile);
console.log(chalk_1.default.green(`generated: src/components/screen/${upperCamel}.tsx
testFile: src/components/screen/__tests__/${upperCamel}.test.tsx`));
process.exit(0);
}
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. If you deleted any of file in .dooboo, you are not able to use dooboo-cli.'));
process.exit(0);
});
});
program
.command('shared <c>')
.description('generate shared component.')
.action(function (c) {
return __awaiter(this, void 0, void 0, function* () {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
process.exit(0);
return;
}
const camel = functions_1.camelize(c); // inside component is camelCase.
const upperCamel = functions_1.upperCamelize(c); // file name is upperCamelCase.
const componentFile = `./src/components/shared/${upperCamel}.tsx`;
const testFile = `./src/components/shared/__tests__/${upperCamel}.test.tsx`;
exists = yield functions_1.fsExists(componentFile);
if (exists) {
console.log(chalk_1.default.redBright(`${upperCamel} shared already exists. Delete or rename existing component first.`));
process.exit(0);
return;
}
exists = yield functions_1.fsExists('.dooboo/react.js');
let tsx, tsxTest;
if (exists) {
tsx = path.resolve(__dirname, '..', 'templates/react/shared/Shared.tsx');
tsxTest = path.resolve(__dirname, '..', 'templates/react/shared/Shared.test.tsx');
console.log(chalk_1.default.cyanBright(`creating shared component...`));
shell.cp(tsx, componentFile);
shell.cp(tsxTest, testFile);
shell.sed('-i', 'Shared', `${camel}`, testFile);
console.log(chalk_1.default.green(`generated: src/components/shared/${upperCamel}.tsx
testFile: src/components/shared/__tests__/${upperCamel}.test.tsx`));
process.exit(0);
return;
}
exists = yield functions_1.fsExists('.dooboo/react-native.js');
if (exists) {
tsx = path.resolve(__dirname, '..', 'templates/react-native/shared/Shared.tsx');
tsxTest = path.resolve(__dirname, '..', 'templates/react-native/shared/Shared.test.tsx');
console.log(chalk_1.default.cyanBright(`creating shared component...`));
shell.cp(tsx, componentFile);
shell.cp(tsxTest, testFile);
shell.sed('-i', 'Shared', `${camel}`, testFile);
console.log(chalk_1.default.green(`generated: src/components/shared/${upperCamel}.tsx
testFile: src/components/shared/__tests__/${upperCamel}.test.tsx`));
process.exit(0);
}
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. If you deleted any of file in .dooboo, you are not able to use dooboo-cli.'));
process.exit(0);
});
});
program
.command('model <c>')
.description('generate model class.')
.action(function (c) {
return __awaiter(this, void 0, void 0, function* () {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
process.exit(0);
return;
}
const camel = functions_1.camelize(c); // inside component is camelCase.
const upperCamel = functions_1.upperCamelize(c); // file name is upperCamelCase.
const modelFile = `./src/models/${upperCamel}.tsx`;
exists = yield functions_1.fsExists(modelFile);
if (exists) {
console.log(chalk_1.default.redBright(`${upperCamel} model already exists. Delete or rename existing file first.`));
process.exit(0);
return;
}
const tsx = path.resolve(__dirname, '..', 'templates/common/Model.tsx');
shell.cp(tsx, modelFile);
shell.sed('-i', 'Model', `${upperCamel}`, modelFile);
console.log(chalk_1.default.cyanBright(`creating model...`));
console.log(chalk_1.default.green(`generated: src/models/${upperCamel}.tsx`));
process.exit(0);
});
});
program
.command('store <c>')
.description('generate store class.')
.action(function (c) {
return __awaiter(this, void 0, void 0, function* () {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
process.exit(0);
return;
}
const camel = functions_1.camelize(c); // inside component is camelCase.
const upperCamel = functions_1.upperCamelize(c); // file name is upperCamelCase.
const storeFile = `./src/stores/${camel}.tsx`;
exists = yield functions_1.fsExists(storeFile);
if (exists) {
console.log(chalk_1.default.redBright(`${camel} store already exists. Delete or rename existing file first.`));
process.exit(0);
return;
}
const tsx = path.resolve(__dirname, '..', 'templates/common/Store.tsx');
shell.cp(tsx, storeFile);
shell.sed('-i', 'Store', `${upperCamel}`, storeFile);
console.log(chalk_1.default.cyanBright(`creating store...`));
console.log(chalk_1.default.green(`generated: src/stores/${camel}.tsx`));
process.exit(0);
});
});
program
.command('api <c>')
.description('generate file for api call format.')
.action(function (c) {
return __awaiter(this, void 0, void 0, function* () {
let exists = yield functions_1.fsExists('.dooboo');
if (!exists) {
console.log(chalk_1.default.redBright('\nproject is not in dooboo repository. Are you sure you are in correct dir?'));
process.exit(0);
return;
}
const camel = functions_1.camelize(c); // inside component is camelCase.
const upperCamel = functions_1.upperCamelize(c); // file name is upperCamelCase.
const apiFile = `./src/apis/${camel}.tsx`;
exists = yield functions_1.fsExists(apiFile);
if (exists) {
console.log(chalk_1.default.redBright(`${upperCamel} store already exists. Delete or rename existing file first.`));
process.exit(0);
return;
}
const tsx = path.resolve(__dirname, '..', 'templates/common/Api.tsx');
shell.cp(tsx, apiFile);
console.log(chalk_1.default.cyanBright(`creating api file...`));
console.log(chalk_1.default.green(`generated: src/apis/${camel}.tsx`));
process.exit(0);
});
});
program.parse(process.argv);
/**
* RUN help when command is not valid.
*/
if (!program.args.length) {
// show help by default
program.parse([process.argv[0], process.argv[1], '-h']);
process.exit(0);
}
else {
//warn aboud invalid commands
const validCommands = program.commands.map(function (cmd) {
return cmd.name;
});
const invalidCommands = program.args.filter(function (cmd) {
//if command executed it will be an object and not a string
return (typeof cmd === 'string' && validCommands.indexOf(cmd) === -1);
});
// if (invalidCommands.length) {
// console.log('\n [ERROR] - Invalid command: "%s". See "-h or --help" for a list of available commands.\n', invalidCommands.join(', '));
// process.exit(1);
// }
}
//# sourceMappingURL=root.js.map