Skip to content

Commit

Permalink
fix: allow to install icon packages individually
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Feb 16, 2023
1 parent 4d74c59 commit c8dd82b
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@ import path from "path"
import fs from "fs"
import { IconifyJSON } from "@iconify/types"
import { getIconCSS, getIconData } from "@iconify/utils"
import { createRequire } from "module"
import { CollectionNames } from "../types"

declare const TSUP_FORMAT: "esm" | "cjs"
const req =
typeof TSUP_FORMAT === "undefined" || TSUP_FORMAT === "cjs"
? require
: createRequire(import.meta.url)

const localResolve = (cwd: string, id: string) => {
const resolved = require.resolve(id, { paths: [cwd] })
const resolved = req.resolve(id, { paths: [cwd] })
return resolved
}

export const getIconCollections = (
include: CollectionNames[] | "all" = "all",
) => {
const pkgPath = localResolve(process.cwd(), "@iconify/json/package.json")
if (!pkgPath) return {}
if (!pkgPath) {
if (Array.isArray(include)) {
return include.reduce((result, name) => {
const jsonPath = localResolve(
process.cwd(),
`@iconify-json/${name}/icons.json`,
)
if (!jsonPath) {
throw new Error(
`Icon collection "${name}" not found. Please install @iconify-json/${name} or @iconify/json`,
)
}
return {
...result,
[name]: req(jsonPath),
}
}, {})
}
return {}
}
const pkgDir = path.dirname(pkgPath)
const files = fs.readdirSync(path.join(pkgDir, "json"))
const collections: Record<string, IconifyJSON> = {}
Expand All @@ -22,7 +48,7 @@ export const getIconCollections = (
include === "all" ||
include.includes(file.replace(".json", "") as any)
) {
const json: IconifyJSON = require(path.join(pkgDir, "json", file))
const json: IconifyJSON = req(path.join(pkgDir, "json", file))
collections[json.prefix] = json
}
}
Expand Down

0 comments on commit c8dd82b

Please sign in to comment.