zengenti-buildstartup-package
Version:
Post-build scripts to generate the startup scripts for any configured environments
80 lines (69 loc) • 2.89 kB
JavaScript
const fs = require("fs");
const path = require("path");
const _module = path.basename(__filename);
var buildUtils = require("./start/startup.utils").buildUtils;
var {
loadConfig,
makeReplacements,
readModuleFileSync,
writeModuleFileSync,
} = buildUtils;
function readDotEnvFiles() {
var envs = [];
fs.readdirSync(process.cwd()).forEach((filename) => {
if (filename.startsWith(".env")) {
envs.push(filename);
}
});
return envs;
}
try {
console.log("/* ", _module, "- Building server start scripts for .env* file(s)", "*/");
// read all files in root called .env* and create separate
// startup files for all configured environments, so the production
// bundles can be started with any of these scripts
var envs = readDotEnvFiles();
// the base startup template we are going to make environment specific
// replacements on
var template = readModuleFileSync(__dirname + "/start/startup.template.js");
envs.forEach((env) => {
// for each env we will load the .env.* file into process.env.* variables
// then make replacements to the template
var config = loadConfig(env);
var staticPath = config.STATIC_PATH || "static";
var clientStartup = makeReplacements(config, template, true)
.replace(/\s+/g, " ")
.trim();
var serverStartup = makeReplacements(config, template);
if (process.env.dotenv == ".env") {
// write the 'default' project startup (.env) as just start.js
// so it is ready to launch with just a default server start script
// and serve a default client bundle
writeModuleFileSync("dist/" + staticPath + "/startup.js", clientStartup);
writeModuleFileSync("dist/server/start.js", serverStartup);
}
// write a startup.projectId.alias.js file for the environment alias read from
// the .env.* file so the script can be used to start the server
// and serve the bundle configuration for that specific environment
writeModuleFileSync(
`dist/server/start.${config.PROJECT.toLowerCase()}.${config.ALIAS.toLowerCase()}.js`,
serverStartup
);
});
console.log("Deploying server start scripts");
fs.copyFileSync(__dirname + "/start/start.js", "dist/start.js");
fs.copyFileSync(
__dirname + "/start/startup.utils.js",
"dist/server/startup.utils.js"
);
// Copying these new files for the ability to generate a targeted server
// start script inside the container for an unknown CMS
fs.copyFileSync(__dirname + "/start/launcher.js", "dist/server/launcher.js");
fs.copyFileSync(
__dirname + "/start/startup.template.js",
"dist/server/startup.template.js"
);
console.log("/* 👏 ", _module, "completed successfully", "*/");
} catch (e) {
console.log("/* !! ", _module, "encountered a problem: ", "*/", e.stack);
}