UNPKG

2.69 kBJavaScriptView Raw
1#!/usr/bin/env node
2/* eslint-env node */
3/* eslint-disable no-console */
4const newProject = require('@creuna/create-react-app');
5const {
6 newComponent,
7 newPage,
8 rename,
9 toStateful,
10 toStateless
11} = require('@creuna/react-scripts');
12const semver = require('semver');
13
14const configstore = require('./source/configstore');
15const currentVersion = require('./source/get-this-version');
16const fetchLatestVersion = require('./source/fetch-latest-version');
17const getConfig = require('./source/get-config');
18const lib = require('./source/get-components-from-library');
19const messages = require('./source/messages');
20const [command, arg1, arg2] = process.argv.slice(2);
21
22const supportedCommands = {
23 component: 'component',
24 lib: 'lib',
25 new: 'new',
26 page: 'page',
27 rename: 'rename',
28 stateful: 'stateful',
29 stateless: 'stateless'
30};
31
32if (process.argv.includes('--version') || process.argv.includes('-v')) {
33 messages.version(currentVersion);
34 process.exit(0);
35}
36
37fetchLatestVersion();
38
39let shouldExit = false;
40
41if (!command) {
42 messages.help();
43 shouldExit = true;
44} else if (command === supportedCommands.new) {
45 // 'new' does not require .creunarc.json
46 newProject();
47} else if (Object.values(supportedCommands).includes(command)) {
48 // All other commands require .creunarc.json to run
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 // NOTE: This should never happen
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
91const latestVersion = configstore.get('latestVersion');
92if (latestVersion && semver.gt(latestVersion, currentVersion)) {
93 messages.emptyLine();
94 messages.versionConflict(currentVersion, latestVersion);
95 messages.emptyLine();
96}
97
98if (shouldExit) {
99 process.exit(0);
100}