Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add resolveLockfile #14

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ const filename = await resolveFile('README.md', {
})
```

### `resolveLockFile`

Find path to the lock file (`yarn.lock`, `package-lock.json`, `pnpm-lock.yaml`, `npm-shrinkwrap.json`) or throws an error.

```js
import { resolveLockFile } from 'pkg-types'
const lockfile = await resolveFile('.')
```

## Types

**Note:** In order to make types working, you need to install `typescript` as a devDependency.
Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ 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 lockFiles = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml', 'npm-shrinkwrap.json']

export async function resolveLockfile (id: string = process.cwd(), opts: ResolveOptions = {}): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts)
const _opts = { startingFrom: resolvedPath, ...opts }
for (const lockFile of lockFiles) {
try {
return await findNearestFile(lockFile, _opts)
} catch { }
}
throw new Error('No lockfile found from ' + id)
}
Empty file added test/fixture/sub/yarn.lock
Empty file.
12 changes: 11 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
writePackageJSON,
writeTSConfig,
TSConfig,
ResolveOptions
ResolveOptions,
resolveLockfile
} from '../src'

const fixtureDir = resolve(dirname(fileURLToPath(import.meta.url)), 'fixture')
Expand Down Expand Up @@ -87,3 +88,12 @@ describe('tsconfig.json', () => {
// expectTypeOf(options.maxNodeModuleJsDepth).toEqualTypeOf<number | undefined>()
})
})

describe('resolveLockfile', () => {
it('works for subdir', async () => {
expect(await resolveLockfile(rFixture('./sub'))).to.equal(rFixture('./sub/yarn.lock'))
})
it('works for root dir', async () => {
expect(await resolveLockfile(rFixture('.'))).to.equal(rFixture('../..', 'pnpm-lock.yaml'))
})
})