index.js 2.84 KB
Newer Older
1 2 3 4
const { join } = require('path');
const { writeFileSync, mkdirSync, existsSync } = require('fs');
const rimraf = require('rimraf');
const serveStatic = require('serve-static');
5 6
const { resolve } = require('path');
const { portfinder, chalk, delay } = require('@umijs/utils');
dengxiaofeng's avatar
dengxiaofeng committed
7 8
const argv = require('./argv');
const setup = require('./middlewares/frontendMiddleware');
邓晓峰's avatar
邓晓峰 committed
9
const pkg = require('../package.json');
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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);
dengxiaofeng's avatar
dengxiaofeng committed
31

32
  await delay(500);
33
  const absNodeModulesPath = `${process.cwd()}/node_modules`;
34
  const openAPIFilesPath = join(absNodeModulesPath, 'civ_open_api');
35

36 37 38 39 40 41
  const loadEnv = () => {
    const basePath = join(process.cwd(), '.env');
    const localPath = `${basePath}.local`;
    loadDotEnv(basePath);
    loadDotEnv(localPath);
  };
dengxiaofeng's avatar
dengxiaofeng committed
42

43
  loadEnv();
44

45 46 47 48 49 50 51 52
  // const absNodeModulesPath = cwd + '/node_modules';
  try {
    if (existsSync(openAPIFilesPath)) {
      rimraf.sync(openAPIFilesPath);
    }
    mkdirSync(join(openAPIFilesPath));
  } catch (error) {
    console.error(error);
dengxiaofeng's avatar
dengxiaofeng committed
53 54
  }

55 56 57 58 59 60
  const server = new Server({
    compress: true,
    https: !!isHTTPS,
    headers: {
      'access-control-allow-origin': '*',
    },
61
    port,
62 63 64 65 66 67 68 69
    beforeMiddlewares: [
      (config.mock || process.env.MOCK !== 'none') && mockMiddewares,
    ],
    afterMiddlewares: [serveStatic(openAPIFilesPath)],
    proxy: config.proxy,
    ...(config.devServer || {}),
  });

70

71 72 73 74 75 76 77 78 79
  setup(
    server.app,
    {
      outputPath: resolve(process.cwd(), pkg.name.toLocaleLowerCase()),
      publicPath: `/${pkg.name.toLocaleLowerCase()}`,
    },
    config,
    {
      port,
80 81
      homename,
    },
82 83 84
  );

  emitter.on('onDevCompileDone', async () => {
dengxiaofeng's avatar
dengxiaofeng committed
85
    try {
86
      const openAPIConfig = config.openAPI;
87
      if (openAPIConfig && openAPIConfig.schemaPath) {
88 89 90 91 92 93
        const openAPIJson = await getSchema(openAPIConfig.schemaPath);
        writeFileSync(
          join(openAPIFilesPath, 'civ-plugins_openapi.json'),
          JSON.stringify(openAPIJson, null, 2),
        );
      }
94 95
    } catch (error) {
      console.error(error);
dengxiaofeng's avatar
dengxiaofeng committed
96
    }
97 98 99 100 101 102 103
  });

  await server.listen({
    port,
    homename,
  });
})();