From 65562a6d9e36018181f5261f04fac7f6c926eed1 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 | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/scripts/fix-pjax-api.js b/scripts/fix-pjax-api.js index b3ac6c7..7fbccc0 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,14 @@ 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 loose = process.argv.includes('loose') -fixPJAXAPI(loose) +fixPJAXAPI()