path.utils: enhance logging for development environment and improve path resolution
This commit is contained in:
parent
f3bae04446
commit
a1aab62adf
@ -9,10 +9,12 @@ import { fileURLToPath } from 'url';
|
|||||||
*/
|
*/
|
||||||
const isDevEnvironment = (): boolean => {
|
const isDevEnvironment = (): boolean => {
|
||||||
const isDev = process.env.NODE_ENV === 'development';
|
const isDev = process.env.NODE_ENV === 'development';
|
||||||
|
|
||||||
// Log only in development to avoid noisy production logs
|
// Log only in development to avoid noisy production logs
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
console.log('[PATH-UTILS] Environment:', 'development');
|
console.log('[PATH-UTILS] Environment:', 'development');
|
||||||
}
|
}
|
||||||
|
|
||||||
return isDev;
|
return isDev;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -22,6 +24,7 @@ const isDevEnvironment = (): boolean => {
|
|||||||
// Helper: walk up the directory tree looking for a predicate
|
// Helper: walk up the directory tree looking for a predicate
|
||||||
const findUp = (startDir: string, predicate: (d: string) => boolean): string | null => {
|
const findUp = (startDir: string, predicate: (d: string) => boolean): string | null => {
|
||||||
let dir = path.resolve(startDir);
|
let dir = path.resolve(startDir);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
if (predicate(dir)) {
|
if (predicate(dir)) {
|
||||||
@ -30,8 +33,11 @@ const findUp = (startDir: string, predicate: (d: string) => boolean): string | n
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
// ignore fs errors and continue climbing
|
// ignore fs errors and continue climbing
|
||||||
}
|
}
|
||||||
|
|
||||||
const parent = path.dirname(dir);
|
const parent = path.dirname(dir);
|
||||||
|
|
||||||
if (parent === dir) return null;
|
if (parent === dir) return null;
|
||||||
|
|
||||||
dir = parent;
|
dir = parent;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -52,11 +58,13 @@ const getBackendRoot = (): string => {
|
|||||||
|
|
||||||
// Otherwise, try to find upward a directory containing package.json and src
|
// Otherwise, try to find upward a directory containing package.json and src
|
||||||
const pkgRoot = findUp(cwd, (d) => fs.existsSync(path.join(d, 'package.json')) && fs.existsSync(path.join(d, 'src')));
|
const pkgRoot = findUp(cwd, (d) => fs.existsSync(path.join(d, 'package.json')) && fs.existsSync(path.join(d, 'src')));
|
||||||
|
|
||||||
if (pkgRoot) return pkgRoot;
|
if (pkgRoot) return pkgRoot;
|
||||||
|
|
||||||
// Try using the file's directory as a fallback starting point
|
// Try using the file's directory as a fallback starting point
|
||||||
const fileDir = path.dirname(fileURLToPath(import.meta.url));
|
const fileDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const pkgRootFromFile = findUp(fileDir, (d) => fs.existsSync(path.join(d, 'package.json')) && fs.existsSync(path.join(d, 'src')));
|
const pkgRootFromFile = findUp(fileDir, (d) => fs.existsSync(path.join(d, 'package.json')) && fs.existsSync(path.join(d, 'src')));
|
||||||
|
|
||||||
if (pkgRootFromFile) return pkgRootFromFile;
|
if (pkgRootFromFile) return pkgRootFromFile;
|
||||||
|
|
||||||
// Last resort: assume two levels up from this file (previous behaviour)
|
// Last resort: assume two levels up from this file (previous behaviour)
|
||||||
@ -74,20 +82,26 @@ const getProjectRoot = (): string => {
|
|||||||
const fileDir = path.dirname(fileURLToPath(import.meta.url));
|
const fileDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
const publicFromCwd = findUp(cwd, (d) => fs.existsSync(path.join(d, 'public')));
|
const publicFromCwd = findUp(cwd, (d) => fs.existsSync(path.join(d, 'public')));
|
||||||
|
|
||||||
if (publicFromCwd) {
|
if (publicFromCwd) {
|
||||||
if (isDevEnvironment()) console.log('[PATH-UTILS] Project root (public) found from CWD:', publicFromCwd);
|
if (isDevEnvironment()) console.log('[PATH-UTILS] Project root (public) found from CWD:', publicFromCwd);
|
||||||
|
|
||||||
return publicFromCwd;
|
return publicFromCwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicFromFile = findUp(fileDir, (d) => fs.existsSync(path.join(d, 'public')));
|
const publicFromFile = findUp(fileDir, (d) => fs.existsSync(path.join(d, 'public')));
|
||||||
|
|
||||||
if (publicFromFile) {
|
if (publicFromFile) {
|
||||||
if (isDevEnvironment()) console.log('[PATH-UTILS] Project root (public) found from file dir:', publicFromFile);
|
if (isDevEnvironment()) console.log('[PATH-UTILS] Project root (public) found from file dir:', publicFromFile);
|
||||||
|
|
||||||
return publicFromFile;
|
return publicFromFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no public folder found, fallback to backend root heuristics
|
// If no public folder found, fallback to backend root heuristics
|
||||||
const backendRoot = getBackendRoot();
|
const backendRoot = getBackendRoot();
|
||||||
|
|
||||||
if (isDevEnvironment()) console.log('[PATH-UTILS] Falling back to backend root as project root:', backendRoot);
|
if (isDevEnvironment()) console.log('[PATH-UTILS] Falling back to backend root as project root:', backendRoot);
|
||||||
|
|
||||||
return backendRoot;
|
return backendRoot;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -98,6 +112,7 @@ const getProjectRoot = (): string => {
|
|||||||
*/
|
*/
|
||||||
const verifyPathExists = (pathToVerify: string, description: string): void => {
|
const verifyPathExists = (pathToVerify: string, description: string): void => {
|
||||||
const exists = fs.existsSync(pathToVerify);
|
const exists = fs.existsSync(pathToVerify);
|
||||||
|
|
||||||
if (isDevEnvironment()) {
|
if (isDevEnvironment()) {
|
||||||
console.log(`[PATH-UTILS] ${description}: ${pathToVerify} (${exists ? 'EXISTS' : 'MISSING'})`);
|
console.log(`[PATH-UTILS] ${description}: ${pathToVerify} (${exists ? 'EXISTS' : 'MISSING'})`);
|
||||||
}
|
}
|
||||||
@ -109,10 +124,12 @@ const verifyPathExists = (pathToVerify: string, description: string): void => {
|
|||||||
|
|
||||||
// Initialize the path utilities (only verbose logs in development)
|
// Initialize the path utilities (only verbose logs in development)
|
||||||
const isDev = isDevEnvironment();
|
const isDev = isDevEnvironment();
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
console.log('---------------------------------------------------------');
|
console.log('---------------------------------------------------------');
|
||||||
console.log('[PATH-UTILS] Initializing path utilities...');
|
console.log('[PATH-UTILS] Initializing path utilities...');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine project root
|
// Determine project root
|
||||||
const projectRoot = getProjectRoot();
|
const projectRoot = getProjectRoot();
|
||||||
|
|
||||||
@ -131,11 +148,13 @@ export const internalApiHtmlFilePath = path.join(openApiDirectoryPath, 'internal
|
|||||||
if (isDev) {
|
if (isDev) {
|
||||||
console.log('[PATH-UTILS] Project root resolved to:', projectRoot);
|
console.log('[PATH-UTILS] Project root resolved to:', projectRoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
verifyPathExists(publicDirectoryPath, 'Public files directory');
|
verifyPathExists(publicDirectoryPath, 'Public files directory');
|
||||||
verifyPathExists(webcomponentBundlePath, 'Webcomponent bundle');
|
verifyPathExists(webcomponentBundlePath, 'Webcomponent bundle');
|
||||||
verifyPathExists(frontendHtmlPath, 'Index HTML file');
|
verifyPathExists(frontendHtmlPath, 'Index HTML file');
|
||||||
verifyPathExists(publicApiHtmlFilePath, 'Public API documentation');
|
verifyPathExists(publicApiHtmlFilePath, 'Public API documentation');
|
||||||
verifyPathExists(internalApiHtmlFilePath, 'Internal API documentation');
|
verifyPathExists(internalApiHtmlFilePath, 'Internal API documentation');
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
console.log('---------------------------------------------------------');
|
console.log('---------------------------------------------------------');
|
||||||
console.log('');
|
console.log('');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user