Skip to content

Commit

Permalink
refactor: rename to resolveRootPackageJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 12, 2022
1 parent afb6ab7 commit 2b4fc2e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 29 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<string> {
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 })
}
}
33 changes: 0 additions & 33 deletions src/workspace.ts

This file was deleted.

8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'))
})
})

Expand Down

0 comments on commit 2b4fc2e

Please sign in to comment.