From 9964ef5f8a2cf542b816436eada3d3124f21c0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Aparicio=20Mart=C3=ADn?= <40391767+apaniel@users.noreply.github.com> Date: Thu, 28 Jul 2022 17:51:18 +0200 Subject: [PATCH 1/2] feat: run from any folder in monorepo & pnpm --- src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e87c271..cb118ec 100755 --- a/src/index.ts +++ b/src/index.ts @@ -250,8 +250,16 @@ function getLines(data: string) { .split(/\r?\n/) } -function getRunner(root: string) { - return fs.isFile(join(root, 'package-lock.json')) ? 'npm' : 'yarn' +function getRunner(directory: string) { + if (fs.isFile(join(directory, 'package-lock.json'))) { + return 'npm' + } else if (fs.isFile(join(directory, 'yarn.lock'))) { + return 'yarn' + } else if (fs.isFile(join(directory, 'pnpm-lock.yaml'))) { + return 'pnpm' + } + + return getRunner(join(directory, '..')) } type Falsy = null | undefined | false | 0 | '' From b8321b888579b21d7519d06754d3b14a57ce8067 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:34:23 -0400 Subject: [PATCH 2/2] fix: avoid infinite loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …and refactor a little bit --- src/index.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index cb118ec..cfe3856 100755 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import fs = require('saxon/sync') import spawn from './spawn' import checksum from './checksum' import createLog from './log' +import * as os from 'os' const PKG_JSON = 'package.json' const CACHE_NAME = '.bic_cache' @@ -250,16 +251,23 @@ function getLines(data: string) { .split(/\r?\n/) } +const runnersByLockfile = { + 'package-lock.json': 'npm', + 'pnpm-lock.yaml': 'pnpm', + 'yarn.lock': 'yarn', +} + function getRunner(directory: string) { - if (fs.isFile(join(directory, 'package-lock.json'))) { - return 'npm' - } else if (fs.isFile(join(directory, 'yarn.lock'))) { - return 'yarn' - } else if (fs.isFile(join(directory, 'pnpm-lock.yaml'))) { - return 'pnpm' + for (const file of fs.list(directory)) { + const runner = runnersByLockfile[file] + if (runner) { + return runner + } } - - return getRunner(join(directory, '..')) + if (directory !== os.homedir()) { + return getRunner(dirname(directory)) + } + throw Error('No lockfile was found') } type Falsy = null | undefined | false | 0 | ''