Skip to content

Commit de138a7

Browse files
author
CakmLexi
committed
fix: 初始化脚本错误
1 parent 3d2d9a8 commit de138a7

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
"license": "GPL-3.0-only",
1515
"author": "Karin",
1616
"type": "module",
17-
"imports": {
18-
"#Karin": "./lib/index.js"
19-
},
2017
"main": "./lib/index.js",
2118
"types": "./lib/index.d.ts",
2219
"bin": {

src/core/dir.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ import path from 'path'
22
import { fileURLToPath } from 'url'
33

44
const filename = fileURLToPath(import.meta.url)
5+
/**
6+
* - 获取当前npm包的根目录
7+
*/
58
export const karinDir = path.resolve(filename, '../../../').replace(/\\/g, '/').replace(/\/$/, '')

src/tools/init.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,14 @@ for (const dir of delList) {
2929
}
3030
}
3131

32-
// 判断是否为第一次使用
33-
if (!fs.existsSync('./index.js')) {
34-
// 创建一个index.js文件 以供初次开箱使用,写入默认配置
35-
fs.writeFileSync('./index.js', `import { Bot } from 'node-karin'
36-
console.log(Bot.name + '开始初始化~')
37-
`)
38-
}
39-
4032
// 修改package.json为esm模块
4133
const pack = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
42-
pack.type = 'module'
43-
fs.writeFileSync('./package.json', JSON.stringify(pack, null, 2))
44-
45-
console.log('初始化完成~,请通过 node index 启动程序~')
34+
// 解析包内的package.json文件
35+
const npmPack = JSON.parse(fs.readFileSync(`${karinDir}/package.json`, 'utf-8'))
36+
npmPack.main = './node_modules/node-karin/lib/index.js'
37+
delete npmPack.bin
38+
delete npmPack.types
39+
npmPack.dependencies = pack.dependencies
40+
fs.writeFileSync('./package.json', JSON.stringify(npmPack, null, 2))
41+
42+
console.log('初始化完成~,请通过 【 node . 】 启动程序~')

src/utils/config.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import { Logger } from 'log4js'
23
import { karinDir } from 'karin/core/dir'
34
import { fs, yaml as Yaml, chokidar } from 'karin/modules'
@@ -38,7 +39,7 @@ export const config = new (class Cfg {
3839
'./plugins/karin-plugin-example',
3940
]
4041

41-
list.forEach(path => this.checkPath(path))
42+
list.forEach(path => this.mkdir(path))
4243
if (this.npmCfgDir !== (this._path + '/defSet').replace(/\\/g, '/')) {
4344
const files = fs.readdirSync(this.npmCfgDir).filter(file => file.endsWith('.yaml'))
4445
files.forEach(file => {
@@ -64,21 +65,25 @@ export const config = new (class Cfg {
6465
}
6566

6667
/**
67-
* 检查路径是否存在 不存在则创建
68+
* 递归创建目录
69+
* @param dirname - 要创建的文件夹路径
6870
*/
69-
checkPath (path: string) {
70-
if (!fs.existsSync(path)) fs.mkdirSync(path)
71+
mkdir (dirname: string): boolean {
72+
if (fs.existsSync(dirname)) return true
73+
/** 递归自调用 */
74+
if (this.mkdir(path.dirname(dirname))) fs.mkdirSync(dirname)
75+
return true
7176
}
7277

7378
/**
7479
* 为每一个插件建立对应的文件夹
7580
*/
7681
async dirPath (name: string, plugins: string[]) {
7782
name = `./${name}`
78-
this.checkPath(name)
83+
this.mkdir(name)
7984
for (const plugin of plugins) {
8085
const path = `${name}/${plugin}`
81-
this.checkPath(path)
86+
this.mkdir(path)
8287
}
8388
}
8489

0 commit comments

Comments
 (0)