mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-26 02:51:59 -04:00
fix: fixes for Node 4, style fixes
This commit is contained in:
committed by
Zoltan Kochan
parent
ca48f5d943
commit
08ef2e430b
@@ -6,23 +6,21 @@ async function delay(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
async function lock(lockFilename: string, notFirstTime: boolean = false): Promise<{}> {
|
||||
async function lock(lockFilename: string, firstTime: boolean): Promise<{}> {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
lockfile.lock(
|
||||
lockFilename,
|
||||
{realpath: false},
|
||||
async (err: Error & {code: string}) => {
|
||||
if (err) {
|
||||
if (err.code === 'ELOCKED') {
|
||||
if (!notFirstTime) {
|
||||
logger.warn('waiting for another installation to complete...')
|
||||
}
|
||||
await delay(200)
|
||||
await lock(lockFilename, true)
|
||||
resolve()
|
||||
} else {
|
||||
reject(err)
|
||||
if (err && err.code === 'ELOCKED') {
|
||||
if (firstTime) {
|
||||
logger.warn('waiting for another installation to complete...')
|
||||
}
|
||||
await delay(200)
|
||||
await lock(lockFilename, false)
|
||||
resolve()
|
||||
} else if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
@@ -42,7 +40,7 @@ async function unlock(lockFilename: string): Promise<{}> {
|
||||
|
||||
export default async function withLock<T> (storePath: string, fn: () => Promise<T>): Promise<T> {
|
||||
const lockFilename: string = path.resolve(storePath, 'lock')
|
||||
await lock(lockFilename)
|
||||
await lock(lockFilename, true)
|
||||
try {
|
||||
const result = await fn()
|
||||
await unlock(lockFilename)
|
||||
|
||||
Reference in New Issue
Block a user