bajo 0.3.3 → 0.3.5

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.
@@ -0,0 +1,14 @@
1
+ import { get } from 'lodash-es'
2
+
3
+ function getPluginFile (file) {
4
+ if (file.includes(':')) {
5
+ const [plugin, path] = file.split(':')
6
+ if (plugin !== 'file' && this && this[plugin] && plugin.length > 1) {
7
+ const dir = get(this[plugin], 'config.dir.pkg')
8
+ file = `${dir}${path}`
9
+ }
10
+ }
11
+ return file
12
+ }
13
+
14
+ export default getPluginFile
@@ -1,9 +1,10 @@
1
1
  import resolvePath from './resolve-path.js'
2
+ import getPluginFile from './get-plugin-file.js'
2
3
  import { isFunction, isPlainObject } from 'lodash-es'
3
4
  import error from './error.js'
4
5
  import fs from 'fs-extra'
5
6
 
6
- async function load (file, asDefaultImport = true, noCache = false) {
7
+ async function load (file, asDefaultImport = true, noCache = true) {
7
8
  file = resolvePath(file, true)
8
9
  if (noCache) file += `?_=${Date.now()}`
9
10
  const imported = await import(file)
@@ -12,6 +13,7 @@ async function load (file, asDefaultImport = true, noCache = false) {
12
13
  }
13
14
 
14
15
  async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
16
+ file = getPluginFile.call(this, file)
15
17
  if (!fs.existsSync(file)) return
16
18
  let mod = await load(file, asDefaultImport, noCache)
17
19
  if (!asHandler) return mod
@@ -1,4 +1,4 @@
1
- import { isPlainObject, map, last, isEmpty, has, keys, values, trim, get } from 'lodash-es'
1
+ import { isPlainObject, last, isEmpty, has, keys, values, get } from 'lodash-es'
2
2
  import os from 'os'
3
3
  import getModuleDir from './get-module-dir.js'
4
4
  import resolvePath from './resolve-path.js'
@@ -7,46 +7,21 @@ import defaultsDeep from './defaults-deep.js'
7
7
  import path from 'path'
8
8
  import fs from 'fs-extra'
9
9
 
10
- /**
11
- * Load/import a package dynamically. You can import package one-by-one or multiple
12
- * packages at once.
13
- *
14
- * Package 'pkg' must be in the following format: ```<original name>:<new name>:<bajo package name>```
15
- * ```<new name>``` can be omitted, so the format will be: ```<name>::<bajo package name>```
16
- *
17
- * If you only import one package, returned value is the imported package itself
18
- * If multiple packages are imported, returned value is an object with ```<new name>``` as its
19
- * keys and imported packages as its values
20
- *
21
- * Example:
22
- * ```
23
- * const imported = await importPkg('ora::bajo-cli')
24
- * const multiple = await importPkg('ora::bajo-cli', 'lodash:_:bajo')
25
- *
26
- * @param {...string} pkg
27
- * @returns
28
- */
29
-
30
- async function importPkg (...pkg) {
10
+ async function importPkg (...pkgs) {
31
11
  const result = {}
32
12
  let opts = { returnDefault: true, thrownNotFound: false, noCache: false }
33
- if (isPlainObject(last(pkg))) {
34
- opts = defaultsDeep(pkg.pop(), opts)
13
+ if (isPlainObject(last(pkgs))) {
14
+ opts = defaultsDeep(pkgs.pop(), opts)
35
15
  }
36
- for (const p of pkg) {
37
- const parts = map(p.split(':'), i => trim(i))
38
- let [ns, orgName, name] = parts
39
- if (parts.length === 1) {
40
- orgName = ns
41
- ns = 'bajo'
42
- name = orgName
43
- } else if (parts.length === 2) {
44
- name = orgName
16
+ for (const pkg of pkgs) {
17
+ let [plugin, name] = pkg.split(':').map(item => item.trim())
18
+ if (!name) {
19
+ name = plugin
20
+ plugin = 'bajo'
45
21
  }
46
- if (isEmpty(name)) name = orgName
47
- const dir = getModuleDir.call(this, orgName, ns)
48
- const pkg = readJson(`${dir}/package.json`, opts.thrownNotFound)
49
- const mainFileOrg = dir + '/' + (pkg.main ?? get(pkg, 'exports.default', 'index.js'))
22
+ const dir = getModuleDir.call(this, name, plugin)
23
+ const p = readJson(`${dir}/package.json`, opts.thrownNotFound)
24
+ const mainFileOrg = dir + '/' + (p.main ?? get(p, 'exports.default', 'index.js'))
50
25
  let mainFile = resolvePath(mainFileOrg, os.platform() === 'win32')
51
26
  if (isEmpty(path.extname(mainFile))) {
52
27
  if (fs.existsSync(`${mainFileOrg}/index.js`)) mainFile += '/index.js'
@@ -60,7 +35,7 @@ async function importPkg (...pkg) {
60
35
  }
61
36
  result[name] = mod
62
37
  }
63
- if (pkg.length === 1) return result[keys(result)[0]]
38
+ if (pkgs.length === 1) return result[keys(result)[0]]
64
39
  if (opts.asObject) return result
65
40
  return values(result)
66
41
  }
@@ -1,5 +1,6 @@
1
1
  import path from 'path'
2
2
  import resolvePath from './resolve-path.js'
3
+ import getPluginFile from './get-plugin-file.js'
3
4
  import readJson from './read-json.js'
4
5
  import parseObject from './parse-object.js'
5
6
  import { find, map, isEmpty } from 'lodash-es'
@@ -7,7 +8,7 @@ import error from './error.js'
7
8
  import fg from 'fast-glob'
8
9
 
9
10
  async function readConfig (file, { pattern, globOptions = {}, ignoreError, defValue = {} } = {}) {
10
- file = resolvePath(file)
11
+ file = resolvePath(getPluginFile.call(this, file))
11
12
  let ext = path.extname(file)
12
13
  const fname = path.dirname(file) + '/' + path.basename(file, ext)
13
14
  ext = ext.toLowerCase()
@@ -1,7 +1,6 @@
1
1
  async function startPlugin (name, ...args) {
2
- const { getConfig, importModule } = this.bajo.helper
3
- const cfg = getConfig(name, { full: true })
4
- const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
2
+ const { importModule } = this.bajo.helper
3
+ const start = await importModule(`${name}:/bajo/start.js`)
5
4
  await start.call(this, ...args)
6
5
  }
7
6
 
@@ -7,8 +7,7 @@ async function collectExitHandlers () {
7
7
  this.bajo.exitHandler = this.bajo.exitHandler ?? {}
8
8
  const names = []
9
9
  await eachPlugins(async function ({ plugin, dir }) {
10
- const file = `${dir}/bajo/exit.js`
11
- const mod = await importModule(file)
10
+ const mod = await importModule(`${dir}/bajo/exit.js`)
12
11
  if (!mod) return undefined
13
12
  this.bajo.exitHandler[plugin] = mod
14
13
  names.push(plugin)
@@ -8,8 +8,7 @@ async function run () {
8
8
  for (const f of methods) {
9
9
  await runHook(`bajo:${camelCase(`before ${f} all plugins`)}`)
10
10
  await eachPlugins(async function ({ plugin, dir }) {
11
- const file = `${dir}/bajo/${f}.js`
12
- const mod = await importModule(file)
11
+ const mod = await importModule(`${dir}/bajo/${f}.js`)
13
12
  if (mod) {
14
13
  log.debug('%s: %s', print.__(upperFirst(f)), plugin)
15
14
  await runHook(`bajo:${camelCase(`before ${f} ${plugin}`)}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {