From ac3a562b5a16f25b81f1dbcba7b48680c507f861 Mon Sep 17 00:00:00 2001 From: waset Date: Fri, 17 Jan 2025 17:21:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0bin=E5=85=A5?= =?UTF-8?q?=E5=8F=A3=E5=92=8C=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/bin/index.ts | 24 ++++++++++++++++++++++++ src/core/index.ts | 28 ++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/bin/index.ts 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..bff3db6 --- /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(undefined) + +/** + * 转换图标 + */ +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 From 22012e8e129b1c68609018ba576b059f62d6272a Mon Sep 17 00:00:00 2001 From: waset Date: Fri, 17 Jan 2025 17:58:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=88=9D=E5=A7=8B=E5=8C=96Iconify?= =?UTF-8?q?=E6=97=B6=E6=94=AF=E6=8C=81=E7=A9=BA=E9=85=8D=E7=BD=AE=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/index.ts b/src/bin/index.ts index bff3db6..3461809 100644 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -6,7 +6,7 @@ import { Iconify } from '../core' /** * 初始化 */ -const handle = new Iconify(undefined) +const handle = new Iconify({}) /** * 转换图标