1 | const path = require('path');
|
2 | const prompt = require('@creuna/prompt');
|
3 |
|
4 | const getComponentPath = require('./utils/get-component-path');
|
5 | const messages = require('./messages');
|
6 | const supportedCommands = require('./supported-commands');
|
7 |
|
8 | function runScript({
|
9 | dataFileExtension,
|
10 | dataFileContent,
|
11 | command,
|
12 | componentsPath,
|
13 | eslintConfig,
|
14 | shellArguments,
|
15 | staticSitePath
|
16 | }) {
|
17 | const { pathOrName } = prompt({
|
18 | pathOrName: {
|
19 | text:
|
20 | command === supportedCommands.page
|
21 | ? 'Name of page'
|
22 | : 'Name of component',
|
23 | value: shellArguments[0]
|
24 | }
|
25 | });
|
26 |
|
27 | const { shouldBeStateful } =
|
28 | command === supportedCommands.component
|
29 | ? prompt({
|
30 | shouldBeStateful: {
|
31 | text: 'Should the component be stateful?',
|
32 | type: Boolean,
|
33 | value: shellArguments[1] === '-s' ? true : undefined
|
34 | }
|
35 | })
|
36 | : {};
|
37 |
|
38 | const { groupName, humanReadableName, pageUrl } =
|
39 | command === supportedCommands.page
|
40 | ? prompt({
|
41 | humanReadableName: {
|
42 | text: 'Human readable name (optional)',
|
43 | optional: true,
|
44 | value: shellArguments[1]
|
45 | },
|
46 | groupName: {
|
47 | text: 'Group name for the index page (optional)',
|
48 | optional: true,
|
49 | value: shellArguments[2]
|
50 | },
|
51 | pageUrl: {
|
52 | text: 'Custom url for the page (optional)',
|
53 | optional: true,
|
54 | value: shellArguments[3]
|
55 | }
|
56 | })
|
57 | : {};
|
58 |
|
59 | const { newComponentName } =
|
60 | command === supportedCommands.rename
|
61 | ? prompt({
|
62 | newComponentName: {
|
63 | text: 'New name of component',
|
64 | value: shellArguments[1]
|
65 | }
|
66 | })
|
67 | : {};
|
68 |
|
69 | const {
|
70 | newComponent,
|
71 | newPage,
|
72 | rename,
|
73 | toStateful,
|
74 | toStateless
|
75 | } = require('@creuna/react-scripts');
|
76 |
|
77 | const isPath = pathOrName.includes(path.sep);
|
78 | const componentName = path.basename(pathOrName, path.extname(pathOrName));
|
79 | const pageBasePath = isPath
|
80 | ? path.join(staticSitePath, path.dirname(pathOrName))
|
81 | : staticSitePath;
|
82 | const componentBasePath = isPath
|
83 | ? path.join(componentsPath, path.dirname(pathOrName))
|
84 | : componentsPath;
|
85 |
|
86 | switch (command) {
|
87 | case supportedCommands.component:
|
88 | return newComponent({
|
89 | componentName,
|
90 | eslintConfig,
|
91 | folderPath: componentBasePath,
|
92 | shouldBeStateful
|
93 | });
|
94 | case supportedCommands.page:
|
95 | return newPage({
|
96 | componentName,
|
97 | dataFileContent,
|
98 | dataFileExtension,
|
99 | eslintConfig,
|
100 | folderPath: pageBasePath,
|
101 | groupName,
|
102 | humanReadableName,
|
103 | url: pageUrl
|
104 | });
|
105 | case supportedCommands.rename:
|
106 | return rename({
|
107 | eslintConfig,
|
108 | filePath: getComponentPath({ basePath: componentsPath, pathOrName }),
|
109 | newComponentName
|
110 | });
|
111 | case supportedCommands.stateful:
|
112 | return toStateful({
|
113 | eslintConfig,
|
114 | filePath: getComponentPath({ basePath: componentsPath, pathOrName })
|
115 | });
|
116 | case supportedCommands.stateless:
|
117 | return toStateless({
|
118 | eslintConfig,
|
119 | filePath: getComponentPath({ basePath: componentsPath, pathOrName })
|
120 | });
|
121 | }
|
122 | }
|
123 |
|
124 | module.exports = function(options) {
|
125 | return runScript(options)
|
126 | .then(response => {
|
127 | messages.emptyLine();
|
128 | messages.messageList(response.messages);
|
129 | messages.emptyLine();
|
130 | })
|
131 | .catch(messages.error);
|
132 | };
|