From acdf7f41393ede724df263bdfc526dfaafb48dd2 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sun, 3 Nov 2024 15:49:59 +0800 Subject: [PATCH] Optimize comment.js --- .github/workflows/commenter.yml | 2 +- lib/comment.js | 60 +++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/.github/workflows/commenter.yml b/.github/workflows/commenter.yml index ce1f94c..9505b6b 100644 --- a/.github/workflows/commenter.yml +++ b/.github/workflows/commenter.yml @@ -13,7 +13,7 @@ jobs: uses: actions/setup-node@v4 - run: npm install - run: | - node lib/comment.js | tee -a results.txt + node lib/comment.js --diff | tee -a results.txt - name: Comment PR uses: marocchino/sticky-pull-request-comment@v2 with: diff --git a/lib/comment.js b/lib/comment.js index e5173fa..533aaee 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -5,6 +5,16 @@ const yaml = require('js-yaml'); const { spawnSync } = require('child_process'); const ssri = require('ssri'); const { getVendors } = require('hexo-theme-next/scripts/events/lib/utils'); +const yargs = require('yargs/yargs'); +const { hideBin } = require('yargs/helpers'); + +const argv = yargs(hideBin(process.argv)) + .option('diff', { + describe: 'Enable diff mode', + type: 'boolean', + default: false + }) + .argv; const vendorsFile = fs.readFileSync( path.join(path.dirname(require.resolve('hexo-theme-next')), '_vendors.yml') @@ -12,22 +22,6 @@ const vendorsFile = fs.readFileSync( const dependencies = yaml.load(vendorsFile); const newPlugins = require('../package.json').dependencies; -const oldPlugins = JSON.parse(spawnSync('curl', ['https://raw.githubusercontent.com/next-theme/plugins/main/package.json']).stdout).dependencies; - -const diff = []; -Object.keys(newPlugins).forEach(key => { - if (!(key in oldPlugins) || newPlugins[key] !== oldPlugins[key]) { - diff.push(key); - } -}); -if (diff.length > 0) { - console.log('The following packages are updated in the Pull Request.'); - console.log(diff.map(item => '- ' + item).join('\n')); - console.log('\nThe affected CDN links are listed below.'); - printCDNTable(); -} else { - console.log('No packages are updated in the Pull Request.'); -} function request(url) { return new Promise((resolve, reject) => { @@ -75,10 +69,10 @@ async function formatTable(name, links, groundTruth) { ${content}`); } -async function printCDNTable() { +async function printCDNTable(names) { for (const [key, value] of Object.entries(dependencies)) { const { name, file } = value; - if (!diff.includes(name) || !file) continue; + if (!names.includes(name) || !file) continue; const version = newPlugins[name]; const links = getVendors({ ...value, version, minified: file }); const integrity = await ssri @@ -86,3 +80,33 @@ async function printCDNTable() { await formatTable(key, links, integrity.toString()); } } + +async function printDiff() { + const oldPlugins = JSON.parse(spawnSync('curl', ['https://raw.githubusercontent.com/next-theme/plugins/main/package.json']).stdout).dependencies; + + const diff = []; + Object.keys(newPlugins).forEach(key => { + if (!(key in oldPlugins) || newPlugins[key] !== oldPlugins[key]) { + diff.push(key); + } + }); + if (diff.length > 0) { + console.log('The following packages are updated in the Pull Request.'); + console.log(diff.map(item => '- ' + item).join('\n')); + console.log('\nThe affected CDN links are listed below.'); + printCDNTable(diff); + } else { + console.log('No packages are updated in the Pull Request.'); + } +} + +async function printAll() { + const names = Object.keys(dependencies).map(key => dependencies[key].name); + printCDNTable(names); +} + +if (argv.diff) { + printDiff(); +} else { + printAll(); +}