Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
98 lines
3.2 KiB
Plaintext
98 lines
3.2 KiB
Plaintext
import fs from 'fs';
|
|
import path from 'path';
|
|
/**
|
|
* Synchronously walks up parent directories until a condition is met and/or one of the file names within the fileNames array is found.
|
|
*/ export function findUpSync({ condition, dir, fileNames }) {
|
|
const { root } = path.parse(dir);
|
|
while(true){
|
|
if (fileNames?.length) {
|
|
let found = false;
|
|
for (const fileName of fileNames){
|
|
const filePath = path.join(dir, fileName);
|
|
const exists = pathExistsAndIsAccessibleSync(filePath);
|
|
if (exists) {
|
|
if (!condition) {
|
|
return filePath;
|
|
}
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!found && dir !== root) {
|
|
dir = path.dirname(dir); // Move up one directory level.
|
|
continue;
|
|
}
|
|
}
|
|
const result = condition?.(dir);
|
|
if (result === true) {
|
|
return dir;
|
|
}
|
|
if (typeof result === 'string' && result?.length) {
|
|
return result;
|
|
}
|
|
if (dir === root) {
|
|
return null // Reached the root directory without a match.
|
|
;
|
|
}
|
|
dir = path.dirname(dir); // Move up one directory level.
|
|
}
|
|
}
|
|
/**
|
|
* Asynchronously walks up parent directories until a condition is met and/or one of the file names within the fileNames array is found.
|
|
*/ export async function findUp({ condition, dir, fileNames }) {
|
|
const { root } = path.parse(dir);
|
|
while(true){
|
|
if (fileNames?.length) {
|
|
let found = false;
|
|
for (const fileName of fileNames){
|
|
const filePath = path.resolve(dir, fileName);
|
|
const exists = await pathExistsAndIsAccessible(filePath);
|
|
if (exists) {
|
|
if (!condition) {
|
|
return filePath;
|
|
}
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!found && dir !== root) {
|
|
dir = path.dirname(dir); // Move up one directory level.
|
|
continue;
|
|
}
|
|
}
|
|
const result = await condition?.(dir);
|
|
if (result === true) {
|
|
return dir;
|
|
}
|
|
if (typeof result === 'string' && result?.length) {
|
|
return result;
|
|
}
|
|
if (dir === root) {
|
|
return null // Reached the root directory without a match.
|
|
;
|
|
}
|
|
dir = path.dirname(dir); // Move up one directory level.
|
|
}
|
|
}
|
|
// From https://github.com/sindresorhus/path-exists/blob/main/index.js
|
|
// fs.accessSync is preferred over fs.existsSync as it's usually a good idea
|
|
// to check if the process has permission to read/write to a file before doing so.
|
|
// Also see https://github.com/nodejs/node/issues/39960
|
|
export function pathExistsAndIsAccessibleSync(path) {
|
|
try {
|
|
fs.accessSync(path);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
export async function pathExistsAndIsAccessible(path) {
|
|
try {
|
|
await fs.promises.access(path);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//# sourceMappingURL=findUp.js.map |