Skip to content

Commit

Permalink
build(scripts): add more assertions for metadata and assets
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard committed Feb 1, 2024
1 parent a240fbd commit f62dad6
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"chalk": "^4",
"commander": "^9.5.0",
"prettier": "^2.8.3",
"svgson": "^5.3.1",
"tsx": "^4.7.0",
"typescript": "^4.9.4",
"vite": "^5.0.12"
Expand Down
51 changes: 51 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion raw/fill/bus-fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion raw/fill/gear-fine-fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 16 additions & 35 deletions scripts/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
"use strict";

import fs from "node:fs/promises";
import path from "path";
import axios from "axios";
import chalk from "chalk";
import { Command } from "commander";

import { version } from "../package.json";
import { CATEGORY_MAP } from ".";
import { assertValidAssets, assertValidApiResponse } from "./verify";
import {
CATALOG_PATH,
CATEGORY_MAP,
CURRENT_NUMERIC_VERSION,
type IconAPIResponse,
} from ".";

const ICON_API_URL = "https://api.phosphoricons.com";

const [MAJOR_PART, MINOR_PART] = version.split(".");
const CURRENT_NUMERIC_VERSION = +MAJOR_PART + +MINOR_PART / 10;

main().catch(console.error);

async function main() {
(async function main() {
const program = new Command();
program
.version(version)
Expand All @@ -34,8 +34,12 @@ async function main() {
const params = new URLSearchParams(Object.entries(program.opts())).toString();

try {
const res = await axios.get(`${ICON_API_URL}?${params}`);
await assertValidAssets();

const res = await axios.get<IconAPIResponse>(`${ICON_API_URL}?${params}`);
if (res.data) {
assertValidApiResponse(res.data);

res.data.icons.sort((a, b) => (a.name < b.name ? -1 : 1));

let fileString = `\
Expand All @@ -46,30 +50,7 @@ export type PhosphorIcon = typeof icons[number]
export const icons = <const>[
`;

console.log(res.data.icons);
res.data.icons.forEach((icon) => {
if (!icon.codepoint) {
console.error(
`${chalk.inverse.red(" FAIL ")} ${icon.name} missing Codepoint`
);
throw new Error("codepoint");
}

if (!icon.category) {
console.error(
`${chalk.inverse.red(" FAIL ")} ${icon.name} missing Category`
);
throw new Error("category");
}

let figma_category = CATEGORY_MAP[icon.category];
if (!figma_category) {
console.error(
`${chalk.inverse.red(" FAIL ")} Invalid category ${icon.category}`
);
throw new Error("figma_category");
}

let categories = "[";
icon.search_categories?.forEach((c) => {
categories += `IconCategory.${c.toUpperCase()},`;
Expand All @@ -87,7 +68,7 @@ export const icons = <const>[
: ""
}
categories: ${categories},
figma_category: FigmaCategory.${figma_category},
figma_category: FigmaCategory.${CATEGORY_MAP[icon.category]},
tags: ${JSON.stringify([
...(icon.published_in >= CURRENT_NUMERIC_VERSION
? ["*new*"]
Expand All @@ -109,7 +90,7 @@ export const icons = <const>[
`;

try {
await fs.writeFile(path.join(__dirname, "../src/icons.ts"), fileString);
await fs.writeFile(CATALOG_PATH, fileString);
console.log(
`${chalk.green(" DONE ")} ${res.data.icons.length} icons ingested`
);
Expand All @@ -125,7 +106,7 @@ export const icons = <const>[
console.error(e);
process.exit(1);
}
}
})().catch(console.error);

function pascalize(str) {
return str
Expand Down
44 changes: 44 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
"use strict";

import path from "node:path";
import { fileURLToPath } from "node:url";

import { version } from "../package.json";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export const ASSETS_PATH = path.join(__dirname, "../assets");
export const RAW_PATH = path.join(__dirname, "../raw");
export const CATALOG_PATH = path.join(__dirname, "../src/icons.ts");

const [MAJOR_PART, MINOR_PART] = version.split(".");
export const CURRENT_NUMERIC_VERSION = +MAJOR_PART + +MINOR_PART / 10;

export const CATEGORY_MAP = {
Arrows: "ARROWS",
Brands: "BRAND",
Expand All @@ -20,3 +35,32 @@ export const CATEGORY_MAP = {
Time: "TIME",
"Weather & Nature": "WEATHER",
};

export type IconStatus =
| "Backlog"
| "Designing"
| "Designed"
| "Implemented"
| "Deprecated"
| "";

export type IconMetadata = {
name: string;
alias?: string;
rid: string;
category: keyof typeof CATEGORY_MAP;
tags: string[];
search_categories: string[];
status: IconStatus;
codepoint?: number;
published: boolean;
published_in: number;
updated_in: number;
notes?: string;
};

export type IconAPIResponse = {
icons: IconMetadata[];
count: number;
version: number;
};
Loading

0 comments on commit f62dad6

Please sign in to comment.