Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加bin入口和配置文件支持 #51

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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({})

/**
* 转换图标
*/
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
Loading