simple-icons/scripts/release/update-svgs-count.js
Álvaro Mondéjar ed4c29f7b6
Automatically update milestone (SVG count) in README (#6951)
* Automatically update milestone (SVG count) in README

* Use updateRange variale

* Rewrite 2 lines

* Simplify code

* Format new file

* Fix error in script

* Apply suggested changes
2021-12-09 17:02:58 -08:00

39 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
/**
* @fileoverview
* Replaces the SVG count milestone "Over <NUMBER> Free SVG icons..." located
* at README every time the number of current icons is more than `updateRange`
* more than the previous milestone.
*/
const fs = require('fs');
const path = require('path');
const regexMatcher = /Over\s(\d+)\s/;
const updateRange = 100;
const rootDir = path.resolve(__dirname, '..', '..');
const dataFile = path.resolve(rootDir, '_data', 'simple-icons.json');
const readmeFile = path.resolve(rootDir, 'README.md');
const readmeContent = fs.readFileSync(readmeFile, 'utf-8');
let overNIconsInReadme;
try {
overNIconsInReadme = parseInt(regexMatcher.exec(readmeContent)[1]);
} catch (err) {
console.error(
'Failed to obtain number of SVG icons of current milestone in README:',
err,
);
process.exit(1);
}
const nIcons = require(dataFile).icons.length,
newNIcons = overNIconsInReadme + updateRange;
if (nIcons <= newNIcons) {
process.exit(0);
}
const newContent = readmeContent.replace(regexMatcher, `Over ${newNIcons} `);
fs.writeFileSync(readmeFile, newContent);