dsql-admin/dsql-app/utils/grabDist.ts
Benjamin Toby e0a030f10d Updates
2025-01-13 09:00:21 +01:00

61 lines
1.6 KiB
TypeScript

// @ts-check
import fs from "fs";
import path from "path";
const isLocal = process.env.NEXT_PUBLIC_DSQL_LOCAL || null;
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 {
if (isLocal) {
return ".local_dist";
}
const DIST_DIR = path.resolve(process.cwd(), "./.dist");
if (isBuilding) {
const distDir = (() => {
if (isLocal) return ".local_dist";
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 = (() => {
if (isLocal) return ".local_dist";
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;
}