cavallium-website/build.js

126 lines
5.4 KiB
JavaScript

#! /usr/bin/env node
const fs = require("fs");
const { spawn } = require('cross-spawn');
var execOptions = {
encoding: "utf8",
shell: "",
};
const argument0 = process.argv[2];
async function main(runMode) {
const angularSourceBuffer = fs.readFileSync("buildconfig.json");
const angularSource = JSON.parse(angularSourceBuffer);
const generalConfiguration = angularSource.angular.projects[angularSource.projectName].architect.build.options;
angularSource.angular.projects[angularSource.projectName].architect.build.options = {};
const productionConfiguration = angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production"];
delete angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production"];
angularSource.languages.forEach((language, languageIndex) => {
const defaultIndex = generalConfiguration.index;
const defaultIndexName = defaultIndex.split(".").slice(0, -1).join(".");
const defaultIndexExtension = defaultIndex.split(".").pop();
const languageSpecificIndex = defaultIndexName + "." + language + ".generated." + defaultIndexExtension;
const languageConfiguration = {
...angularSource.angular.projects[angularSource.projectName].architect.build.configurations[language]
};
delete angularSource.angular.projects[angularSource.projectName].architect.build.configurations[language];
angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production_" + language] = {
...generalConfiguration,
...productionConfiguration,
...languageConfiguration
};
angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production_" + language].fileReplacements = [
{
"replace": defaultIndex,
"with": languageSpecificIndex
},
...angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production_" + language].fileReplacements
];
resolvePaths(angularSource.angular.projects[angularSource.projectName].architect.build.configurations["production_" + language], "assets", language);
angularSource.angular.projects[angularSource.projectName].architect.build.configurations["serve_" + language] = {
...generalConfiguration,
...languageConfiguration,
"aot": true,
};
if (angularSource.angular.projects[angularSource.projectName].architect.build.configurations["serve_" + language].fileReplacements === undefined) {
angularSource.angular.projects[angularSource.projectName].architect.build.configurations["serve_" + language].fileReplacements = [];
}
angularSource.angular.projects[angularSource.projectName].architect.build.configurations["serve_" + language].fileReplacements.push({
"replace": defaultIndex,
"with": languageSpecificIndex
});
angularSource.angular.projects[angularSource.projectName].architect.serve.configurations[language] = {
"browserTarget": "cavallium-website:build:serve_"+language,
"port": 4200 + languageIndex
};
resolvePaths(angularSource.angular.projects[angularSource.projectName].architect.build.configurations["serve_" + language], "assets", language);
// Create index.language.html
let indexText = fs.readFileSync(defaultIndex).toString("utf8");
indexText = indexText.replace("generate=\"language_attribute\"", "lang=\"" + language + "\"");
fs.writeFileSync(languageSpecificIndex, indexText);
});
fs.writeFileSync("angular.json", JSON.stringify(angularSource.angular, null, "\t"), "utf8");
console.log("Angular source built");
switch (runMode) {
case "build": {
await Promise.all(angularSource.languages.map((language, index) => {
console.log("Building for language " + JSON.stringify(language) + (index > 0 ? " (hidden)" : ""));
const childProcess = spawn("ng", ["build", "--configuration=production_" + language],
{ stdio: [process.stdin, index == 0 ? process.stdout : null, process.stderr] });
return onExit(childProcess);
}));
}
break;
case "serve":
await Promise.all(angularSource.languages.map((language, index) => {
console.log("Building for language " + JSON.stringify(language) + (index > 0 ? " (hidden)" : ""));
const arguments = ["serve", "--configuration=" + language];
if (angularSource.localIpAddress) {
arguments.push("--host=" + angularSource.localIpAddress);
}
if (angularSource.disableHostCheck) {
arguments.push("--disable-host-check");
}
const childProcess = spawn("ng", arguments,
{ stdio: [process.stdin, index == 0 ? process.stdout : null, process.stderr] });
return onExit(childProcess);
}));
break;
default:
console.error("Error! Only Build and Serve commands are accepted!");
return;
}
}
main(argument0);
function onExit(childProcess) {
return new Promise((resolve, reject) => {
childProcess.once('exit', (code, signal) => {
if (code === 0) {
resolve(undefined);
} else {
reject(new Error('Exit with error code: ' + code));
}
});
childProcess.once('error', (err) => {
reject(err);
});
});
}
function resolvePaths(obj, elem, language) {
if (obj !== undefined && obj[elem] !== undefined && Array.isArray(obj[elem])) {
obj[elem] = obj[elem].map((asset) => typeof asset === "string" ? asset.replace("_GENERATED_LANGUAGE_", language) : {
"glob": asset.glob.replace("_GENERATED_LANGUAGE_", language),
"input": asset.input.replace("_GENERATED_LANGUAGE_", language),
"output": asset.output.replace("_GENERATED_LANGUAGE_", language)
});
}
}