1 |
|
2 | const currentVersion = require('./source/get-this-version');
|
3 | const messages = require('./source/messages');
|
4 |
|
5 | const path = require('path');
|
6 | const semver = require('semver');
|
7 |
|
8 | const clearGitHubCredentials = require('./source/clear-github-credentials');
|
9 | const configstore = require('./source/configstore');
|
10 | const fetchLatestVersion = require('./source/fetch-latest-version');
|
11 | const getConfig = require('./source/get-config');
|
12 | const lib = require('./source/get-components-from-library');
|
13 | const newApp = require('./source/new-app');
|
14 | const runReactScript = require('./source/run-react-script');
|
15 | const supportedCommands = require('./source/supported-commands');
|
16 |
|
17 | module.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 |
|
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 | };
|