Updates
This commit is contained in:
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* # Grab the current distribution directory
|
||||
* @description This returns the relative path from the CWD. Eg `./.dist/build-1`
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
export default function grabDist(): string | undefined;
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = grabDist;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const production = process.env.NODE_ENV == "production";
|
||||
const isBuilding = process.env.BUILDING_APP;
|
||||
/**
|
||||
* # Grab the current distribution directory
|
||||
* @description This returns the relative path from the CWD. Eg `./.dist/build-1`
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
function grabDist() {
|
||||
const DIST_DIR = path_1.default.resolve(process.cwd(), "./.dist");
|
||||
if (isBuilding) {
|
||||
const distDir = (() => {
|
||||
try {
|
||||
const buildNumber = fs_1.default.readFileSync(`${DIST_DIR}/BUILD`, "utf-8");
|
||||
return `.dist/build-${buildNumber}`;
|
||||
}
|
||||
catch ( /** @type {*} */error) {
|
||||
console.log("Build Number Generation Error =>", error.message);
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
return distDir;
|
||||
}
|
||||
if (production) {
|
||||
const distDir = (() => {
|
||||
try {
|
||||
const buildNumber = fs_1.default.readFileSync(`${DIST_DIR}/BUILD`, "utf-8");
|
||||
return `.dist/build-${buildNumber}`;
|
||||
}
|
||||
catch ( /** @type {*} */error) {
|
||||
console.log("Build Number Parse Error =>", error.message);
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
return distDir;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bun
|
||||
export {};
|
||||
Vendored
+113
@@ -0,0 +1,113 @@
|
||||
#!/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 fs_1 = __importDefault(require("fs"));
|
||||
const child_process_1 = require("child_process");
|
||||
const DIST_DIR = path_1.default.resolve(process.cwd(), "./.dist");
|
||||
let PREV_BUILD_NO = "0";
|
||||
const MAX_BUILDS = process.env.BUNCID_MAX_BUILDS
|
||||
? Number(process.env.BUNCID_MAX_BUILDS)
|
||||
: 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);
|
||||
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 `.dist/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);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user