From afd656872537b6c47241c70191ae0e4824ef0c88 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Thu, 17 Oct 2024 00:11:55 +0800 Subject: [PATCH] Fix SPDX license usages in `add-icon-data` script --- scripts/add-icon-data.js | 9 +++------ scripts/lint/ourlint.js | 17 ++--------------- scripts/utils.js | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/scripts/add-icon-data.js b/scripts/add-icon-data.js index 91c05890..38138481 100644 --- a/scripts/add-icon-data.js +++ b/scripts/add-icon-data.js @@ -21,7 +21,7 @@ import { titleToSlug, urlRegex, } from '../sdk.mjs'; -import {getJsonSchemaData, writeIconsData} from './utils.js'; +import {getJsonSchemaData, getSpdxLicenseIds, writeIconsData} from './utils.js'; /** @type {{icons: import('../sdk.js').IconData[]}} */ const iconsData = JSON.parse(await getIconsDataString()); @@ -34,11 +34,8 @@ const aliasTypes = ['aka', 'old'].map((key) => ({ value: key, })); -/** @type {{name: string, value: string}[]} */ -const licenseTypes = - jsonSchema.definitions.brand.properties.license.oneOf[0].properties.type.enum.map( - (/** @type {string} */ license) => ({name: license, value: license}), - ); +const spdxLicenseIds = await getSpdxLicenseIds(); +const licenseTypes = spdxLicenseIds.map((id) => ({name: id, value: id})); /** * Whether an input is a valid URL. diff --git a/scripts/lint/ourlint.js b/scripts/lint/ourlint.js index 43aec487..f43078ac 100644 --- a/scripts/lint/ourlint.js +++ b/scripts/lint/ourlint.js @@ -10,17 +10,16 @@ * @typedef {IconData[]} IconsData */ -import fs from 'node:fs/promises'; import path from 'node:path'; import process from 'node:process'; import fakeDiff from 'fake-diff'; import { collator, - getDirnameFromImportMeta, getIconsDataString, normalizeNewlines, titleToSlug, } from '../../sdk.mjs'; +import {getSpdxLicenseIds} from '../utils.js'; /** * Contains our tests so they can be isolated from each other. @@ -208,19 +207,7 @@ const TESTS = { /* Check if all licenses are valid SPDX identifiers */ async checkLicense(data) { - const spdxLicenseIds = new Set( - JSON.parse( - await fs.readFile( - path.join( - getDirnameFromImportMeta(import.meta.url), - '..', - '..', - 'node_modules/spdx-license-ids/index.json', - ), - 'utf8', - ), - ), - ); + const spdxLicenseIds = new Set(await getSpdxLicenseIds()); const badLicenses = []; for (const {title, slug, license} of data.icons) { if ( diff --git a/scripts/utils.js b/scripts/utils.js index 2ee6b2f7..686bed82 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -45,3 +45,21 @@ export const writeIconsData = async ( 'utf8', ); }; + +/** + * Get SPDX license IDs from `spdx-license-ids` package. + * @param {string} rootDirectory Path to the root directory of the project. + * @returns {Promise} 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); +};