Skip to content

Commit

Permalink
feat: build web.json for web using
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Oct 20, 2022
1 parent 5676840 commit 5b22814
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
18 changes: 4 additions & 14 deletions packages/solid-iconify/build/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import path from "path"
import { DIST_PATH, log } from "./constants"
import { fileTypes } from "./file-types"
import { CollectionInfo } from "./types"
import { convertCollectionName, getFileByPath } from "./utils"

const getIconName = (cName: string, iName: string) => {
return `${cName}-${iName}`.replace(/-(\w)/g, (all, letter) => {
return letter.toUpperCase()
})
}
import { convertCollectionName, getFileByPath, getIconName } from "./utils"

function writeEachPack(
cName: string,
Expand All @@ -21,17 +15,13 @@ function writeEachPack(
const packFolder = `${DIST_PATH}/${cName}`

fs.mkdirSync(packFolder)

const icons: IconifyJSON = JSON.parse(
getFileByPath(path.resolve(`node_modules/@iconify/json/json/${cName}.json`))
)
for (let index = 0; index < fileTypes.length; index++) {
const type = fileTypes[index]
const fileName = `${packFolder}/${type.fileName}`

fs.appendFileSync(fileName, type.header)
const icons: IconifyJSON = JSON.parse(
getFileByPath(
path.resolve(`node_modules/@iconify/json/json/${cName}.json`)
)
)

Object.entries(icons.icons).forEach(([iName, icon]) => {
fs.appendFileSync(
Expand Down
13 changes: 9 additions & 4 deletions packages/solid-iconify/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { buildCollections } from "./collections"
import { Collections } from "./types"
import { getFileByPath } from "./utils"
import { DIST_PATH, supportedArgs } from "./constants"
import { buildWeb } from "./web"

function getArgs() {
const args = arg({
Expand All @@ -15,12 +16,12 @@ function getArgs() {

return {
isIsolate: args[supportedArgs.ISOLATE],
buildWeb: args[supportedArgs.WEB],
isBuildWeb: args[supportedArgs.WEB],
}
}

const main = async () => {
const { isIsolate, buildWeb } = getArgs()
const { isIsolate, isBuildWeb } = getArgs()

fs.rmSync(DIST_PATH, { recursive: true, force: true })
const collectionsObj: Collections = JSON.parse(
Expand All @@ -29,8 +30,12 @@ const main = async () => {
const collections = Object.entries(collectionsObj).filter(([name, _]) => {
return typeof isIsolate === "string" ? name.includes(isIsolate) : true
})
await buildAssets(collections)
buildCollections(collections)
if (isBuildWeb) {
buildWeb(collections)
} else {
await buildAssets(collections)
buildCollections(collections)
}
}

main()
4 changes: 4 additions & 0 deletions packages/solid-iconify/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export type CollectionInfo = IconifyInfo
export type Collections = {
[key: string]: CollectionInfo
}

export interface Collection extends IconifyInfo {
icons: string[]
}
18 changes: 18 additions & 0 deletions packages/solid-iconify/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import fs from "fs"
import path from "path"

export const getFileByPath = (path: string) => fs.readFileSync(path, "utf8")

export const mkdirSync = (dirname: string) => {
if (fs.existsSync(dirname)) {
return true
} else {
if (mkdirSync(path.dirname(dirname))) {
fs.mkdirSync(dirname)
return true
}
}
}

export const firstUpperCase = (str: string) => {
if (!str || str.length === 0) {
return ""
Expand All @@ -24,3 +36,9 @@ export const convertCollectionName = (name: string) => {
// }
return firstUpperCase(ans)
}

export const getIconName = (cName: string, iName: string) => {
return `${cName}-${iName}`.replace(/-(\w)/g, (all, letter) => {
return letter.toUpperCase()
})
}
56 changes: 56 additions & 0 deletions packages/solid-iconify/build/web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IconifyJSON } from "@iconify/types"
import chalk from "chalk"
import path from "path"
import fs from "fs"
import { DIST_PATH, log } from "./constants"
import { CollectionInfo, Collection } from "./types"
import {
convertCollectionName,
getFileByPath,
getIconName,
mkdirSync,
} from "./utils"

const buildPack = (
cName: string,
convertedName: string,
collection: CollectionInfo
): Collection => {
const icons: IconifyJSON = JSON.parse(
getFileByPath(path.resolve(`node_modules/@iconify/json/json/${cName}.json`))
)
const iconNames = Object.entries(icons.icons).map(([iName]) => {
return getIconName(convertedName, iName)
})
const aliases = Object.entries(icons.aliases ?? {}).map(([iName, alias]) => {
return getIconName(convertedName, iName)
})
return {
...collection,
icons: [...iconNames, ...aliases],
}
}

export const buildWeb = (collectionInfos: [string, CollectionInfo][]) => {
mkdirSync(DIST_PATH)
const dist_file = `${DIST_PATH}/web.json`
const collections: Collection[] = []

const map = new Map<string, string>()
for (const [cName, collectionInfo] of collectionInfos) {
const convertedName = convertCollectionName(cName)
if (map.has(convertedName)) {
log(
chalk.white(`❗ ${cName}`) +
chalk.red(" is duplicated with") +
chalk.white(` ${map.get(convertedName)}`) +
chalk.yellow(" !")
)
} else {
map.set(convertedName, cName)
}
const collection = buildPack(cName, convertedName, collectionInfo)
collections.push(collection)
}
fs.appendFileSync(dist_file, JSON.stringify(collections, null, 2))
}

0 comments on commit 5b22814

Please sign in to comment.