bajo 0.3.4 → 0.3.6

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.
@@ -1,7 +1,7 @@
1
1
  import { filter, isArray, each, pullAt, camelCase, has, find, set, get, cloneDeep } from 'lodash-es'
2
2
 
3
3
  async function buildCollections (options = {}) {
4
- const { getConfig, getPluginName, fatal, runHook, error } = this.bajo.helper
4
+ const { getConfig, getPluginName, fatal, runHook, error, join } = this.bajo.helper
5
5
  let { plugin, handler, dupChecks = [], container = 'connections', useDefaultName } = options
6
6
  useDefaultName = useDefaultName ?? true
7
7
  if (!plugin) plugin = getPluginName(4)
@@ -32,7 +32,7 @@ async function buildCollections (options = {}) {
32
32
  each(dupChecks, d => {
33
33
  const checker = set({}, d, c[d])
34
34
  const match = filter(data, checker)
35
- if (match.length > 1) fatal('One or more %s shared the same \'%s\'', container, dupChecks.join(', '))
35
+ if (match.length > 1) fatal('One or more %s shared the same \'%s\'', container, join(dupChecks))
36
36
  })
37
37
  })
38
38
  await runHook(`${plugin}:${camelCase(`after build ${container}`)}`)
@@ -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,5 +1,6 @@
1
1
  import resolvePath from './resolve-path.js'
2
- import { isFunction, isPlainObject, get } from 'lodash-es'
2
+ import getPluginFile from './get-plugin-file.js'
3
+ import { isFunction, isPlainObject } from 'lodash-es'
3
4
  import error from './error.js'
4
5
  import fs from 'fs-extra'
5
6
 
@@ -12,13 +13,7 @@ async function load (file, asDefaultImport = true, noCache = true) {
12
13
  }
13
14
 
14
15
  async function importModule (file, { asDefaultImport, asHandler, noCache } = {}) {
15
- if (file.includes(':')) {
16
- const [plugin, path] = file.split(':')
17
- if (plugin.length > 1) {
18
- const dir = get(this[plugin], 'config.dir.pkg')
19
- file = `${dir}${path}`
20
- }
21
- }
16
+ file = getPluginFile.call(this, file)
22
17
  if (!fs.existsSync(file)) return
23
18
  let mod = await load(file, asDefaultImport, noCache)
24
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
  }
@@ -0,0 +1,11 @@
1
+ import { isPlainObject } from 'lodash-es'
2
+ import isSet from './is-set.js'
3
+
4
+ const join = (array, sep) => {
5
+ if (isSet(sep) && !isPlainObject(sep)) return array.join(sep)
6
+ const { separator = ', ', joiner = 'and' } = sep ?? {}
7
+ const last = (array.pop() ?? '').trim()
8
+ return array.map(a => (a + '').trim()).join(separator) + ` ${joiner} ${last}`
9
+ }
10
+
11
+ export default join
@@ -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()
@@ -58,7 +58,6 @@ class Log {
58
58
  text = JSON.stringify(json)
59
59
  } else {
60
60
  text = `[${dayjs(dt).utc(true).format(this.format)}] ${upperFirst(level)}: ${msg}`
61
- // if (!isEmpty(data)) text += '\n ' + (pretty.render(data, prettyOpts).split('\n').join('\n '))
62
61
  if (!isEmpty(data)) text += '\n' + JSON.stringify(data)
63
62
  }
64
63
  console.log(text)
@@ -2,7 +2,7 @@ import { reduce, map, trim, keys, intersection, each, camelCase, get } from 'lod
2
2
  import semver from 'semver'
3
3
 
4
4
  async function runner ({ plugin, pkg, dependencies }) {
5
- const { log, getConfig, error } = this.bajo.helper
5
+ const { log, getConfig, error, join } = this.bajo.helper
6
6
  log.trace('Checking dependencies: %s', plugin)
7
7
  const config = getConfig()
8
8
  const odep = reduce(dependencies, (o, k) => {
@@ -13,7 +13,7 @@ async function runner ({ plugin, pkg, dependencies }) {
13
13
  const deps = keys(odep)
14
14
  if (deps.length > 0) {
15
15
  if (intersection(config.plugins, deps).length !== deps.length) {
16
- throw error('Dependency for \'%s\' unfulfilled: %s', pkg, deps.join(', '), { code: 'BAJO_DEPENDENCY' })
16
+ throw error('Dependency for \'%s\' unfulfilled: %s', pkg, join(deps), { code: 'BAJO_DEPENDENCY' })
17
17
  }
18
18
  each(deps, d => {
19
19
  if (!odep[d]) return
@@ -1,7 +1,7 @@
1
1
  import { isFunction, isPlainObject, map } from 'lodash-es'
2
2
 
3
3
  async function collectConfigHandlers (pkg) {
4
- const { getModuleDir, importModule, log } = this.bajo.helper
4
+ const { getModuleDir, importModule, log, join } = this.bajo.helper
5
5
  for (const pkg of this.bajo.config.plugins) {
6
6
  let dir
7
7
  try {
@@ -16,7 +16,7 @@ async function collectConfigHandlers (pkg) {
16
16
  this.bajo.configHandlers.concat(mod)
17
17
  }
18
18
  const exts = map(this.bajo.configHandlers, 'ext')
19
- log.trace('Config handlers: %s', exts.join(', '))
19
+ log.trace('Config handlers: %s', join(exts))
20
20
  }
21
21
 
22
22
  export default collectConfigHandlers
@@ -1,7 +1,7 @@
1
1
  import {} from 'lodash-es'
2
2
 
3
3
  async function collectExitHandlers () {
4
- const { importModule, log, eachPlugins, getConfig, print } = this.bajo.helper
4
+ const { importModule, log, eachPlugins, getConfig, print, join } = this.bajo.helper
5
5
  const config = getConfig()
6
6
  if (!config.exitHandler) return
7
7
  this.bajo.exitHandler = this.bajo.exitHandler ?? {}
@@ -12,7 +12,7 @@ async function collectExitHandlers () {
12
12
  this.bajo.exitHandler[plugin] = mod
13
13
  names.push(plugin)
14
14
  })
15
- log.trace('Exit handlers: %s', names.length === 0 ? print.__('none') : names.join(', '))
15
+ log.trace('Exit handlers: %s', names.length === 0 ? print.__('none') : join(names))
16
16
  }
17
17
 
18
18
  export default collectExitHandlers
@@ -1,7 +1,7 @@
1
1
  import { set, get, camelCase, upperFirst, map } from 'lodash-es'
2
2
 
3
3
  async function run () {
4
- const { runHook, log, eachPlugins, importModule, freeze, getConfig, print } = this.bajo.helper
4
+ const { runHook, log, eachPlugins, importModule, freeze, getConfig, print, join } = this.bajo.helper
5
5
  const config = getConfig()
6
6
  const methods = ['init']
7
7
  if (!get(config, 'tool')) methods.push('start')
@@ -20,7 +20,7 @@ async function run () {
20
20
  })
21
21
  await runHook(`bajo:${camelCase(`after ${f} all plugins`)}`)
22
22
  }
23
- log.debug('Loaded plugins: %s', map(config.plugins, b => camelCase(b)).join(', '))
23
+ log.debug('Loaded plugins: %s', join(map(config.plugins, b => camelCase(b))))
24
24
  }
25
25
 
26
26
  export default run
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "A framework to build a giant monstrous app rapidly",
5
5
  "main": "boot/index.js",
6
6
  "scripts": {