This repository has been archived on 2024-01-23. You can view files and clone it, but cannot push or open issues/pull-requests.
hairdresser-builder/builder.js

107 lines
2.6 KiB
JavaScript

import fs from "fs-extra";
import config from "./config.js";
import util from "util";
import { exec } from "child_process";
const execPromise = util.promisify(exec);
function readConfig(name) {
const config = fs.readJsonSync(name);
return config;
}
function createTmpDir() {
// check if tmp dir exists
// if not, create it
if (!fs.existsSync(config.tmpFolder)) {
fs.mkdirSync(config.tmpFolder);
console.log("tmp folder created");
}
}
function copyTemplateWebsite() {
console.log("copy template website");
fs.copySync(config.templateWebsiteFolder, `${config.tmpFolder}/template`);
console.log("copy template website success");
// delete config.json file in template folder
fs.removeSync(`${config.tmpFolder}/template/src/config.json`);
}
function deleteTmpDir() {
console.log("delete tmp folder");
fs.removeSync(config.tmpFolder);
}
async function buildCustomerWebsite(pageId, pageConfig) {
console.log(`Start building customer website ${pageId}`);
// create config.json file in template folder
fs.writeJsonSync(`${config.tmpFolder}/template/src/config.json`, pageConfig);
// delete public folder in tmp folder and recopy it from website template folder
fs.removeSync(`${config.tmpFolder}/template/public`);
fs.copySync(
`${config.templateWebsiteFolder}/public`,
`${config.tmpFolder}/template/public`
);
// copy all assets from customer data to public folder
fs.copySync(
`${config.customerWebsiteDataFolder}/${pageId}/`,
`${config.tmpFolder}/template/public/`
);
// run npm run build in template folder
try {
const { stdout } = await execPromise("npm run build", {
cwd: `${config.tmpFolder}/template`,
});
console.log(stdout);
} catch (err) {
console.error(`Failed to build customer website ${pageId} ${err}`);
} finally {
console.log(`Build customer website ${pageId} success`);
// copy build folder to customer websites folder
fs.copySync(
`${config.tmpFolder}/template/build`,
`${config.customerWebsiteFolder}/${pageId}`
);
}
}
function runBuilder() {
deleteTmpDir();
createTmpDir();
copyTemplateWebsite();
(async () => {
await buildCustomerWebsite(
"test1",
readConfig("../dev-only/test-config.json")
);
await buildCustomerWebsite(
"test2",
readConfig("../dev-only/test-config2.json")
);
console.log("build all customer websites success");
console.log("DONT FORGET TO COPY THE FOLDERS TO NGINX");
})();
//buildCustomerWebsite("test1", readConfig("./test-config.json"));
//buildCustomerWebsite("test2", readConfig("./test-config2.json"));
}
runBuilder();