1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2024-11-16 00:59:07 +02:00
simple-icons/scripts/release/update-svgs-count.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-03-24 19:38:18 +02:00
#!/usr/bin/env node
/**
2024-06-06 14:40:35 +02:00
* @file
* 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.
*/
import fs from 'node:fs/promises';
import path from 'node:path';
2024-03-24 19:38:18 +02:00
import process from 'node:process';
import {getDirnameFromImportMeta, getIconsData} from '../../sdk.mjs';
const regexMatcher = /Over\s(\d+)\s/;
const updateRange = 100;
const __dirname = getDirnameFromImportMeta(import.meta.url);
2024-03-24 19:38:18 +02:00
const rootDirectory = path.resolve(__dirname, '..', '..');
const readmeFile = path.resolve(rootDirectory, 'README.md');
2024-03-24 19:38:18 +02:00
const readmeContent = await fs.readFile(readmeFile, 'utf8');
try {
2024-06-06 14:40:35 +02:00
const match = regexMatcher.exec(readmeContent);
if (match === null) {
console.error(
'Failed to obtain number of SVG icons of current milestone in README:',
'No match found',
);
process.exit(1);
} else {
const overNIconsInReadme = Number.parseInt(match[1], 10);
const iconsData = await getIconsData();
const nIcons = iconsData.length;
const nIconsRounded = Math.floor(nIcons / updateRange) * updateRange;
2024-06-06 14:40:35 +02:00
if (overNIconsInReadme !== nIconsRounded) {
2024-06-06 14:40:35 +02:00
const newContent = readmeContent.replace(
regexMatcher,
`Over ${nIconsRounded} `,
2024-06-06 14:40:35 +02:00
);
await fs.writeFile(readmeFile, newContent);
}
}
2024-03-24 19:38:18 +02:00
} catch (error) {
console.error(
2024-06-06 14:40:35 +02:00
'Failed to update number of SVG icons of current milestone in README:',
2024-03-24 19:38:18 +02:00
error,
);
process.exit(1);
}