fix: patch @primer/behaviors

This commit is contained in:
EnixCoda 2022-12-16 01:24:19 +08:00
parent e5096e89b6
commit 20ed9b9a59
2 changed files with 36 additions and 6 deletions

View file

@ -0,0 +1,23 @@
const { fixDep } = require('.')
const targetFilePath = `@primer/behaviors/dist/esm/anchored-position.js`
const pairs = [
[
`if (parentNode === document.body)`,
`if (parentNode === document) break
if (parentNode === document.body)`,
],
[
`const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;`, // prettier-ignore
`const clippingNode = parentNode === document ? document.documentElement : parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;`, // prettier-ignore
],
]
exports.fix = async () => {
try {
await fixDep(targetFilePath, pairs)
} catch (err) {
console.error((err && err.message) || err)
process.exit(1)
}
}

View file

@ -13,10 +13,6 @@ function modify(source = '', pairs = []) {
} else {
throw new Error(`Original string not found: ${JSON.stringify(original)}`)
}
if (source.includes(original)) {
throw new Error(`More than one original string found`, JSON.stringify(original))
}
}
return source
@ -24,15 +20,26 @@ function modify(source = '', pairs = []) {
const nodeModulesPath = path.resolve(__dirname, '../../', `node_modules`)
const MODIFIED_MARK = `\n/* This file has been modified */\n`
exports.fixDep = async function fixDep(targetFilePath, pairs) {
const filePath = path.resolve(nodeModulesPath, targetFilePath)
const source = await fs.readFile(filePath, 'utf-8')
const modified = modify(source, pairs)
if (source.includes(MODIFIED_MARK)) {
console.log(`${filePath} has been fixed, skipping.`)
return
}
console.log(`Fixing ${targetFilePath}`)
const modified = modify(source, pairs) + MODIFIED_MARK
await fs.writeFile(filePath, modified, 'utf-8')
}
async function fixDeps() {
for (const fix of [require('./pjax-api').fix, require('./styled-components').fix]) {
for (const fix of [
require('./pjax-api').fix,
require('./styled-components').fix,
require('./@primer__behaviors').fix,
]) {
await fix()
}
}