UNPKG

zengenti-buildstartup-package

Version:

Post-build scripts to generate the startup scripts for any configured environments

51 lines (45 loc) 1.76 kB
var fs = require("fs"); var minimist = require("minimist"); var buildStartupRuntime = require("./startup.utils").buildStartupRuntime; var argv = minimist(process.argv.slice(2)); var alias = process.env.npm_config_alias || argv.alias || process.env.alias; var projectId = process.env.npm_config_projectId || argv.projectId || process.env.projectId; var accessToken = process.env.npm_config_accessToken || argv.accessToken || process.env.accessToken; var startFileName = `./start.${projectId}.${alias}.js`.toLowerCase(); var startScriptPath = `dist/server/${startFileName}`; // try to launch application with targeted start script try { if (!alias && !projectId) throw new Error(`--alias is ${alias}, --projectId is ${projectId}`); // To avoid an annoying bug with require caching // a MODULE_NOT_FOUND error we will check if the script // exists before require-ing it. console.log( `[launcher] startup file: ${startFileName} - exists?: ${fs.existsSync( startScriptPath )}` ); if (!fs.existsSync(startScriptPath)) { // If the script does not exist we will generate one console.log( `[launcher runtime] Generating start scripts for '${projectId}' on '${alias}'` ); buildStartupRuntime({ alias, projectId, accessToken, }); } try { // Require the targeted start script to launch the app require(`./${startFileName}`); } catch (startException) { // Failed to start with a targeted start script console.error("[launcher start server]", startException); } } catch (buildStartupException) { // Failed to generate runtime start script console.error("[launcher generate script]", buildStartupException); }