2024-06-06 14:40:35 +02:00
|
|
|
/**
|
|
|
|
* @file Internal utilities.
|
|
|
|
*
|
|
|
|
* Here resides all the functionality that does not qualifies to reside
|
|
|
|
* in the SDK because is not publicly exposed.
|
|
|
|
*/
|
|
|
|
|
2022-09-25 03:04:58 +02:00
|
|
|
import fs from 'node:fs/promises';
|
2024-03-24 19:38:18 +02:00
|
|
|
import path from 'node:path';
|
|
|
|
import {getDirnameFromImportMeta, getIconDataPath} from '../sdk.mjs';
|
2022-09-28 04:11:27 +02:00
|
|
|
|
|
|
|
const __dirname = getDirnameFromImportMeta(import.meta.url);
|
|
|
|
|
2024-06-06 14:40:35 +02:00
|
|
|
/**
|
|
|
|
* @typedef {import("../sdk.js").IconData} IconData
|
|
|
|
*/
|
|
|
|
|
2022-09-28 04:11:27 +02:00
|
|
|
/**
|
|
|
|
* Get JSON schema data.
|
2024-06-06 14:40:35 +02:00
|
|
|
* @param {string} rootDirectory Path to the root directory of the project.
|
|
|
|
* @returns {Promise<any>} JSON schema data.
|
2022-09-28 04:11:27 +02:00
|
|
|
*/
|
|
|
|
export const getJsonSchemaData = async (
|
2024-03-24 19:38:18 +02:00
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
2022-09-28 04:11:27 +02:00
|
|
|
) => {
|
2024-03-24 19:38:18 +02:00
|
|
|
const jsonSchemaPath = path.resolve(rootDirectory, '.jsonschema.json');
|
2022-09-28 04:11:27 +02:00
|
|
|
const jsonSchemaString = await fs.readFile(jsonSchemaPath, 'utf8');
|
|
|
|
return JSON.parse(jsonSchemaString);
|
|
|
|
};
|
|
|
|
|
2022-09-25 03:04:58 +02:00
|
|
|
/**
|
|
|
|
* Write icons data to _data/simple-icons.json.
|
2024-06-06 14:40:35 +02:00
|
|
|
* @param {{icons: IconData[]}} iconsData Icons data object.
|
|
|
|
* @param {string} rootDirectory Path to the root directory of the project.
|
2024-10-01 05:52:48 +02:00
|
|
|
* @param {boolean} minify Whether to minify the JSON output.
|
2022-09-25 03:04:58 +02:00
|
|
|
*/
|
2023-04-19 15:23:13 +02:00
|
|
|
export const writeIconsData = async (
|
|
|
|
iconsData,
|
2024-03-24 19:38:18 +02:00
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
2024-10-01 05:52:48 +02:00
|
|
|
minify,
|
2023-04-19 15:23:13 +02:00
|
|
|
) => {
|
2023-08-08 06:38:52 +02:00
|
|
|
await fs.writeFile(
|
2024-03-24 19:38:18 +02:00
|
|
|
getIconDataPath(rootDirectory),
|
2024-10-01 05:52:48 +02:00
|
|
|
`${JSON.stringify(iconsData, null, minify ? 0 : 4)}\n`,
|
2022-09-25 03:04:58 +02:00
|
|
|
'utf8',
|
|
|
|
);
|
|
|
|
};
|
2024-10-16 18:11:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get SPDX license IDs from `spdx-license-ids` package.
|
|
|
|
* @param {string} rootDirectory Path to the root directory of the project.
|
|
|
|
* @returns {Promise<string[]>} Set of SPDX license IDs.
|
|
|
|
*/
|
|
|
|
export const getSpdxLicenseIds = async (
|
|
|
|
rootDirectory = path.resolve(__dirname, '..'),
|
|
|
|
) => {
|
|
|
|
const getSpdxLicenseJson = path.resolve(
|
|
|
|
rootDirectory,
|
|
|
|
'node_modules',
|
|
|
|
'spdx-license-ids',
|
|
|
|
'index.json',
|
|
|
|
);
|
|
|
|
const getSpdxLicenseString = await fs.readFile(getSpdxLicenseJson, 'utf8');
|
|
|
|
return JSON.parse(getSpdxLicenseString);
|
|
|
|
};
|