-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
255 lines (221 loc) · 7.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import chalk from 'chalk';
import Commander from 'commander';
import prompts from 'prompts';
import path from 'path';
import fs from 'fs';
import packageJson from './package.json';
import { validateNpmName } from './utils/validate-pkg';
import { generateRandomPort } from './utils/random-port';
import { getObjectValue } from './utils/get-values';
import { createApp } from './app';
let projectPath: string = '';
const handleSigTerm = () => process.exit(0);
process.on('SIGINT', handleSigTerm);
process.on('SIGTERM', handleSigTerm);
const onPromptState = (state: any) => {
if (state.aborted) {
process.stdout.write('\x1B[?25h');
process.stdout.write('\n');
process.exit(1);
}
};
const program = new Commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')} [options]`)
.action((name) => {
projectPath = name;
})
.option('-n, --name <name>', 'Project name')
.option('--js --javascript', 'Initialize with JavaScript (default)')
.option('--ts --typescript', 'Initialize with TypeScript')
.option('-t --app-type <type>', 'Application type (default: host)')
.option('-p, --port <number>', 'Port number', generateRandomPort())
.option('--pf --packages-folder <folder>', 'The folder that contains the MFEs(default: packages)')
.option('--public-path <path>', 'The public path for the MFEs (default: /)')
.option('-h, --help', 'Display this message')
.parse(process.argv);
const getProgramValues = <T>(value: any): T => getObjectValue(program.opts(), value);
const runApp = async (): Promise<void> => {
// const config = new Conf({ projectName: projectPath });
if (typeof projectPath === 'string') {
projectPath = projectPath.trim();
}
if (!projectPath) {
const res = await prompts({
onState: onPromptState,
type: 'text',
name: 'path',
message: 'What is the name of your app?',
initial: 'my-mfe-app'
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
if (typeof res.path === 'string') {
projectPath = res.path.trim();
}
}
if (!projectPath) {
console.log(
'\nPlease specify the project directory:\n'
+ ` ${chalk.cyan(program.name())} ${chalk.green(
'<project-directory>'
)}\n`
+ 'For example:\n'
+ ` ${chalk.cyan(program.name())} ${chalk.green('my-mfe-app')}\n\n`
+ `Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`
);
process.exit(1);
}
const resolvedProjectPath = path.resolve(projectPath);
const projectName = path.basename(resolvedProjectPath);
const { valid, problems } = validateNpmName(projectName);
if (!valid) {
console.error(
`Could not create a project called ${chalk.red(
`"${projectName}"`
)} because of npm naming restrictions:`
);
problems!.forEach((p) => console.error(` ${chalk.red.bold('*')} ${p}`));
process.exit(1);
}
const root = path.resolve(resolvedProjectPath);
const folderExists = fs.existsSync(root);
if (folderExists) {
process.exit(1);
}
/**
* Set language preference if it is not set
*/
const selectedJs = getProgramValues<boolean | undefined>('javascript');
const selectedTs = getProgramValues<boolean | undefined>('typescript');
if (!selectedJs && !selectedTs) {
const res = await prompts({
onState: onPromptState,
type: 'select',
name: 'language',
message: 'What language would you like to use?',
choices: [
{ title: 'JavaScript', value: 'js' },
{ title: 'TypeScript', value: 'ts' }
],
initial: 0
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
if (res.language === 'js') {
program.opts().javascript = true;
} else {
program.opts().typescript = true;
}
}
/**
* Select the type of MFE application (host or remote)
* */
const selectedAppType = getProgramValues<string | undefined>('appType');
if (selectedAppType && selectedAppType !== 'host' && selectedAppType !== 'remote') {
console.error(chalk.red('Invalid application type. Please select either "host" or "remote".'));
process.exit(1);
}
if (!selectedAppType) {
const res = await prompts({
onState: onPromptState,
type: 'select',
name: 'appType',
message: 'What type of application would you like to create?',
choices: [
{ title: 'Host', value: 'host' },
{ title: 'Remote', value: 'remote' }
],
initial: 0
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
program.opts().appType = res.appType;
}
/**
* Set the port number
* */
const selectedPort = getProgramValues<number | undefined>('port');
if (!selectedPort) {
const res = await prompts({
onState: onPromptState,
type: 'number',
name: 'port',
message: 'What port number would you like to use?',
initial: generateRandomPort()
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
program.opts().port = res.port;
}
/**
* Set public path
* */
const selectedPublicPath = getProgramValues<string | undefined>('publicPath');
if (!selectedPublicPath) {
const res = await prompts({
onState: onPromptState,
type: 'text',
name: 'publicPath',
message: 'What public path would you like to use?',
initial: '/'
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
program.opts().publicPath = res.publicPath;
}
/**
* Set packages folder
* */
// TODO: Add validation for the folder name and also in the app
const selectedPackagesFolder = getProgramValues<string | undefined>('packagesFolder');
if (!selectedPackagesFolder) {
const res = await prompts({
onState: onPromptState,
type: 'text',
name: 'packagesFolder',
message: 'What is the name of the folder that contains the MFEs?',
initial: 'packages'
}, {
onCancel: () => {
console.error(chalk.red('Exiting the program...'));
process.exit(1);
}
});
program.opts().packagesFolder = res.packagesFolder;
}
try {
await createApp({
appPath: projectPath,
port: program.opts().port,
language: program.opts().javascript ? 'js' : 'ts',
appType: program.opts().appType,
publicPath: program.opts().publicPath
});
} catch (err) {
console.log(err);
}
};
runApp()
.then(() => {
console.log(chalk.green('Done.'));
})
.catch((err) => {
console.log(err);
});