-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinit.ts
99 lines (92 loc) · 2.76 KB
/
init.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
import color from 'ansi-colors';
import prompts from 'prompts';
import type { MockServerConfigInitArgv } from '@/utils/types';
import { baseUrlSchema, portSchema, staticPathSchema } from '@/utils/validate';
import { createTemplate } from './helpers';
export const init = async (argv: MockServerConfigInitArgv) => {
try {
const response = await prompts(
[
{
name: 'ts',
type: 'toggle',
message: 'Would you like to use TypeScript?',
initial: true,
active: 'Yes',
inactive: 'No'
},
{
type: 'select',
name: 'apiType',
message: 'Choose api type',
initial: 0,
choices: [
{ title: 'Rest', description: 'Rest api sample', value: 'rest' },
{ title: 'GraphQL', description: 'GraphQL api sample', value: 'graphql' },
{ title: 'Both', description: 'Rest api and GraphQL api sample', value: 'full' }
]
},
{
name: 'baseUrl',
type: argv.baseUrl ? null : 'text',
message: 'Base url (must start with a forward slash):',
initial: '/',
validate: (baseUrl) => {
try {
baseUrlSchema.parse(baseUrl);
return true;
} catch {
return 'Invalid base url value';
}
}
},
{
name: 'port',
type: argv.port ? null : 'number',
message: 'Port:',
initial: 31299,
validate: (port) => {
try {
portSchema.parse(+port);
return true;
} catch {
return 'Invalid port value';
}
}
},
{
name: 'staticPath',
type: argv.staticPath ? null : 'text',
message: 'Static path (must start with a forward slash):',
initial: '/',
validate: (staticPath) => {
try {
staticPathSchema.parse(staticPath);
return true;
} catch {
return 'Invalid static path value';
}
}
}
],
{
onCancel: () => {
throw new Error('❌ Operation cancelled');
}
}
);
await createTemplate({ ...argv, ...response });
const userAgent = process.env.npm_config_user_agent ?? '';
const packageManager = /pnpm/.test(userAgent)
? 'pnpm'
: /yarn/.test(userAgent)
? 'yarn'
: 'npx';
console.log('\n');
console.log(color.bold('🎉 Thanks for using mock-config-server! 🎉'));
console.log(`start command: ${color.bold(color.green(`${packageManager} mcs`))}`);
} catch (cancelled: any) {
console.log(cancelled?.message);
process.exit(1);
}
};