start.js 892 Bytes
#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const execSync = require('child_process').execSync;
const chalk = require('chalk');
const getConfigContent = require('../config/defaultConfig.js');
const { findUsablePort } = require('../utils');

const umircCwdFilePath = path.resolve(process.cwd(), '.umirc.js');

async function writeFile() {
  const port = await findUsablePort(process.env.PORT || 8000);
  const content = getConfigContent(port);

  fs.writeFile(umircCwdFilePath, content, (err) => {
    if (err) {
      console.log(chalk.red(`文件写入失败, ${err}`));
      process.exit(1);
    } else {
      execSync(
        `npx cross-env ${process.argv
          .slice(2)
          .join(' ')} SOCKET_SERVER=http://localhost:${port} umi dev`,
        { stdio: 'inherit' },
      );
      fs.unlinkSync(umircCwdFilePath);
    }
  });
}

writeFile();