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-cdn-urls.js

65 lines
1.8 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
* Updates the CDN URLs in the README.md to match the major version in the
* NPM package manifest. Does nothing if the README.md is already up-to-date.
*/
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} from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
2024-03-24 19:38:18 +02:00
const rootDirectory = path.resolve(__dirname, '..', '..');
const packageJsonFile = path.resolve(rootDirectory, 'package.json');
const readmeFile = path.resolve(rootDirectory, 'README.md');
2024-06-06 14:40:35 +02:00
/**
* Get the major version number from a semantic version string.
2024-06-06 14:40:35 +02:00
* @param {string} semVersion A semantic version string.
* @returns {number} The major version number.
*/
2024-03-24 19:38:18 +02:00
const getMajorVersion = (semVersion) => {
const majorVersionAsString = semVersion.split('.')[0];
return Number.parseInt(majorVersionAsString, 10);
};
2024-06-06 14:40:35 +02:00
/**
* Get the package.json manifest.
* @returns {Promise<{version: string}>} The package.json manifest.
*/
const getManifest = async () => {
2024-03-24 19:38:18 +02:00
const manifestRaw = await fs.readFile(packageJsonFile, 'utf8');
return JSON.parse(manifestRaw);
};
2024-06-06 14:40:35 +02:00
/**
* Update the version number in the README.md.
2024-06-06 14:40:35 +02:00
* @param {number} majorVersion The major version number.
*/
const updateVersionInReadmeIfNecessary = async (majorVersion) => {
let content = await fs.readFile(readmeFile, 'utf8');
2024-03-24 19:38:18 +02:00
content = content.replaceAll(
/simple-icons@v\d+/g,
`simple-icons@v${majorVersion}`,
);
await fs.writeFile(readmeFile, content);
};
const main = async () => {
try {
const manifest = await getManifest();
const majorVersion = getMajorVersion(manifest.version);
await updateVersionInReadmeIfNecessary(majorVersion);
} catch (error) {
console.error('Failed to update CDN version number:', error);
process.exit(1);
}
};
await main();