UNPKG

1.99 kBJavaScriptView Raw
1/* eslint-env node */
2const path = require('path');
3
4const checkVersion = require('./source/check-version');
5const clearGitHubCredentials = require('./source/clear-github-credentials');
6const fetchLatestVersion = require('./source/fetch-latest-version');
7const getConfig = require('./source/get-config');
8const lib = require('./source/get-components-from-library');
9const messages = require('./source/messages');
10const newApp = require('./source/new-app');
11const runReactScript = require('./source/run-react-script');
12const supportedCommands = require('./source/supported-commands');
13
14const reactScripts = [
15 supportedCommands.stateful,
16 supportedCommands.stateless,
17 supportedCommands.page,
18 supportedCommands.component,
19 supportedCommands.rename
20];
21
22module.exports = function({ cwd = process.cwd(), command, shellArguments }) {
23 if (!reactScripts.includes(command)) {
24 fetchLatestVersion();
25 }
26
27 if (!Object.values(supportedCommands).includes(command)) {
28 messages.unrecognizedCommand(command);
29 messages.help();
30 return;
31 }
32
33 if (command === supportedCommands.logout) {
34 clearGitHubCredentials();
35 messages.clearedGitHubCredentials();
36 return;
37 }
38
39 if (command === supportedCommands.new) {
40 const projectPath = path.join(cwd, shellArguments[0] || '');
41 newApp(projectPath);
42 return;
43 }
44
45 const [
46 {
47 componentsPath,
48 dataFileContent,
49 dataFileExtension,
50 eslintConfig,
51 staticSitePath
52 },
53 configError
54 ] = getConfig(cwd);
55
56 if (configError) {
57 messages.error(configError.message);
58 return;
59 }
60
61 if (command === supportedCommands.lib) {
62 lib(componentsPath);
63 return;
64 }
65
66 // If the command isn't 'new', 'lib' or 'logout', the command is a @creuna/react-scripts command.
67 runReactScript({
68 command,
69 componentsPath,
70 dataFileContent,
71 dataFileExtension,
72 eslintConfig,
73 shellArguments,
74 staticSitePath
75 }).then(() => {
76 checkVersion();
77 process.exit(0);
78 });
79};