Blame view

build/script/preview.ts 821 Bytes
陈文彬 authored
1
2
import chalk from 'chalk';
import Koa from 'koa';
vben authored
3
// import inquirer from 'inquirer';
陈文彬 authored
4
5
6
import staticServer from 'koa-static';
import portfinder from 'portfinder';
import { resolve } from 'path';
7
import { getIPAddress } from '../utils';
陈文彬 authored
8
9
// start server
陈文彬 authored
10
11
12
13
14
const startApp = () => {
  const port = 9680;
  portfinder.basePort = port;
  const app = new Koa();
15
  app.use(staticServer(resolve(process.cwd(), 'dist')));
陈文彬 authored
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

  portfinder.getPort(async (err, port) => {
    if (err) {
      throw err;
    } else {
      app.listen(port, function () {
        const empty = '    ';
        const common = `The preview program is already running:
    - LOCAL: http://localhost:${port}/
    - NETWORK: http://${getIPAddress()}:${port}/
    `;
        console.log(chalk.cyan('\n' + empty + common));
      });
    }
  });
};
vben authored
33
startApp();