Skip to content

Commit

Permalink
refa: move browser loader to @koishijs/play
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 13, 2023
1 parent 83c50f4 commit a68d2a8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 209 deletions.
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"dependencies": {
"@koishijs/core": "4.11.6",
"@koishijs/loader": "2.5.6",
"@koishijs/loader": "2.6.1",
"@koishijs/utils": "^7.0.0",
"@satorijs/satori": "^2.1.2",
"cac": "^6.7.14",
Expand Down
17 changes: 3 additions & 14 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
{
"name": "@koishijs/loader",
"description": "Config Loader for Koishi",
"version": "2.5.6",
"main": "lib/node.js",
"version": "2.6.1",
"main": "lib/index.js",
"module": "lib/shared.mjs",
"typings": "lib/index.d.ts",
"exports": {
".": {
"node": "./lib/node.js",
"browser": "./lib/browser.mjs"
},
"./shared": {
"require": "./lib/shared.js",
"import": "./lib/shared.mjs"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
},
"files": [
"lib"
],
Expand Down
62 changes: 0 additions & 62 deletions packages/loader/src/browser.ts

This file was deleted.

131 changes: 127 additions & 4 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,128 @@
// placeholder file, do not modify
import Loader from './node'
import { accessSync, constants, readdirSync, readFileSync, statSync, writeFileSync } from 'fs'
import { dirname, extname, resolve } from 'path'
import { Context, Logger } from '@koishijs/core'
import { Loader, unwrapExports } from './shared'
import * as dotenv from 'dotenv'
import * as yaml from 'js-yaml'
import ns from 'ns-require'

export default Loader
export * from './node'
export * from './shared'

const logger = new Logger('app')

const writableExts = ['.json', '.yml', '.yaml']
const supportedExts = ['.js', '.json', '.ts', '.coffee', '.yaml', '.yml']

export default class NodeLoader extends Loader {
public envData = JSON.parse(process.env.KOISHI_SHARED || '{}')
public baseDir = process.cwd()
public extname: string
public scope: ns.Scope
public ctxData = {
env: process.env,
}

constructor(filename?: string) {
super()
if (filename) {
filename = resolve(this.baseDir, filename)
const stats = statSync(filename)
if (stats.isFile()) {
this.filename = filename
this.baseDir = dirname(filename)
this.extname = extname(filename)
if (!supportedExts.includes(this.extname)) {
throw new Error('extension not supported')
}
} else {
this.baseDir = filename
this.findConfig()
}
} else {
this.findConfig()
}
this.writable = this.checkWritable()
this.envfile = resolve(this.baseDir, '.env')
this.scope = ns({
namespace: 'koishi',
prefix: 'plugin',
official: 'koishijs',
dirname: this.baseDir,
})
}

private checkWritable() {
if (!writableExts.includes(this.extname)) return false
try {
accessSync(this.filename, constants.W_OK)
return true
} catch {
return false
}
}

private findConfig() {
const files = readdirSync(this.baseDir)
for (const basename of ['koishi.config', 'koishi']) {
for (const extname of supportedExts) {
if (files.includes(basename + extname)) {
this.extname = extname
this.filename = resolve(this.baseDir, basename + extname)
return
}
}
}
throw new Error('config file not found')
}

readConfig() {
// load .env file into process.env
dotenv.config({ path: this.envfile })

if (['.yaml', '.yml'].includes(this.extname)) {
this.config = yaml.load(readFileSync(this.filename, 'utf8')) as any
} else if (['.json'].includes(this.extname)) {
// we do not use require here because it will pollute require.cache
this.config = JSON.parse(readFileSync(this.filename, 'utf8')) as any
} else {
const module = require(this.filename)
this.config = module.default || module
}

return new Context.Config(this.interpolate(this.config))
}

writeConfig() {
this.app.emit('config')
this.suspend = true
if (!this.writable) throw new Error('cannot overwrite readonly config')
if (this.extname === '.json') {
writeFileSync(this.filename, JSON.stringify(this.config, null, 2))
} else {
writeFileSync(this.filename, yaml.dump(this.config))
}
}

async resolve(name: string) {
return this.scope.resolve(name)
}

async resolvePlugin(name: string) {
try {
this.cache[name] ||= this.scope.resolve(name)
} catch (err) {
logger.error(err.message)
return
}
return unwrapExports(require(this.cache[name]))
}

fullReload(code = Loader.exitCode) {
const body = JSON.stringify(this.envData)
process.send({ type: 'shared', body }, (err: any) => {
if (err) logger.error('failed to send shared data')
logger.info('trigger full reload')
process.exit(code)
})
}
}
128 changes: 0 additions & 128 deletions packages/loader/src/node.ts

This file was deleted.

0 comments on commit a68d2a8

Please sign in to comment.