Skip to content

Commit

Permalink
feat: add findWorkspaceRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 10, 2022
1 parent 22bc2da commit eb32854
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { statSync } from 'fs'

export const isFile = (path: string): boolean => {
try {
if (statSync(path).isFile()) { return true }
} catch {}
return false
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isAbsolute } from 'pathe'

export * from './types'
export * from './utils'
export * from './workspace'

export type ResolveOptions = _ResolveOptions & FindNearestFileOptions

Expand Down
9 changes: 2 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { statSync } from 'fs'
import { join, resolve } from 'pathe'
import { isFile } from './_utils'

export interface FindNearestFileOptions {
/**
Expand All @@ -24,12 +24,7 @@ export interface FindNearestFileOptions {
const defaultFindOptions: Required<FindNearestFileOptions> = {
startingFrom: '.',
rootPattern: /^node_modules$/,
test: (filePath: string) => {
try {
if (statSync(filePath).isFile()) { return true }
} catch { }
return null
}
test: (filePath: string) => isFile(filePath) || null
}

export async function findNearestFile (filename: string, _options: FindNearestFileOptions = {}): Promise<string> {
Expand Down
34 changes: 34 additions & 0 deletions src/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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'
import { resolvePackageJSON } from '.'

const rootFiles = ['pnpm-workspace.yaml', 'pnpm-lock.yaml', 'yarn.lock', 'lerna.json']

export async function findWorkspaceRoot (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 resolvePackageJSON(id, opts)
}
}
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
writePackageJSON,
writeTSConfig,
TSConfig,
findWorkspaceRoot
} from '../src'

const fixtureDir = resolve(dirname(fileURLToPath(import.meta.url)), 'fixture')
Expand Down Expand Up @@ -61,6 +62,10 @@ describe('package.json', () => {
it('correctly reads a version from package', async () => {
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'))
})
})

describe('tsconfig.json', () => {
Expand Down

0 comments on commit eb32854

Please sign in to comment.