From b29f9a6812bd15461b94138bfc605f63a90b1f95 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 3 Jul 2021 20:15:54 +0800 Subject: [PATCH] chore: update pjax-api script --- scripts/fix-pjax-api.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/scripts/fix-pjax-api.js b/scripts/fix-pjax-api.js index b3ac6c7..b20976a 100644 --- a/scripts/fix-pjax-api.js +++ b/scripts/fix-pjax-api.js @@ -5,17 +5,15 @@ const fs = require('fs').promises const path = require('path') -function modify(source = '', pairs = [], loose = true) { +function modify(source = '', pairs = []) { for (const [original, replace] of pairs) { if (source.includes(original)) { source = source.replace(original, replace) if (source.includes(original)) { throw new Error(`More than one original string found`, JSON.stringify(original)) } - } else { - if (loose) console.log(`Skipped missing snippet`, original) - else throw new Error(`Original string not found`, JSON.stringify(original)) } + throw new Error(`Original string not found`, JSON.stringify(original)) } return source @@ -54,11 +52,16 @@ async function fixPJAXAPI(loose) { }`, ], ] - const filePath = path.resolve(__dirname, '..', `node_modules/pjax-api/dist/pjax-api.js`) - const source = await fs.readFile(filePath, 'utf-8') - const modified = modify(source, pairs, loose) - fs.writeFile(filePath, modified, 'utf-8') + try { + const filePath = path.resolve(__dirname, '..', `node_modules/pjax-api/dist/pjax-api.js`) + const source = await fs.readFile(filePath, 'utf-8') + const modified = modify(source, pairs, loose) + await fs.writeFile(filePath, modified, 'utf-8') + } catch (err) { + console.error((err && err.message) || err) + const shouldTerminate = true + if (shouldTerminate) process.exit(1) + } } -const loose = process.argv.includes('loose') -fixPJAXAPI(loose) +fixPJAXAPI()