fix: race condition between normalize-packages and usage-to-readme
The first was writting `package.json` at the same time the second one was reading it. The file is now read only once by `normalize-packages`.
This commit is contained in:
parent
1026d18e4b
commit
7864c05ee1
@ -126,7 +126,7 @@ require('exec-promise')(() =>
|
||||
|
||||
return Promise.all([
|
||||
forceRelativeSymlink(NPMIGNORE_PATH, `${dir}/.npmignore`),
|
||||
usageToReadme(`${dir}/.USAGE.md`).catch(error => {
|
||||
usageToReadme(`${dir}/.USAGE.md`, dir, pkg).catch(error => {
|
||||
if (error.code !== 'ENOENT') {
|
||||
console.error('Error while handling README', error)
|
||||
}
|
||||
|
@ -9,41 +9,40 @@ const { execFile } = require('node:child_process')
|
||||
|
||||
const generateReadme = readFile(join(__dirname, 'README.md.tpl'), 'utf8').then(compile)
|
||||
|
||||
async function usageToReadme(...args) {
|
||||
const gitAdd = args[0] === '--git-add'
|
||||
async function usageToReadme(usagePath, pkgDir, pkg, gitAdd = false) {
|
||||
const usage = await readFile(usagePath, 'utf8')
|
||||
|
||||
const readmePath = join(pkgDir, 'README.md')
|
||||
await writeFile(readmePath, (await generateReadme)({ pkg, usage: usage.trim() }))
|
||||
|
||||
if (gitAdd) {
|
||||
args.shift()
|
||||
await new Promise((resolve, reject) => {
|
||||
execFile('git', ['add', '--', readmePath])
|
||||
.on('error', reject)
|
||||
.on('exit', code => {
|
||||
if (code !== 0) {
|
||||
reject(new Error('command exited with non-zero status code: ' + code))
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
module.exports = usageToReadme
|
||||
|
||||
for (const usagePath of args) {
|
||||
const dir = dirname(usagePath)
|
||||
|
||||
const [usage, pkg] = await Promise.all([
|
||||
readFile(usagePath, 'utf8'),
|
||||
readFile(join(dir, 'package.json')).then(JSON.parse),
|
||||
])
|
||||
|
||||
const readmePath = join(dir, 'README.md')
|
||||
await writeFile(readmePath, (await generateReadme)({ pkg, usage: usage.trim() }))
|
||||
|
||||
if (module.parent === null) {
|
||||
async function main(...args) {
|
||||
const gitAdd = args[0] === '--git-add'
|
||||
if (gitAdd) {
|
||||
await new Promise((resolve, reject) => {
|
||||
execFile('git', ['add', '--', readmePath])
|
||||
.on('error', reject)
|
||||
.on('exit', code => {
|
||||
if (code !== 0) {
|
||||
reject(new Error('command exited with non-zero status code: ' + code))
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
args.shift()
|
||||
}
|
||||
|
||||
for (const usagePath of args) {
|
||||
const dir = dirname(usagePath)
|
||||
|
||||
await usageToReadme(usagePath, dir, await readFile(join(dir, 'package.json')).then(JSON.parse), gitAdd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (module.parent !== null) {
|
||||
module.exports = usageToReadme
|
||||
} else {
|
||||
usageToReadme(...process.argv.slice(2))
|
||||
main(...process.argv.slice(2))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user