Skip to content

Commit

Permalink
fix: 初始化脚本错误
Browse files Browse the repository at this point in the history
  • Loading branch information
CakmLexi committed Jul 2, 2024
1 parent 3d2d9a8 commit de138a7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions src/core/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\/$/, '')
21 changes: 9 additions & 12 deletions src/tools/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 . 】 启动程序~')
17 changes: 11 additions & 6 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 => {
Expand All @@ -64,21 +65,25 @@ 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
}

/**
* 为每一个插件建立对应的文件夹
*/
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)
}
}

Expand Down

0 comments on commit de138a7

Please sign in to comment.