From de138a723a04c2431517634f5f11f58527553a6b Mon Sep 17 00:00:00 2001 From: CakmLexi Date: Tue, 2 Jul 2024 13:38:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9D=E5=A7=8B=E5=8C=96=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 --- src/core/dir.ts | 3 +++ src/tools/init.ts | 21 +++++++++------------ src/utils/config.ts | 17 +++++++++++------ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 5f96265..83e1c2b 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,6 @@ "license": "GPL-3.0-only", "author": "Karin", "type": "module", - "imports": { - "#Karin": "./lib/index.js" - }, "main": "./lib/index.js", "types": "./lib/index.d.ts", "bin": { diff --git a/src/core/dir.ts b/src/core/dir.ts index 9a2c440..ad9b44b 100644 --- a/src/core/dir.ts +++ b/src/core/dir.ts @@ -2,4 +2,7 @@ import path from 'path' import { fileURLToPath } from 'url' const filename = fileURLToPath(import.meta.url) +/** + * - 获取当前npm包的根目录 + */ export const karinDir = path.resolve(filename, '../../../').replace(/\\/g, '/').replace(/\/$/, '') diff --git a/src/tools/init.ts b/src/tools/init.ts index c359b98..cca0994 100644 --- a/src/tools/init.ts +++ b/src/tools/init.ts @@ -29,17 +29,14 @@ for (const dir of delList) { } } -// 判断是否为第一次使用 -if (!fs.existsSync('./index.js')) { - // 创建一个index.js文件 以供初次开箱使用,写入默认配置 - fs.writeFileSync('./index.js', `import { Bot } from 'node-karin' -console.log(Bot.name + '开始初始化~') -`) -} - // 修改package.json为esm模块 const pack = JSON.parse(fs.readFileSync('./package.json', 'utf-8')) -pack.type = 'module' -fs.writeFileSync('./package.json', JSON.stringify(pack, null, 2)) - -console.log('初始化完成~,请通过 node index 启动程序~') +// 解析包内的package.json文件 +const npmPack = JSON.parse(fs.readFileSync(`${karinDir}/package.json`, 'utf-8')) +npmPack.main = './node_modules/node-karin/lib/index.js' +delete npmPack.bin +delete npmPack.types +npmPack.dependencies = pack.dependencies +fs.writeFileSync('./package.json', JSON.stringify(npmPack, null, 2)) + +console.log('初始化完成~,请通过 【 node . 】 启动程序~') diff --git a/src/utils/config.ts b/src/utils/config.ts index 8fbe219..202c6c2 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -1,3 +1,4 @@ +import path from 'path' import { Logger } from 'log4js' import { karinDir } from 'karin/core/dir' import { fs, yaml as Yaml, chokidar } from 'karin/modules' @@ -38,7 +39,7 @@ export const config = new (class Cfg { './plugins/karin-plugin-example', ] - list.forEach(path => this.checkPath(path)) + list.forEach(path => this.mkdir(path)) if (this.npmCfgDir !== (this._path + '/defSet').replace(/\\/g, '/')) { const files = fs.readdirSync(this.npmCfgDir).filter(file => file.endsWith('.yaml')) files.forEach(file => { @@ -64,10 +65,14 @@ export const config = new (class Cfg { } /** - * 检查路径是否存在 不存在则创建 + * 递归创建目录 + * @param dirname - 要创建的文件夹路径 */ - checkPath (path: string) { - if (!fs.existsSync(path)) fs.mkdirSync(path) + mkdir (dirname: string): boolean { + if (fs.existsSync(dirname)) return true + /** 递归自调用 */ + if (this.mkdir(path.dirname(dirname))) fs.mkdirSync(dirname) + return true } /** @@ -75,10 +80,10 @@ export const config = new (class Cfg { */ async dirPath (name: string, plugins: string[]) { name = `./${name}` - this.checkPath(name) + this.mkdir(name) for (const plugin of plugins) { const path = `${name}/${plugin}` - this.checkPath(path) + this.mkdir(path) } }