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
const { join } = require('path');
const { writeFileSync, mkdirSync, existsSync } = require('fs');
const rimraf = require('rimraf');
const serveStatic = require('serve-static');
const argv = require('./argv');
const setup = require('./middlewares/frontendMiddleware');
const pkg = require('../package.json');
const { resolve } = require('path');
const { portfinder, chalk, delay } = require('@umijs/utils');
const config = require('../config/config');
const mockMiddewares = require('./mock');
const emitter = require('./event');
const Server = require('./server');
const loadDotEnv = require('./utils/loadDotEnv');
const { getSchema } = require('./openapi');
(async () => {
const defaultPort =
process.env.PORT ||
argv.port ||
(config && config.devServer && config.devServer.port);
const port = await portfinder.getPortPromise({
port: defaultPort ? parseInt(String(defaultPort), 10) : 8080,
});
const homename =
process.env.HOST ||
(config && config.devServer && config.devServer.host) ||
'127.0.0.1';
console.log(chalk.cyan('Starting the development server...'));
// process.send && process.send({ type: 'UPDATE_PORT', port });
const isHTTPS = process.env.HTTPS || (argv && argv.https);
await delay(500);
const absNodeModulesPath = process.cwd() + '/node_modules';
const openAPIFilesPath = join(absNodeModulesPath, 'civ_open_api');
const loadEnv = () => {
const basePath = join(process.cwd(), '.env');
const localPath = `${basePath}.local`;
loadDotEnv(basePath);
loadDotEnv(localPath);
};
loadEnv();
// const absNodeModulesPath = cwd + '/node_modules';
try {
if (existsSync(openAPIFilesPath)) {
rimraf.sync(openAPIFilesPath);
}
mkdirSync(join(openAPIFilesPath));
} catch (error) {
console.error(error);
}
const server = new Server({
compress: true,
https: !!isHTTPS,
headers: {
'access-control-allow-origin': '*',
},
port: port,
beforeMiddlewares: [
(config.mock || process.env.MOCK !== 'none') && mockMiddewares,
],
afterMiddlewares: [serveStatic(openAPIFilesPath)],
proxy: config.proxy,
...(config.devServer || {}),
});
setup(
server.app,
{
outputPath: resolve(process.cwd(), pkg.name.toLocaleLowerCase()),
publicPath: `/${pkg.name.toLocaleLowerCase()}`,
},
config,
{
port,
homename
}
);
emitter.on('onDevCompileDone', async () => {
try {
const openAPIConfig = config.openAPI;
if(openAPIConfig && openAPIConfig.schemaPath) {
const openAPIJson = await getSchema(openAPIConfig.schemaPath);
writeFileSync(
join(openAPIFilesPath, 'civ-plugins_openapi.json'),
JSON.stringify(openAPIJson, null, 2),
);
}
} catch (error) {
console.error(error);
}
});
await server.listen({
port,
homename,
});
})();