dsql-admin/dsql-app/utils/grabDist.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

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