Skip to content

Commit

Permalink
fix(manager): package provider should not affect dep tree
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 15, 2022
1 parent 152a23c commit b723a3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/addons/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function loadDependencies(filename: string, ignored: Set<string>) {
return dependencies
}

const logger = new Logger('app:watcher')
const logger = new Logger('watch')

export default class FileWatcher extends Service {
private root: string
Expand Down
19 changes: 16 additions & 3 deletions plugins/frontend/manager/src/packages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, App, Context, Dict, omit, pick, Plugin, Schema } from 'koishi'
import { Adapter, App, Context, Dict, omit, pick, Plugin, remove, Schema } from 'koishi'
import { DataSource } from '@koishijs/plugin-console'
import { promises as fsp } from 'fs'
import { dirname } from 'path'
Expand All @@ -11,6 +11,19 @@ function unwrap(module: any) {
return module.default || module
}

/** require without affecting the dependency tree */
function getExports(id: string) {
const path = require.resolve(id)
let result = require.cache[path]
if (!result) {
require(path)
result = require.cache[path]
remove(module.children, result)
delete require.cache[path]
}
return unwrap(result.exports)
}

export class PackageProvider extends DataSource<Dict<PackageProvider.Data>> {
cache: Dict<Promise<PackageProvider.Data>> = {}
task: Promise<void>
Expand Down Expand Up @@ -101,12 +114,12 @@ export class PackageProvider extends DataSource<Dict<PackageProvider.Data>> {
const result = pick(data, ['name', 'version', 'description']) as PackageProvider.Data

// workspace packages are followed by symlinks
result.workspace = !require.resolve(path).includes('node_modules')
result.workspace = !require.resolve(name).includes('node_modules')
result.shortname = data.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')

// check adapter
const oldLength = Object.keys(Adapter.library).length
const exports = unwrap(require(name))
const exports = getExports(name)
const newLength = Object.keys(Adapter.library).length
if (newLength > oldLength) this.ctx.console.services.protocols.broadcast()

Expand Down

0 comments on commit b723a3d

Please sign in to comment.