|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | +const fs = require("fs-extra"); |
| 5 | +const chalk = require("chalk"); // eslint-disable-line |
| 6 | +const pkg = require("../package.json"); |
| 7 | + |
| 8 | +process.on("unhandledRejection", (err) => { |
| 9 | + throw err; |
| 10 | +}); |
| 11 | + |
| 12 | +// 部署环境 |
| 13 | +const DEPLOYMENT_ENV = process.env.DEPLOYMENT_ENV; |
| 14 | +const appDirectory = fs.realpathSync(process.cwd()); |
| 15 | + |
| 16 | +// 复制 pm2 配置文件 |
| 17 | +const ecosystemFilePath = path.resolve( |
| 18 | + appDirectory, |
| 19 | + `ecosystem/ecosystem.config-${DEPLOYMENT_ENV}.js` |
| 20 | +); |
| 21 | + |
| 22 | +if (checkRequiredFiles([ecosystemFilePath])) { |
| 23 | + console.log(chalk.cyan(`Current deploy environment: ${DEPLOYMENT_ENV}.\n`)); |
| 24 | + const input = fs.readFileSync(ecosystemFilePath, "utf8"); |
| 25 | + const output = input.replace(/__PROJECT_NAME__/g, pkg.name); |
| 26 | + fs.writeFileSync( |
| 27 | + path.resolve(appDirectory, "ecosystem.config.js"), |
| 28 | + output, |
| 29 | + "utf8" |
| 30 | + ); |
| 31 | + fs.copy( |
| 32 | + path.resolve(appDirectory, "src/origin"), |
| 33 | + path.resolve(appDirectory, "dist/origin") |
| 34 | + ); |
| 35 | +} else { |
| 36 | + process.exitCode = 1; |
| 37 | +} |
| 38 | + |
| 39 | +function checkRequiredFiles(files) { |
| 40 | + let currentFilePath; |
| 41 | + try { |
| 42 | + files.forEach((filePath) => { |
| 43 | + currentFilePath = filePath; |
| 44 | + fs.accessSync(filePath, fs.F_OK); |
| 45 | + }); |
| 46 | + return true; |
| 47 | + } catch (err) { |
| 48 | + const dirName = path.dirname(currentFilePath); |
| 49 | + const fileName = path.basename(currentFilePath); |
| 50 | + console.log(chalk.red("Could not find a required file.")); |
| 51 | + console.log(chalk.red(" Name: ") + chalk.cyan(fileName)); |
| 52 | + console.log(chalk.red(" Searched in: ") + chalk.cyan(dirName)); |
| 53 | + return false; |
| 54 | + } |
| 55 | +} |
0 commit comments