UNPKG

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