buncid/rebuilds/next-js/grabDist.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-02-03 12:41:13 +00:00
import fs from "fs";
import path from "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}
*/
export default function grabDist(): string | undefined {
const DIST_DIR = path.resolve(process.cwd(), "./.dist");
if (isBuilding) {
const distDir = (() => {
try {
const buildNumber = fs.readFileSync(
`${DIST_DIR}/BUILD`,
"utf-8"
);
return `.dist/build-${buildNumber}`;
} catch (/** @type {*} */ error: any) {
console.log("Build Number Generation Error =>", error.message);
process.exit();
}
})();
return distDir;
}
if (production) {
const distDir = (() => {
try {
const buildNumber = fs.readFileSync(
`${DIST_DIR}/BUILD`,
"utf-8"
);
return `.dist/build-${buildNumber}`;
} catch (/** @type {*} */ error: any) {
console.log("Build Number Parse Error =>", error.message);
process.exit();
}
})();
return distDir;
}
return undefined;
}