Skip to content

Commit

Permalink
refactor: reuse readwantedlockfile logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Mar 27, 2022
1 parent 6be9138 commit a4369ef
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions packages/lockfile-file/src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,10 @@ export async function readWantedLockfileAndAutofixConflicts (
lockfile: Lockfile | null
hadConflicts: boolean
}> {
const lockfileNames: string[] = [WANTED_LOCKFILE]
if (opts.useGitBranchLockfile) {
const gitBranchLockfileName: string = await getWantedLockfileName(opts)
if (gitBranchLockfileName !== WANTED_LOCKFILE) {
lockfileNames.unshift(gitBranchLockfileName)
}
}
let result: { lockfile: Lockfile | null, hadConflicts: boolean } = { lockfile: null, hadConflicts: false }
for (const lockfileName of lockfileNames) {
result = await _read(path.join(pkgPath, lockfileName), pkgPath, { ...opts, autofixMergeConflicts: true })
if (result.lockfile) {
if (opts.mergeGitBranchLockfiles) {
result.lockfile = await _mergeGitBranchLockfiles(result.lockfile, pkgPath, pkgPath, opts)
}
break
}
}
return result
return _readWantedLockfile(pkgPath, {
...opts,
autofixMergeConflicts: true,
})
}

export async function readWantedLockfile (
Expand All @@ -70,24 +56,7 @@ export async function readWantedLockfile (
mergeGitBranchLockfiles?: boolean
}
): Promise<Lockfile | null> {
const lockfileNames: string[] = [WANTED_LOCKFILE]
if (opts.useGitBranchLockfile) {
const gitBranchLockfileName: string = await getWantedLockfileName(opts)
if (gitBranchLockfileName !== WANTED_LOCKFILE) {
lockfileNames.unshift(gitBranchLockfileName)
}
}
let lockfile: Lockfile | null = null
for (const lockfileName of lockfileNames) {
lockfile = (await _read(path.join(pkgPath, lockfileName), pkgPath, opts)).lockfile
if (lockfile) {
if (opts.mergeGitBranchLockfiles) {
lockfile = await _mergeGitBranchLockfiles(lockfile, pkgPath, pkgPath, opts)
}
break
}
}
return lockfile
return (await _readWantedLockfile(pkgPath, opts)).lockfile
}

async function _read (
Expand Down Expand Up @@ -189,6 +158,39 @@ export function createLockfileObject (
}
}

async function _readWantedLockfile (
pkgPath: string,
opts: {
wantedVersion?: number
ignoreIncompatible: boolean
useGitBranchLockfile?: boolean
mergeGitBranchLockfiles?: boolean
autofixMergeConflicts?: boolean
}
): Promise<{
lockfile: Lockfile | null
hadConflicts: boolean
}> {
const lockfileNames: string[] = [WANTED_LOCKFILE]
if (opts.useGitBranchLockfile) {
const gitBranchLockfileName: string = await getWantedLockfileName(opts)
if (gitBranchLockfileName !== WANTED_LOCKFILE) {
lockfileNames.unshift(gitBranchLockfileName)
}
}
let result: { lockfile: Lockfile | null, hadConflicts: boolean } = { lockfile: null, hadConflicts: false }
for (const lockfileName of lockfileNames) {
result = await _read(path.join(pkgPath, lockfileName), pkgPath, { ...opts, autofixMergeConflicts: true })
if (result.lockfile) {
if (opts.mergeGitBranchLockfiles) {
result.lockfile = await _mergeGitBranchLockfiles(result.lockfile, pkgPath, pkgPath, opts)
}
break
}
}
return result
}

async function _mergeGitBranchLockfiles (
lockfile: Lockfile | null,
lockfileDir: string,
Expand Down

0 comments on commit a4369ef

Please sign in to comment.