bajo 1.1.19 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,10 +15,10 @@ async function bootOrder () {
15
15
  for (let n of this.pluginPkgs) {
16
16
  n = map(n.split(':'), m => trim(m))[0]
17
17
  const dir = n === this.mainNs ? (`${this.dir.base}/${this.mainNs}`) : this.getModuleDir(n)
18
- if (n !== this.mainNs && !fs.existsSync(`${dir}/plugin`)) throw this.error('packageNotFoundOrNotBajo%s', n)
18
+ if (n !== this.mainNs && !fs.existsSync(dir)) throw this.error('packageNotFoundOrNotBajo%s', n)
19
19
  norder[n] = NaN
20
20
  try {
21
- norder[n] = Number(trim(await fs.readFile(`${dir}/plugin/.bootorder`, 'utf8')))
21
+ norder[n] = Number(trim(await fs.readFile(`${dir}/.bootorder`, 'utf8')))
22
22
  } catch (err) {}
23
23
  }
24
24
  const result = []
@@ -4,7 +4,6 @@ import checkClash from '../bajo-plugin/check-clash.js'
4
4
  import attachMethod from '../bajo-plugin/attach-method.js'
5
5
  import collectHooks from '../bajo-plugin/collect-hooks.js'
6
6
  import run from '../bajo-plugin/run.js'
7
- import collectExitHandlers from '../bajo-plugin/collect-exit-handlers.js'
8
7
 
9
8
  async function bootPlugins () {
10
9
  await buildConfig.call(this.app)
@@ -12,7 +11,6 @@ async function bootPlugins () {
12
11
  await checkDependency.call(this.app)
13
12
  await attachMethod.call(this.app)
14
13
  await collectHooks.call(this.app)
15
- await collectExitHandlers.call(this.app)
16
14
  await run.call(this.app)
17
15
  }
18
16
 
@@ -24,12 +24,9 @@ async function buildPlugins () {
24
24
  dir = `${this.dir.base}/${this.mainNs}`
25
25
  fs.ensureDirSync(dir)
26
26
  fs.ensureDirSync(`${dir}/plugin`)
27
- } else {
28
- dir = this.getModuleDir(pkg)
29
- if (!fs.existsSync(`${dir}/plugin`)) throw new Error(`Package '${pkg}' isn't a valid Bajo package`)
30
- }
27
+ } else dir = this.getModuleDir(pkg)
31
28
  let plugin
32
- const factory = `${dir}/plugin/factory.js`
29
+ const factory = `${dir}/index.js`
33
30
  if (fs.existsSync(factory)) {
34
31
  const { default: builder } = await import(resolvePath(factory, true))
35
32
  const FactoryClass = await builder.call(this, pkg)
@@ -2,10 +2,8 @@ async function exit (signal) {
2
2
  const { eachPlugins } = this
3
3
  this.log.warn('signalReceived%s', signal)
4
4
  await eachPlugins(async function ({ ns }) {
5
- const handler = this.exitHandler
6
- if (!handler) return undefined
7
5
  try {
8
- await handler.call(this)
6
+ await this.stop()
9
7
  } catch (err) {}
10
8
  this.log.debug('exited')
11
9
  })
@@ -1,6 +1,5 @@
1
1
  import Plugin from './plugin.js'
2
2
  import BajoPlugin from './bajo-plugin.js'
3
- import dayjs from '../lib/dayjs.js'
4
3
  import increment from 'add-filename-increment'
5
4
  import fs from 'fs-extra'
6
5
  import path from 'path'
@@ -16,8 +15,6 @@ import { customAlphabet } from 'nanoid'
16
15
  import fastGlob from 'fast-glob'
17
16
  import querystring from 'querystring'
18
17
  import deepFreeze from 'deep-freeze-strict'
19
- import { sprintf } from 'sprintf-js'
20
- import outmatch from 'outmatch'
21
18
  import resolvePath from '../lib/resolve-path.js'
22
19
  import importModule from '../lib/import-module.js'
23
20
  import logLevels from '../lib/log-levels.js'
@@ -37,12 +34,6 @@ class BajoCore extends Plugin {
37
34
  super('bajo', app)
38
35
  this.runAt = new Date()
39
36
  this.mainNs = 'main'
40
- this.lib._ = lodash
41
- this.lib.fs = fs
42
- this.lib.fastGlob = fastGlob
43
- this.lib.sprintf = sprintf
44
- this.lib.outmatch = outmatch
45
- this.lib.dayjs = dayjs
46
37
  this.lib.BajoPlugin = BajoPlugin
47
38
  this.applets = []
48
39
  this.pluginPkgs = []
@@ -115,11 +106,31 @@ class BajoCore extends Plugin {
115
106
  }
116
107
  if (!this.app[ns]) throw this.error('unknownPluginOrNotLoaded%s')
117
108
  }
118
- const fullPath = path
119
109
  let qs
120
110
  [path, qs] = path.split('?')
121
111
  qs = querystring.parse(qs) ?? {}
122
- return { ns, path, subNs, subSubNs, qs, fullPath, fullNs }
112
+ // normalize path
113
+ const parts = path.split('/')
114
+ const realParts = []
115
+ const params = {}
116
+ for (const idx in parts) {
117
+ const part = parts[idx]
118
+ if (part[0] !== ':' || part.indexOf('|') === -1) {
119
+ realParts.push(part)
120
+ continue
121
+ }
122
+ const [key, val] = part.split('|')
123
+ parts[idx] = key
124
+ params[key.slice(1)] = val
125
+ realParts.push(val)
126
+ }
127
+ path = parts.join('/')
128
+ const realPath = realParts.join('/')
129
+ let fullPath = path
130
+ if (!isEmpty(qs)) fullPath += ('?' + querystring.stringify(qs))
131
+ let realFullPath = realPath
132
+ if (!isEmpty(qs)) realFullPath += ('?' + querystring.stringify(qs))
133
+ return { ns, path, subNs, subSubNs, qs, fullPath, fullNs, realPath, realFullPath }
123
134
  }
124
135
 
125
136
  buildCollections = async (options = {}) => {
@@ -382,8 +393,7 @@ class BajoCore extends Plugin {
382
393
  }
383
394
 
384
395
  getPluginDataDir = (name, ensureDir = true) => {
385
- const { getPlugin } = this.app.bajo
386
- const plugin = getPlugin(name)
396
+ const plugin = this.getPlugin(name)
387
397
  const dir = `${this.app.bajo.dir.data}/plugins/${plugin.name}`
388
398
  if (ensureDir) fs.ensureDirSync(dir)
389
399
  return dir
@@ -476,7 +486,7 @@ class BajoCore extends Plugin {
476
486
  isValidApp = (dir) => {
477
487
  if (!dir) dir = this.app.dir
478
488
  dir = resolvePath(dir)
479
- const hasMainDir = fs.existsSync(`${dir}/main/plugin`)
489
+ const hasMainDir = fs.existsSync(`${dir}/main`)
480
490
  const hasPackageJson = fs.existsSync(`${dir}/package.json`)
481
491
  return hasMainDir && hasPackageJson
482
492
  }
@@ -484,7 +494,7 @@ class BajoCore extends Plugin {
484
494
  isValidPlugin = (dir) => {
485
495
  if (!dir) dir = this.app.dir
486
496
  dir = resolvePath(dir)
487
- const hasPluginDir = fs.existsSync(`${dir}/plugin`)
497
+ const hasPluginDir = fs.existsSync(dir)
488
498
  const hasPackageJson = fs.existsSync(`${dir}/package.json`)
489
499
  return hasPluginDir && hasPackageJson
490
500
  }
@@ -649,8 +659,7 @@ class BajoCore extends Plugin {
649
659
 
650
660
  saveAsDownload = async (file, obj, printSaved = true) => {
651
661
  const { print, getPluginDataDir } = this.app.bajo
652
- const plugin = this.name
653
- const fname = increment(`${getPluginDataDir(plugin)}/${trim(file, '/')}`, { fs: true })
662
+ const fname = increment(`${getPluginDataDir(this.name)}/${trim(file, '/')}`, { fs: true })
654
663
  const dir = path.dirname(fname)
655
664
  if (!fs.existsSync(dir)) fs.ensureDirSync(dir)
656
665
  await fs.writeFile(fname, obj, 'utf8')
@@ -6,7 +6,7 @@ async function attachMethod () {
6
6
  me.bajo.log.debug('attachMethods')
7
7
  await eachPlugins(async function ({ ns, pkgName }) {
8
8
  const dir = ns === me.bajo.mainNs ? (`${me.bajo.dir.base}/${me.bajo.mainNs}`) : me.bajo.getModuleDir(pkgName)
9
- const num = await createMethod.call(me[ns], `${dir}/plugin/method`, pkgName)
9
+ const num = await createMethod.call(me[ns], `${dir}/plugin-method`, pkgName)
10
10
  me.bajo.log.trace('- %s (%d)', ns, num)
11
11
  })
12
12
  }
@@ -4,11 +4,13 @@ const { camelCase, map } = lodash
4
4
  async function run () {
5
5
  const me = this
6
6
  const { runHook, eachPlugins, join } = me.bajo
7
+ const { freeze } = me.bajo
7
8
  const methods = ['init']
8
9
  if (!me.bajo.applet) methods.push('start')
9
10
  for (const method of methods) {
10
11
  await runHook(`bajo:${camelCase(`before ${method} all plugins`)}`)
11
12
  await eachPlugins(async function ({ ns }) {
13
+ if (method === 'start') freeze(me[ns].config)
12
14
  await runHook(`${ns}:${camelCase(`before ${method}`)}`)
13
15
  await me[ns][method]()
14
16
  await runHook(`${ns}:${camelCase(`after ${method}`)}`)
@@ -2,9 +2,8 @@ import Plugin from './plugin.js'
2
2
  import lodash from 'lodash'
3
3
  import omittedPluginKeys from '../lib/omitted-plugin-keys.js'
4
4
  import readAllConfigs from '../lib/read-all-configs.js'
5
- import fs from 'fs-extra'
6
5
 
7
- const { pick, omit, camelCase, trim, without } = lodash
6
+ const { pick, omit } = lodash
8
7
 
9
8
  class BajoPlugin extends Plugin {
10
9
  constructor (pkgName, app) {
@@ -14,14 +13,12 @@ class BajoPlugin extends Plugin {
14
13
 
15
14
  loadConfig = async () => {
16
15
  const { defaultsDeep } = this.lib.aneka
17
- const { log, getModuleDir, readJson, parseObject } = this.app.bajo
16
+ const { log, readJson, parseObject, getModuleDir } = this.app.bajo
18
17
  log.trace('- %s', this.name)
19
18
  const dir = this.name === this.app.bajo.mainNs ? (`${this.app.bajo.dir.base}/${this.app.bajo.mainNs}`) : getModuleDir(this.pkgName)
20
- let cfg = await readAllConfigs.call(this.app, `${dir}/plugin/config`)
19
+ let cfg = await readAllConfigs.call(this.app, `${dir}/config`)
21
20
  this.alias = this.alias ?? (this.pkgName.slice(0, 5) === 'bajo-' ? this.pkgName.slice(5).toLowerCase() : this.name.toLowerCase())
22
- const aliasFile = `${dir}/plugin/.alias`
23
- if (fs.existsSync(aliasFile)) this.alias = fs.readFileSync(aliasFile, 'utf8')
24
- this.alias = camelCase(this.alias)
21
+ this.alias = this.alias.toLowerCase()
25
22
 
26
23
  this.dir = {
27
24
  pkg: dir,
@@ -45,37 +42,16 @@ class BajoPlugin extends Plugin {
45
42
  this.title = this.title ?? cfg.title ?? this.alias
46
43
 
47
44
  this.dependencies = this.dependencies ?? []
48
- const depFile = `${dir}/plugin/.dependencies`
49
- if (fs.existsSync(depFile)) this.dependencies = without(fs.readFileSync(depFile, 'utf8').split('\n').map(item => trim(item)), '')
50
- this.config = parseObject(omit(cfg, ['title', 'dependencies']), { parseValue: true })
51
- }
52
-
53
- _onoff = async (item, ...args) => {
54
- this.state[item] = false
55
- const { runHook, importModule } = this.app.bajo
56
- const mod = await importModule(`${this.dir.pkg}/plugin/${item}.js`)
57
- if (mod) {
58
- const text = this.print.write('plugin%s', this.print.write(item))
59
- this.log.trace(text)
60
- await runHook(`${this.name}:${camelCase(`before ${item}`)}`)
61
- await mod.call(this, ...args)
62
- await runHook(`${this.name}:${camelCase(`after ${item}`)}`)
63
- }
64
- this.state[item] = true
45
+ this.config = parseObject(cfg, { parseValue: true })
65
46
  }
66
47
 
67
48
  init = async () => {
68
- await this._onoff('init')
69
49
  }
70
50
 
71
- start = async (...args) => {
72
- const { freeze } = this.app.bajo
73
- freeze(this.config)
74
- await this._onoff('start', ...args)
51
+ start = async () => {
75
52
  }
76
53
 
77
54
  stop = async () => {
78
- await this._onoff('stop')
79
55
  }
80
56
  }
81
57
 
@@ -43,7 +43,6 @@ class Plugin {
43
43
  this.config = {}
44
44
  this.lib = lib
45
45
  this.lib.outmatchNs = outmatchNs.bind(this)
46
- this.exitHandler = undefined
47
46
  }
48
47
 
49
48
  getConfig = (path, options = {}) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "1.1.19",
3
+ "version": "1.2.1",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {
@@ -1,14 +0,0 @@
1
- async function collectExitHandlers () {
2
- const { importModule, eachPlugins, print, join } = this.bajo
3
- if (!this.bajo.config.exitHandler) return
4
- const nss = []
5
- await eachPlugins(async function ({ ns, dir }) {
6
- const mod = await importModule(`${dir}/plugin/exit.js`)
7
- if (!mod) return undefined
8
- this.app[ns].exitHandler = mod
9
- nss.push(ns)
10
- })
11
- this.bajo.log.trace('exitHandlers%s', nss.length === 0 ? print.write('none') : join(nss))
12
- }
13
-
14
- export default collectExitHandlers