1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | const newProject = require('@creuna/create-react-app');
|
5 | const {
|
6 | newComponent,
|
7 | newPage,
|
8 | rename,
|
9 | toStateful,
|
10 | toStateless
|
11 | } = require('@creuna/react-scripts');
|
12 | const semver = require('semver');
|
13 |
|
14 | const configstore = require('./source/configstore');
|
15 | const currentVersion = require('./source/get-this-version');
|
16 | const fetchLatestVersion = require('./source/fetch-latest-version');
|
17 | const getConfig = require('./source/get-config');
|
18 | const lib = require('./source/get-components-from-library');
|
19 | const messages = require('./source/messages');
|
20 | const [command, arg1, arg2] = process.argv.slice(2);
|
21 |
|
22 | const supportedCommands = {
|
23 | component: 'component',
|
24 | lib: 'lib',
|
25 | new: 'new',
|
26 | page: 'page',
|
27 | rename: 'rename',
|
28 | stateful: 'stateful',
|
29 | stateless: 'stateless'
|
30 | };
|
31 |
|
32 | if (process.argv.includes('--version') || process.argv.includes('-v')) {
|
33 | messages.version(currentVersion);
|
34 | process.exit(0);
|
35 | }
|
36 |
|
37 | fetchLatestVersion();
|
38 |
|
39 | let shouldExit = false;
|
40 |
|
41 | if (!command) {
|
42 | messages.help();
|
43 | shouldExit = true;
|
44 | } else if (command === supportedCommands.new) {
|
45 |
|
46 | newProject();
|
47 | } else if (Object.values(supportedCommands).includes(command)) {
|
48 |
|
49 | getConfig()
|
50 | .then(({ componentsPath, mockupPath }) => {
|
51 | switch (command) {
|
52 | case supportedCommands.lib:
|
53 | lib(componentsPath);
|
54 | break;
|
55 | case supportedCommands.component:
|
56 | newComponent(
|
57 | arg1,
|
58 | process.argv.indexOf('-s') !== -1 ? true : undefined,
|
59 | componentsPath
|
60 | );
|
61 | break;
|
62 | case supportedCommands.page:
|
63 | newPage(arg1, arg2, mockupPath);
|
64 | break;
|
65 | case supportedCommands.rename:
|
66 | rename(arg1, arg2, componentsPath);
|
67 | break;
|
68 | case supportedCommands.stateful:
|
69 | toStateful(arg1, componentsPath);
|
70 | break;
|
71 | case supportedCommands.stateless:
|
72 | toStateless(arg1, componentsPath);
|
73 | break;
|
74 | default:
|
75 |
|
76 | messages.help();
|
77 | shouldExit = true;
|
78 | break;
|
79 | }
|
80 | })
|
81 | .catch(() => {
|
82 | messages.errorReadingConfig();
|
83 | shouldExit = true;
|
84 | });
|
85 | } else {
|
86 | messages.unrecognizedCommand(command);
|
87 | messages.help();
|
88 | shouldExit = true;
|
89 | }
|
90 |
|
91 | const latestVersion = configstore.get('latestVersion');
|
92 | if (latestVersion && semver.gt(latestVersion, currentVersion)) {
|
93 | messages.emptyLine();
|
94 | messages.versionConflict(currentVersion, latestVersion);
|
95 | messages.emptyLine();
|
96 | }
|
97 |
|
98 | if (shouldExit) {
|
99 | process.exit(0);
|
100 | }
|