From 20ed9b9a595c8451caa9f5f1803739a53157c88a Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 16 Dec 2022 01:24:19 +0800 Subject: [PATCH] fix: patch @primer/behaviors --- scripts/fix-deps/@primer__behaviors.js | 23 +++++++++++++++++++++++ scripts/fix-deps/index.js | 19 +++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 scripts/fix-deps/@primer__behaviors.js diff --git a/scripts/fix-deps/@primer__behaviors.js b/scripts/fix-deps/@primer__behaviors.js new file mode 100644 index 0000000..8e4bdbe --- /dev/null +++ b/scripts/fix-deps/@primer__behaviors.js @@ -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) + } +} diff --git a/scripts/fix-deps/index.js b/scripts/fix-deps/index.js index bbb9d0b..bbd26a0 100644 --- a/scripts/fix-deps/index.js +++ b/scripts/fix-deps/index.js @@ -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() } }