133 lines
4.7 KiB
JavaScript
Executable File
133 lines
4.7 KiB
JavaScript
Executable File
#!/usr/bin/env bun
|
|
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
var _a;
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const path_1 = __importDefault(require("path"));
|
|
const util_1 = require("util");
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const child_process_1 = require("child_process");
|
|
const grab_default_dist_name_1 = __importDefault(require("./(utils)/grab-default-dist-name"));
|
|
const args = (0, util_1.parseArgs)({
|
|
args: process.argv,
|
|
options: {
|
|
maxBuilds: {
|
|
type: "string",
|
|
alias: "m",
|
|
},
|
|
distDir: {
|
|
type: "string",
|
|
alias: "d",
|
|
},
|
|
},
|
|
allowPositionals: true,
|
|
});
|
|
const distDirName = args.values.distDir || (0, grab_default_dist_name_1.default)();
|
|
const DIST_DIR = path_1.default.join(process.cwd(), distDirName);
|
|
let PREV_BUILD_NO = "0";
|
|
const MAX_BUILDS = process.env.BUNCID_MAX_BUILDS
|
|
? Number(process.env.BUNCID_MAX_BUILDS)
|
|
: args.values.maxBuilds
|
|
? Number(args.values.maxBuilds)
|
|
: 10;
|
|
if (MAX_BUILDS < 1 ||
|
|
Number.isNaN(MAX_BUILDS) ||
|
|
typeof MAX_BUILDS !== "number") {
|
|
throw new Error("Invalid MAX_BUILDS");
|
|
}
|
|
if (!fs_1.default.existsSync(DIST_DIR))
|
|
fs_1.default.mkdirSync(DIST_DIR, { recursive: true });
|
|
if (fs_1.default.existsSync(`${DIST_DIR}/BUILD`)) {
|
|
PREV_BUILD_NO = fs_1.default.readFileSync(`${DIST_DIR}/BUILD`, "utf-8");
|
|
}
|
|
else {
|
|
fs_1.default.writeFileSync(`${DIST_DIR}/BUILD`, "0", "utf-8");
|
|
}
|
|
try {
|
|
const buildNumber = fs_1.default.readFileSync(`${DIST_DIR}/BUILD`, "utf-8");
|
|
const newBuildNumber = Number(buildNumber) + 1;
|
|
if (newBuildNumber < 0) {
|
|
throw new Error("Invalid Build Number");
|
|
}
|
|
fs_1.default.writeFileSync(`${DIST_DIR}/BUILD`, String(newBuildNumber));
|
|
if (newBuildNumber > MAX_BUILDS) {
|
|
const builds = fs_1.default.readdirSync(DIST_DIR);
|
|
const buildDirs = builds.filter((build) => build.match(/build-\d+/));
|
|
for (const buildDir of buildDirs) {
|
|
const buildDirPath = path_1.default.join(DIST_DIR, buildDir);
|
|
const buildDirStat = fs_1.default.statSync(buildDirPath);
|
|
if (buildDirStat.isDirectory()) {
|
|
const buildDirName = buildDir.split("-")[1];
|
|
const buildDirNumber = Number(buildDirName);
|
|
if (buildDirNumber <= newBuildNumber - MAX_BUILDS) {
|
|
fs_1.default.rmdirSync(buildDirPath, { recursive: true });
|
|
console.log("Deleted Build Directory =>", buildDirPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.log("Build Number Parse Error =>", error.message);
|
|
process.exit(1);
|
|
}
|
|
const spawnSyncOptions = {
|
|
stdio: "inherit",
|
|
encoding: "utf-8",
|
|
shell: ((_a = process.platform) === null || _a === void 0 ? void 0 : _a.match(/win32/i)) ? "bash.exe" : undefined,
|
|
env: Object.assign(Object.assign({}, process.env), { BUILDING_APP: "true" }),
|
|
};
|
|
const build = (0, child_process_1.spawnSync)("bunx", ["next", "build"], spawnSyncOptions);
|
|
/**
|
|
* @returns {string}
|
|
*/
|
|
function grabNewDistDir() {
|
|
try {
|
|
const buildNumber = fs_1.default.readFileSync(`${DIST_DIR}/BUILD`, "utf-8");
|
|
return `${distDirName}/build-${buildNumber}`;
|
|
}
|
|
catch ( /** @type {*} */error) {
|
|
console.log("Build Number Parse Error =>", error.message);
|
|
process.exit();
|
|
}
|
|
}
|
|
const newDistDir = grabNewDistDir();
|
|
// if (!fs.existsSync(newDistDir)) {
|
|
// fs.mkdirSync(newDistDir, { recursive: true });
|
|
// }
|
|
// if (!fs.existsSync(`${newDistDir}/BUILD_ID`)) {
|
|
// fs.writeFileSync(`${newDistDir}/BUILD_ID`, "4");
|
|
// }
|
|
/**
|
|
* # Revert Directories
|
|
* @param {string} dir - New Build Directory Path
|
|
*/
|
|
function revert(dir) {
|
|
var _a;
|
|
console.log("Build Failed!", ((_a = build === null || build === void 0 ? void 0 : build.error) === null || _a === void 0 ? void 0 : _a.message) || build.stderr);
|
|
fs_1.default.writeFileSync(`${DIST_DIR}/BUILD`, PREV_BUILD_NO, "utf-8");
|
|
(0, child_process_1.execSync)(`rm -Rf ${dir}`, { cwd: process.cwd() });
|
|
const writeErr = build.error
|
|
? build.error.message
|
|
: build.stderr
|
|
? build.stderr.toString()
|
|
: "NO BUILD_ID found in New Build Folder";
|
|
fs_1.default.writeFileSync(`${DIST_DIR}/LAST_BUILD_FAIL`, Date() + "\n\n" + writeErr, "utf-8");
|
|
process.exit(1);
|
|
}
|
|
if (build.error ||
|
|
build.stderr ||
|
|
build.status != 0 ||
|
|
!fs_1.default.existsSync(`${newDistDir}/BUILD_ID`)) {
|
|
revert(newDistDir);
|
|
throw new Error("Build Failed!");
|
|
}
|
|
process.on("exit", () => {
|
|
const onExitDir = grabNewDistDir();
|
|
if (!fs_1.default.existsSync(`${onExitDir}/BUILD_ID`)) {
|
|
revert(onExitDir);
|
|
}
|
|
});
|