{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import {\n    execFileSync as execFileSyncOriginal,\n    ExecFileOptions,\n    type ExecFileSyncOptionsWithStringEncoding\n} from 'node:child_process';\nimport {\n    ChildProcessPromise,\n    execFile as execFileOriginal\n} from 'promisify-child-process';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Delays the promise for the given duration.\n *\n * @param duration - An object specifying the delay duration in either seconds or milliseconds.\n * @returns A promise that resolves after the specified duration.\n *\n * @example\n * ```\n * import { delay } from './index';\n *\n * await delay({ seconds: 1 });\n *\n * console.log('1 second later');\n * ```\n */\nexport async function delay(\n    duration: { seconds: number } | { milliseconds: number }\n): Promise<void> {\n    let delayDuration: number;\n\n    if ('seconds' in duration) {\n        delayDuration = duration.seconds * 1000;\n    } else if ('milliseconds' in duration) {\n        delayDuration = duration.milliseconds;\n    } else {\n        throw new TypeError(\n            'Expected an object with either `seconds` or `milliseconds`.'\n        );\n    }\n\n    return new Promise((resolve) => {\n        setTimeout(resolve, delayDuration);\n    });\n}\n\n/**\n * Converts a `URL` or path to a file system path.\n *\n * __Not available in browsers.__\n *\n * @param urlOrPath - The input to convert.\n * @returns A file system path.\n *\n * @example\n * ```\n * import { toPath } from './index';\n *\n * const getUnicornPath = cwd => path.join(toPath(cwd), 'unicorn');\n * ```\n */\nexport function toPath(urlOrPath: URL | string): string {\n    return urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;\n}\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Finds the root directory of the given path.\n *\n * __Not available in browsers.__\n *\n * On Unix-based systems, the root is always `'/'`.\n * On Windows, the root varies and includes the drive letter (e.g., `'C:\\\\'`).\n *\n * This function operates purely on paths and does not interact with the file system.\n *\n * @param pathInput - The path or URL to check.\n * @returns The root directory of the path.\n *\n * @example\n * ```\n * import { rootDirectory } from './index';\n *\n * console.log(rootDirectory('/Users/x/y/z'));\n * //=> '/'\n *\n * console.log(rootDirectory('C:\\\\Users\\\\x\\\\y\\\\z'));\n * //=> 'C:\\\\'\n * ```\n */\nexport function rootDirectory(pathInput: string | URL): string {\n    return path.parse(toPath(pathInput)).root;\n}\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Creates an iterable for traversing from a given start path up to the root directory.\n *\n * __Not available in browsers.__\n *\n * This function operates purely on paths and does not interact with the file system.\n *\n * @param startPath - The starting path. Can be relative.\n * @returns An iterable that iterates over each parent directory up to the root.\n *\n * @example\n * ```\n * import { traversePathUp } from './index';\n *\n * for (const directory of traversePathUp('/Users/x/y/z')) {\n *   console.log(directory);\n *   //=> '/Users/x/y/z'\n *   //=> '/Users/x/y'\n *   //=> '/Users/x'\n *   //=> '/Users'\n *   //=> '/'\n * }\n * ```\n */\nexport function traversePathUp(startPath: string | URL): Iterable<string> {\n    return {\n        *[Symbol.iterator]() {\n            let currentPath = path.resolve(toPath(startPath));\n            let previousPath;\n\n            while (previousPath !== currentPath) {\n                yield currentPath;\n                previousPath = currentPath;\n                currentPath = path.resolve(currentPath, '..');\n            }\n        }\n    };\n}\n\nconst TEN_MEGABYTES_IN_BYTES = 10 * 1024 * 1024;\n\n// noinspection JSUnusedGlobalSymbols\n/**\n * Executes a file.\n *\n * Same as the built-in `execFile` but with:\n * - Promise API\n * - 10 MB `maxBuffer` instead of 1 MB\n *\n * __Not available in browsers.__\n *\n * @param file - The file to execute.\n * @param arguments_ - Arguments for the execution.\n * @param options - Execution options.\n * @returns The stdout and stderr of the executed file.\n *\n * @example\n * ```\n * import { execFile } from './index';\n *\n * console.log(await execFile('ls', ['-l']));\n * ```\n */\nexport function execFile(\n    file: string,\n    arguments_: string[],\n    options?: ExecFileOptions\n): ChildProcessPromise {\n    return execFileOriginal(file, arguments_, {\n        maxBuffer: TEN_MEGABYTES_IN_BYTES,\n        ...options\n    });\n}\n// noinspection JSUnusedGlobalSymbols\n/**\n * Executes a file synchronously.\n *\n * Same as the built-in `execFileSync` but with:\n * - String output instead of buffer (same as `execFile`)\n * - Does not output `stderr` to the terminal by default (same as `execFile`)\n * - 10 MB `maxBuffer` instead of 1 MB\n *\n * __Not available in browsers.__\n *\n * @param file - The file to execute.\n * @param arguments_ - Arguments for the execution.\n * @param options - Execution options.\n * @returns The stdout of the executed file.\n *\n * @example\n * ```\n * import { execFileSync } from './index';\n *\n * console.log(execFileSync('ls', ['-l']));\n * ```\n */\nexport function execFileSync(\n    file: string,\n    arguments_: readonly string[] = [],\n    options?: ExecFileSyncOptionsWithStringEncoding\n): string {\n    return execFileSyncOriginal(file, arguments_, {\n        maxBuffer: TEN_MEGABYTES_IN_BYTES,\n        encoding: 'utf8',\n        stdio: 'pipe',\n        ...options\n    });\n}\n"],"names":["fileURLToPath","execFileOriginal","execFileSyncOriginal"],"mappings":";;;;;;AA4BA,eAAsB,MAClB,UAAwD;AAEpD,MAAA;AAEJ,MAAI,aAAa,UAAU;AACvB,oBAAgB,SAAS,UAAU;AAAA,EAAA,WAC5B,kBAAkB,UAAU;AACnC,oBAAgB,SAAS;AAAA,EAAA,OACtB;AACG,UAAA,IAAI,UACN,6DAA6D;AAAA,EAAA;AAI9D,SAAA,IAAI,QAAQ,CAAC,YAAW;AAC3B,eAAW,SAAS,aAAa;AAAA,EAAA,CACpC;AACL;AAiBM,SAAU,OAAO,WAAuB;AAC1C,SAAO,qBAAqB,MAAMA,uBAAc,SAAS,IAAI;AACjE;AA2BM,SAAU,cAAc,WAAuB;AACjD,SAAO,KAAK,MAAM,OAAO,SAAS,CAAC,EAAE;AACzC;AA2BM,SAAU,eAAe,WAAuB;AAC3C,SAAA;AAAA,IACH,EAAE,OAAO,QAAQ,IAAC;AACd,UAAI,cAAc,KAAK,QAAQ,OAAO,SAAS,CAAC;AAC5C,UAAA;AAEJ,aAAO,iBAAiB,aAAa;AAC3B,cAAA;AACS,uBAAA;AACD,sBAAA,KAAK,QAAQ,aAAa,IAAI;AAAA,MAAA;AAAA,IAChD;AAAA;AAGZ;AAEA,MAAM,yBAAyB,KAAK,OAAO;AAwB3B,SAAA,SACZ,MACA,YACA,SAAyB;AAElB,SAAAC,sBAAA,SAAiB,MAAM,YAAY;AAAA,IACtC,WAAW;AAAA,IACX,GAAG;AAAA,EAAA,CACN;AACL;AAwBM,SAAU,aACZ,MACA,aAAgC,CAAA,GAChC,SAA+C;AAExC,SAAAC,mBAAA,aAAqB,MAAM,YAAY;AAAA,IAC1C,WAAW;AAAA,IACX,UAAU;AAAA,IACV,OAAO;AAAA,IACP,GAAG;AAAA,EAAA,CACN;AACL;;;;;;;"}