Skip to content

Commit

Permalink
feat: 添加bin入口和配置文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
waset committed Jan 17, 2025
1 parent bc33891 commit ac3a562
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
]
}
},
"bin": "dist/bin/index.cjs",
"files": [
"dist"
],
Expand Down
24 changes: 24 additions & 0 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

'use strict'

import { Iconify } from '../core'
/**
* 初始化
*/
const handle = new Iconify(undefined)

/**
* 转换图标
*/
handle.toConvert()

/**
* 加载图标
*/
handle.toLoad()

/**
* 生成 Iconify IntelliSense 配置
*/
handle.toIntelliSense()
28 changes: 26 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Convert, Loaders, Optional, Options } from './types'
import { existsSync } from 'node:fs'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { cwd } from 'node:process'
import { OUTPUT } from '../env'
Expand Down Expand Up @@ -41,10 +41,34 @@ export class Iconify {
* @param options Options
*/
constructor(options: Options | undefined) {
this.options = { ...this.defaultOptions, ...options }
// 读取配置文件
const configFile = this.resolveConfigFile()
this.options = { ...this.defaultOptions, ...configFile, ...options }
this.setOptions(this.options)
}

private resolveConfigFile(): Options {
const configFilePath = join(cwd(), 'iconify.config.json')
if (existsSync(configFilePath)) {
try {
const configContent = readFileSync(configFilePath, 'utf-8')
const configModule = JSON.parse(configContent)
// 假设导出的是一个对象
if (typeof configModule === 'object') {
return configModule
}
else {
throw new TypeError('The imported config is not an object')
}
}
catch (error) {
console.error(`Error importing ${configFilePath}:`, error)
throw error
}
}
return {} as Options
}

/**
* 设置配置
* @param options Options
Expand Down

0 comments on commit ac3a562

Please sign in to comment.