Files
klz-cables.com/.pnpm-store/v10/files/8e/8183a9b9c97f7b4b001a5736adc54a5d30c8ccfc9dcca0375272d22cfb6f1b3665d59f5f0c0002857abb6c5f0598070cbdffe2368fc873584c38b21dfc1b32
Marc Mintel 5397309103
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
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

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