Skip to content

Commit

Permalink
fix: retry writing traced files if there are conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 30, 2022
1 parent 2049def commit 36ca9b0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/rollup/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export function externals (opts: NodeExternalsOptions): Plugin {
}
}

const writeFile = async (file) => {
const writeFile = async (file: string) => {
if (!await isFile(file)) { return }
const src = resolve(opts.traceOptions.base, file)
const { pkgName, subpath } = parseNodeModulePath(file)
Expand All @@ -230,7 +230,7 @@ export function externals (opts: NodeExternalsOptions): Plugin {
}

// Write traced files
await Promise.all(tracedFiles.map(writeFile))
await Promise.all(tracedFiles.map(file => retry(() => writeFile(file), 3)))

// Write an informative package.json
await fsp.writeFile(resolve(opts.outDir, 'package.json'), JSON.stringify({
Expand Down Expand Up @@ -264,3 +264,15 @@ async function isFile (file: string) {
throw err
}
}

async function retry (fn: () => Promise<void>, retries: number) {
let retry = 0
let error: any
while (retry++ < retries) {
try { return await fn() } catch (err) {
error = err
await new Promise(resolve => setTimeout(resolve, 2))
}
}
throw error
}

0 comments on commit 36ca9b0

Please sign in to comment.