Autogenerate SDK TypeScript definitions on releases (#9186)

This commit is contained in:
Álvaro Mondéjar 2023-08-01 12:09:47 -06:00 committed by GitHub
parent 32c1611c8e
commit 3588f61183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 9 deletions

View File

@ -64,6 +64,8 @@ jobs:
run: node ./scripts/release/update-svgs-count.js
- name: Update slugs table
run: node ./scripts/release/update-slugs-table.js
- name: Update SDK Typescript definitions
run: node ./scripts/release/update-sdk-ts-defs.js
- name: Commit version bump
uses: stefanzweifel/git-auto-commit-action@v4.16.0
with:

View File

@ -48,10 +48,12 @@ jobs:
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Replace CDN theme image links from README
run: npm run strip-theme-link
- name: Install dependencies
run: npm i
- name: Replace CDN theme image links from README
run: npm run strip-theme-link
- name: Update SDK Typescript definitions
run: node ./scripts/release/update-sdk-ts-defs.js
- name: Build NodeJS package
run: npm run build
- name: Deploy to NPM
@ -80,7 +82,7 @@ jobs:
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
# Commit that will only be included in the tag
- name: Commit dark theme images strip
- name: Commit CDN theme image links removal
run: |
git add README.md
git commit -m 'Replace README CDN theme image links'

View File

@ -0,0 +1,69 @@
/**
* @fileoverview
* Updates the SDK Typescript definitions located in the file sdk.d.ts
* to match the current definitions of functions of sdk.mjs.
*/
import fsSync from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { execSync } from 'node:child_process';
import { getDirnameFromImportMeta } from '../../sdk.mjs';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const rootDir = path.resolve(__dirname, '..', '..');
const sdkTs = path.resolve(rootDir, 'sdk.d.ts');
const sdkMts = path.resolve(rootDir, 'sdk.d.mts');
const sdkMjs = path.resolve(rootDir, 'sdk.mjs');
const generateSdkMts = async () => {
// remove temporally type definitions imported with comments
// in sdk.mjs to avoid circular imports
const originalSdkMjsContent = await fs.readFile(sdkMjs, 'utf-8');
const tempSdkMjsContent = originalSdkMjsContent
.split('\n')
.filter((line) => {
return !line.startsWith(' * @typedef {import("./sdk")');
})
.join('\n');
await fs.writeFile(sdkMjs, tempSdkMjsContent);
try {
execSync(
'npx tsc sdk.mjs' +
' --declaration --emitDeclarationOnly --allowJs --removeComments',
);
} catch (error) {
console.log(
`Error ${error.status} generating Typescript` +
` definitions: '${error.message}'`,
);
process.exit(1);
}
await fs.writeFile(sdkMjs, originalSdkMjsContent);
};
const generateSdkTs = async () => {
fsSync.existsSync(sdkMts) && (await fs.unlink(sdkMts));
await generateSdkMts();
const autogeneratedMsg = '/* The next code is autogenerated from sdk.mjs */';
const newSdkTsContent =
(await fs.readFile(sdkTs, 'utf-8')).split(autogeneratedMsg)[0] +
`${autogeneratedMsg}\n\n${await fs.readFile(sdkMts, 'utf-8')}`;
await fs.writeFile(sdkTs, newSdkTsContent);
await fs.unlink(sdkMts);
try {
execSync('npx prettier -w sdk.d.ts');
} catch (error) {
console.log(
`Error ${error.status} executing Prettier` +
` to pretiffy SDK TS definitions: '${error.message}'`,
);
process.exit(1);
}
};
await generateSdkTs();

5
sdk.d.ts vendored
View File

@ -3,7 +3,7 @@
* Types for Simple Icons SDK.
*/
import type { License } from './types.d.ts';
import type { License } from './types';
/**
* The data for a third-party extension.
@ -59,8 +59,9 @@ export type IconData = {
aliases?: Aliases;
};
export const URL_REGEX: RegExp;
/* The next code is autogenerated from sdk.mjs */
export const URL_REGEX: RegExp;
export function getDirnameFromImportMeta(importMetaUrl: string): string;
export function getIconSlug(icon: IconData): string;
export function svgToPath(svg: string): string;

View File

@ -120,7 +120,7 @@ export const htmlFriendlyToTitle = (htmlFriendlyTitle) =>
/**
* Get path of *_data/simpe-icons.json*.
* @param {String|undefined} rootDir Path to the root directory of the project
* @param {String} rootDir Path to the root directory of the project
* @returns {String} Path of *_data/simple-icons.json*
*/
export const getIconDataPath = (
@ -131,7 +131,7 @@ export const getIconDataPath = (
/**
* Get contents of *_data/simple-icons.json*.
* @param {String|undefined} rootDir Path to the root directory of the project
* @param {String} rootDir Path to the root directory of the project
* @returns {String} Content of *_data/simple-icons.json*
*/
export const getIconsDataString = (
@ -142,7 +142,7 @@ export const getIconsDataString = (
/**
* Get icons data as object from *_data/simple-icons.json*.
* @param {String|undefined} rootDir Path to the root directory of the project
* @param {String} rootDir Path to the root directory of the project
* @returns {IconData[]} Icons data as array from *_data/simple-icons.json*
*/
export const getIconsData = async (
@ -178,7 +178,7 @@ export const normalizeColor = (text) => {
/**
* Get information about third party extensions from the README table.
* @param {String|undefined} readmePath Path to the README file
* @param {String} readmePath Path to the README file
* @returns {Promise<ThirdPartyExtension[]>} Information about third party extensions
*/
export const getThirdPartyExtensions = async (