Skip to content

Commit

Permalink
fix: plugin load error
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed Jul 16, 2023
1 parent 2b6392a commit 052151e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ ctx.command('plugin/plugin.list')
}).join('\n')+`\n\n■:已启用 □:未启用`
} else {
const packages = await ctx.zhin.getMarketPackages()
const plugins = ctx.zhin.getInstalledModules('plugin','adapter','service')
const plugins = ctx.zhin.getInstalledModules('plugin')
const adapters = ctx.zhin.getInstalledModules('adapter')
const services = ctx.zhin.getInstalledModules('service')
const localPlugins = [...plugins, ...adapters, ...services]
return packages.map((o, idx) => {
const installStatus = plugins.some(p => p['fullName'] === o.name)
const installVersion = plugins.find(p => p['fullName'] === o.name)?.['version']
const installStatus = localPlugins.some(p => p['fullName'] === o.name)
const installVersion = localPlugins.find(p => p['fullName'] === o.name)?.['version']
const isLatest = installVersion === o.version
return `${installStatus ? '●' : '○'} ${o.name}@${o.version}${isLatest ? ' (latest)' : installStatus?`(${installVersion})`:''}`
}).join('\n')+`\n\n●:已安装 ○:未安装`
Expand Down
42 changes: 19 additions & 23 deletions src/zhin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class Zhin extends Context {
}

// 扫描项目依赖中的已安装的模块
getInstalledModules<T extends Zhin.ModuleCategory>(...categories: T[]): Zhin.Modules[T][] {
getInstalledModules<T extends Zhin.ModuleCategory>(category: T): Zhin.Modules[T][] {
const result: Zhin.Modules[T][] = []
const loadManifest = (packageName) => {
const filename = require.resolve(packageName + '/package.json')
Expand All @@ -248,7 +248,7 @@ export class Zhin extends Context {
fullName: data.name,
}
}
const loadPackage = (name, category) => {
const loadPackage = (name) => {
try {
result.push(this.load(parsePackage(name).fullName, category))
} catch (e) {
Expand All @@ -267,17 +267,15 @@ export class Zhin extends Context {
const files = fs.existsSync(base) ? fs.readdirSync(base) : []
for (const name of files) {
const base2 = base + '/' + name
for(const category of categories){
if (name === '@zhinjs') {
const files = fs.readdirSync(base2)
for (const name2 of files) {
if (name2.startsWith(`${category}-`)) {
loadPackage(name + '/' + name2, category)
}
if (name === '@zhinjs') {
const files = fs.readdirSync(base2)
for (const name2 of files) {
if (name2.startsWith(`${category}-`)) {
loadPackage(name + '/' + name2)
}
} else if (name.startsWith(`zhin-${category}-`)) {
loadPackage(name,category)
}
} else if (name.startsWith(`zhin-${category}-`)) {
loadPackage(name)
}
}
if (path.dirname(baseDir) !== baseDir) {
Expand All @@ -286,18 +284,16 @@ export class Zhin extends Context {
}
const startDir = path.dirname(__dirname)
loadDirectory(startDir)
for(const category of categories){
if (fs.existsSync(path.resolve(process.cwd(), this.options.plugin_dir))) {
const dirs = fs.readdirSync(path.resolve(process.cwd(), this.options.plugin_dir))
result.push(
...dirs.map((name) => this.load(name.replace(/\.(d\.)?[d|j]s$/, ''), category))
)
}
if (fs.existsSync(path.resolve(__dirname, `${category}s`))) {
const dirs = fs.readdirSync(path.resolve(__dirname, `${category}s`))
result.push(
...dirs.map((name) => this.load(name.replace(/\.(d\.)?[d|j]s$/, ''), category, true)))
}
if (fs.existsSync(path.resolve(process.cwd(), this.options.plugin_dir))) {
const dirs = fs.readdirSync(path.resolve(process.cwd(), this.options.plugin_dir))
result.push(
...dirs.map((name) => this.load(name.replace(/\.(d\.)?[d|j]s$/, ''), category))
)
}
if (fs.existsSync(path.resolve(__dirname, `${category}s`))) {
const dirs = fs.readdirSync(path.resolve(__dirname, `${category}s`))
result.push(
...dirs.map((name) => this.load(name.replace(/\.(d\.)?[d|j]s$/, ''), category, true)))
}
return result
}
Expand Down

0 comments on commit 052151e

Please sign in to comment.