From c76a7d255c74133ed33dd7aa965598316d12dd25 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 18 Jun 2024 18:54:05 -0700 Subject: [PATCH] use package-json-from-dist to look up package.json --- package-lock.json | 6 ++++++ package.json | 1 + src/bin.mts | 17 ++--------------- test/bin.ts | 9 +++++++-- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index e650409c..6b01c2c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "jackspeak": "^3.1.2", "minimatch": "^9.0.4", "minipass": "^7.1.2", + "package-json-from-dist": "^1.0.0", "path-scurry": "^1.11.1" }, "bin": { @@ -2864,6 +2865,11 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/package-json-from-dist": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/package-json-from-dist/-/package-json-from-dist-1.0.0.tgz", + "integrity": "sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw==" + }, "node_modules/pacote": { "version": "17.0.7", "resolved": "https://registry.npmjs.org/pacote/-/pacote-17.0.7.tgz", diff --git a/package.json b/package.json index f6f7eb4e..8b33f348 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "jackspeak": "^3.1.2", "minimatch": "^9.0.4", "minipass": "^7.1.2", + "package-json-from-dist": "^1.0.0", "path-scurry": "^1.11.1" }, "devDependencies": { diff --git a/src/bin.mts b/src/bin.mts index 80ae0672..34854dd6 100755 --- a/src/bin.mts +++ b/src/bin.mts @@ -1,25 +1,12 @@ #!/usr/bin/env node import { foregroundChild } from 'foreground-child' import { existsSync } from 'fs' -import { readFile } from 'fs/promises' import { jack } from 'jackspeak' +import { loadPackageJson } from 'package-json-from-dist' import { join } from 'path' -import { fileURLToPath } from 'url' import { globStream } from './index.js' -/* c8 ignore start */ -const { version } = JSON.parse( - await readFile( - fileURLToPath(new URL('../../package.json', import.meta.url)), - 'utf8', - ).catch(() => - readFile( - fileURLToPath(new URL('../../package.json', import.meta.url)), - 'utf8', - ), - ), -) as { version: string } -/* c8 ignore stop */ +const { version } = loadPackageJson(import.meta.url, '../package.json') const j = jack({ usage: 'glob [options] [ [ ...]]', diff --git a/test/bin.ts b/test/bin.ts index c5744f4d..68f961c6 100644 --- a/test/bin.ts +++ b/test/bin.ts @@ -1,9 +1,14 @@ import { spawn, SpawnOptions } from 'child_process' +import { readFileSync } from 'fs' import { sep } from 'path' import t from 'tap' import { fileURLToPath } from 'url' -import pkg from '../package.json' assert { type: 'json' } -const { version } = pkg +const { version } = JSON.parse( + readFileSync( + fileURLToPath(new URL('../package.json', import.meta.url)), + 'utf8', + ), +) const bin = fileURLToPath(new URL('../dist/esm/bin.mjs', import.meta.url)) t.cleanSnapshot = s => s.split(version).join('{VERSION}')