Skip to content

Commit

Permalink
Optimize comment.js
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Nov 3, 2024
1 parent 2cfa2fa commit acdf7f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 42 additions & 18 deletions lib/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@ 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')
);
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) => {
Expand Down Expand Up @@ -75,14 +69,44 @@ 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
.fromStream(fs.createReadStream(`./node_modules/${name}/${file}`), { algorithms: ['sha256'] });
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();
}

0 comments on commit acdf7f4

Please sign in to comment.