diff --git a/package.json b/package.json index 6b4ba7a..bf9a1e5 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ ] } }, + "bin": "dist/bin/index.cjs", "files": [ "dist" ], diff --git a/src/bin/index.ts b/src/bin/index.ts new file mode 100644 index 0000000..3461809 --- /dev/null +++ b/src/bin/index.ts @@ -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() diff --git a/src/core/index.ts b/src/core/index.ts index 8d72c8a..0d59142 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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' @@ -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