Skip to content

Commit

Permalink
fix: recursively search for packer lock file for all 3 packers suppor…
Browse files Browse the repository at this point in the history
…ted (#502)

* fix: recursively search for packer lock file for all 3 packers supported

* fix: use array as argument for findUpIO

* fix: use pure fs, avoid unpacking monad

closes #485
  • Loading branch information
Dutzu authored Oct 11, 2023
1 parent 0c7ce8f commit fdf091e
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,30 @@ export function spawnProcess(command: string, args: string[], options: execa.Opt

const rootOf = (p: string) => path.parse(path.resolve(p)).root;
const isPathRoot = (p: string) => rootOf(p) === path.resolve(p);
const findUpIO = (name: string, directory = process.cwd()): IOO.IOOption<string> =>
const findUpIO = (names: string[], directory = process.cwd()): IOO.IOOption<string> =>
pipe(path.resolve(directory), (dir) =>
pipe(
safeFileExistsIO(path.join(dir, name)),
IO.chain((exists: boolean) => {
if (exists) return IOO.some(dir);
IO.sequenceArray(names.map((name) => safeFileExistsIO(path.join(dir, name)))),
IO.chain((exist) => {
if (exist.some(Boolean)) return IOO.some(dir);
if (isPathRoot(dir)) return IOO.none;
return findUpIO(name, path.dirname(dir));
return findUpIO(names, path.dirname(dir));
})
)
);

/**
* Find a file by walking up parent directories
*/
export const findUp = (name: string) => pipe(findUpIO(name), IOO.toUndefined)();
export const findUp = (name: string) => pipe(findUpIO([name]), IOO.toUndefined)();

/**
* Forwards `rootDir` or finds project root folder.
*/
export const findProjectRoot = (rootDir?: string) =>
pipe(
IOO.fromNullable(rootDir),
IOO.fold(() => findUpIO('yarn.lock'), IOO.of),
IOO.fold(() => findUpIO('pnpm-lock.yaml'), IOO.of),
IOO.fold(() => findUpIO('package-lock.json'), IOO.of),
IOO.fold(() => findUpIO(['yarn.lock', 'pnpm-lock.yaml', 'package-lock.json']), IOO.of),
IOO.toUndefined
)();

Expand Down

0 comments on commit fdf091e

Please sign in to comment.