diff --git a/README.md b/README.md index 18c17b0..1b362bf 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,15 @@ const filename = await resolvePackageJSON() const packageJson = await resolvePackageJSON('/fully/resolved/path/to/folder') ``` +### `resolveRootPackageJSON` + +```js +import { resolveRootPackageJSON } from 'pkg-types' +const filename = await resolveRootPackageJSON() +// or +const packageJson = await resolveRootPackageJSON('/fully/resolved/path/to/folder') +``` + ### `readPackageJSON` ```js diff --git a/src/index.ts b/src/index.ts index 1d5d17a..e86cdbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,13 @@ import { promises as fsp } from 'fs' import * as jsonc from 'jsonc-parser' import { ResolveOptions as _ResolveOptions, resolvePath } from 'mlly' -import { isAbsolute } from 'pathe' +import { isAbsolute, join } from 'pathe' import { findNearestFile, FindNearestFileOptions } from './utils' import type { PackageJson, TSConfig } from './types' +import { isFile } from './_utils' export * from './types' export * from './utils' -export * from './workspace' export type ResolveOptions = _ResolveOptions & FindNearestFileOptions @@ -48,3 +48,30 @@ export async function resolveTSConfig (id: string = process.cwd(), opts: Resolve const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts) return findNearestFile('tsconfig.json', { startingFrom: resolvedPath, ...opts }) } + +const rootFiles = ['pnpm-workspace.yaml', 'pnpm-lock.yaml', 'yarn.lock', 'lerna.json'] + +export async function resolveRootPackageJSON (id: string = process.cwd(), opts: ResolveOptions = {}): Promise { + const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts) + try { + return await findNearestFile('package.json', { + startingFrom: resolvedPath, + test: async (filePath) => { + if (!isFile(filePath)) { + return false + } + if (rootFiles.some(file => isFile(join(filePath, '..', file)))) { + return true + } + try { + const blob = await fsp.readFile(resolvedPath, 'utf-8') + return !!(JSON.parse(blob) as PackageJson).workspaces + } catch {} + return false + }, + ...opts + }) + } catch { + return findNearestFile('package.json', { startingFrom: resolvedPath, ...opts }) + } +} diff --git a/src/workspace.ts b/src/workspace.ts deleted file mode 100644 index ce1b06a..0000000 --- a/src/workspace.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { promises as fsp } from 'fs' -import { ResolveOptions, resolvePath } from 'mlly' -import { isAbsolute, join } from 'pathe' -import { PackageJson } from './types' -import { findNearestFile } from './utils' -import { isFile } from './_utils' - -const rootFiles = ['pnpm-workspace.yaml', 'pnpm-lock.yaml', 'yarn.lock', 'lerna.json'] - -export async function findWorkspaceRoot (id: string = process.cwd(), opts: ResolveOptions = {}): Promise { - const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts) - try { - return await findNearestFile('package.json', { - startingFrom: resolvedPath, - test: async (filePath) => { - if (!isFile(filePath)) { - return false - } - if (rootFiles.some(file => isFile(join(filePath, '..', file)))) { - return true - } - try { - const blob = await fsp.readFile(resolvedPath, 'utf-8') - return !!(JSON.parse(blob) as PackageJson).workspaces - } catch {} - return false - }, - ...opts - }) - } catch { - return findNearestFile('package.json', { startingFrom: resolvedPath, ...opts }) - } -} diff --git a/test/index.test.ts b/test/index.test.ts index 588e64d..85056ef 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -7,10 +7,10 @@ import { readTSConfig, resolveTSConfig, resolvePackageJSON, + resolveRootPackageJSON, writePackageJSON, writeTSConfig, - TSConfig, - findWorkspaceRoot + TSConfig } from '../src' const fixtureDir = resolve(dirname(fileURLToPath(import.meta.url)), 'fixture') @@ -63,8 +63,8 @@ describe('package.json', () => { expect(await readPackageJSON('pathe').then(p => p?.version)).to.be.a('string') }) - it('finds the workspace root', async () => { - expect(await findWorkspaceRoot(rFixture('.'))).toBe(rFixture('../../package.json')) + it('finds the root package.json', async () => { + expect(await resolveRootPackageJSON(rFixture('.'))).toBe(rFixture('../../package.json')) }) })