UNPKG

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