preview.ts 821 B

123456789101112131415161718192021222324252627282930313233
  1. import chalk from 'chalk';
  2. import Koa from 'koa';
  3. // import inquirer from 'inquirer';
  4. import staticServer from 'koa-static';
  5. import portfinder from 'portfinder';
  6. import { resolve } from 'path';
  7. import { getIPAddress } from '../utils';
  8. // start server
  9. const startApp = () => {
  10. const port = 9680;
  11. portfinder.basePort = port;
  12. const app = new Koa();
  13. app.use(staticServer(resolve(process.cwd(), 'dist')));
  14. portfinder.getPort(async (err, port) => {
  15. if (err) {
  16. throw err;
  17. } else {
  18. app.listen(port, function () {
  19. const empty = ' ';
  20. const common = `The preview program is already running:
  21. - LOCAL: http://localhost:${port}/
  22. - NETWORK: http://${getIPAddress()}:${port}/
  23. `;
  24. console.log(chalk.cyan('\n' + empty + common));
  25. });
  26. }
  27. });
  28. };
  29. startApp();