bajo-extra 0.2.7 → 0.2.9

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,45 +1,47 @@
1
1
  async function fetchAndSave ({ source = {}, converter, coll, current = {}, options = {} } = {}) {
2
- const { print, setImmediate, importPkg } = this.bajo.helper
2
+ const { setImmediate, importPkg, spinner, print } = this.bajo.helper
3
3
  const { isEmpty, isFunction } = await importPkg('lodash-es')
4
4
  const { fetch } = this.bajoExtra.helper
5
5
  const { recordCreate, recordFind, recordUpdate, validationErrorMessage } = this.bajoDb.helper
6
- const opts = { type: options.showSpinner ? 'bora' : 'log', pkg: options.pkg }
7
- const spinner = print.bora('Fetching starts...', { showCounter: true, showDatetime: true, isEnabled: !options.returnEarly }).start()
6
+ const spin = spinner({ showCounter: true }).start('Fetching starts...')
8
7
  const resp = await fetch(source.url, source.options ?? {})
9
- if (isEmpty(resp)) spinner.fatal('No result from server, aborted!')
8
+ if (isEmpty(resp)) spin.fatal('No result from server, aborted!')
10
9
  if (source.abort) {
11
10
  const aborted = await source.abort.call(this, resp)
12
- if (aborted) spinner.fatal(aborted)
11
+ if (aborted) spin.fatal(aborted)
13
12
  }
14
13
  let count = 0
15
- spinner.setText('Got %d records, processing...', resp.response.length)
16
- const iterator = isFunction(source.dataKey) ? await source.dataKey.call(this, resp) : resp[source.dataKey]
14
+ const iterator = isFunction(source.iterator) ? await source.iterator.call(this, resp) : resp[source.iterator]
15
+ spin.setText('Got %d records, processing...', iterator.length)
17
16
  for (let r of iterator) {
18
17
  await setImmediate()
19
18
  if (converter) r = await converter.call(this, r, options)
19
+ if (isEmpty(r)) continue
20
20
  try {
21
21
  await recordCreate(coll, r)
22
22
  if (current.coll && current.query) {
23
23
  const query = await current.query.call(this, r)
24
- const recs = await recordFind(current.coll, { query })
24
+ const recs = await recordFind(current.coll, { query }, { skipCache: true })
25
25
  const rc = current.converter ? await current.converter.call(this, r, options) : r
26
- if (recs.length > 0) {
27
- const id = recs[0].id
28
- await recordUpdate(current.coll, id, rc)
29
- } else {
30
- await recordCreate(current.coll, rc)
26
+ if (rc) {
27
+ if (recs.length > 0) {
28
+ const id = recs[0].id
29
+ await recordUpdate(current.coll, id, rc)
30
+ } else {
31
+ await recordCreate(current.coll, rc)
32
+ }
31
33
  }
32
34
  }
33
- if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spinner.getElapsed()}] Batch line %d/%d`, count, resp.response.length, opts)
34
- else spinner.setText('Record %d/%d...', count, resp.response.length)
35
+ if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spin.getElapsed()}] Batch line %d/%d`, count, iterator.length)
36
+ else if (!spin.opts.isLog) spin.setText('Record %d/%d...', count, iterator.length)
35
37
  count++
36
38
  } catch (err) {
37
39
  console.log(err)
38
- spinner.setText(validationErrorMessage(err) + ', continue')
40
+ spin.setText(validationErrorMessage(err) + ', continue')
39
41
  }
40
42
  }
41
- spinner.info(`${count}/${resp.response.length} records processed`)
42
- spinner.succeed('Done!')
43
+ spin.info(`${count}/${iterator.length} records processed`)
44
+ spin.succeed('Done!')
43
45
  }
44
46
 
45
47
  export default fetchAndSave
@@ -1,26 +1,21 @@
1
1
  import Path from 'path'
2
2
 
3
- function makeProgress (spinner) {
3
+ function makeProgress (spin) {
4
4
  return async function ({ batchNo, batchTotal, data } = {}) {
5
5
  if (batchTotal === 0) return
6
- spinner.setText('Batch %d of %d (%d records)', batchNo, batchTotal, data.length)
6
+ spin.setText('Batch %d of %d (%d records)', batchNo, batchTotal, data.length)
7
7
  }
8
8
  }
9
9
 
10
- async function exportTo ({ path, args, returnEarly }) {
11
- const { importPkg, print, dayjs, getConfig, importModule } = this.bajo.helper
10
+ async function exportTo ({ path, args }) {
11
+ const { importPkg, print, dayjs, getConfig, importModule, spinner } = this.bajo.helper
12
12
  const { isEmpty, map } = await importPkg('lodash-es')
13
13
  const [input, select] = await importPkg('bajo-cli:@inquirer/input',
14
14
  'bajo-cli:@inquirer/select')
15
- if (!this.bajoDb) {
16
- print.fail('Bajo DB isn\'t loaded', { exit: !returnEarly })
17
- if (returnEarly) return
18
- }
15
+ const config = getConfig()
16
+ if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
19
17
  const schemas = map(this.bajoDb.schemas, 'name')
20
- if (isEmpty(schemas)) {
21
- print.fail('No schema found!', { exit: !returnEarly })
22
- if (returnEarly) return
23
- }
18
+ if (isEmpty(schemas)) return print.fail('No schema found!', { exit: config.tool })
24
19
  let [coll, dest, query] = args
25
20
  if (isEmpty(coll)) {
26
21
  coll = await select({
@@ -40,8 +35,8 @@ async function exportTo ({ path, args, returnEarly }) {
40
35
  message: print.__('Please enter a query (if any):')
41
36
  })
42
37
  }
43
- const spinner = print.bora('Exporting...').start()
44
- const progressFn = makeProgress.call(this, spinner)
38
+ const spin = spinner().start('Exporting...')
39
+ const progressFn = makeProgress.call(this, spin)
45
40
  const cfg = getConfig('bajoDb', { full: true })
46
41
  const { batch } = getConfig()
47
42
  const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
@@ -50,10 +45,10 @@ async function exportTo ({ path, args, returnEarly }) {
50
45
  try {
51
46
  const filter = { query }
52
47
  const result = await this.bajoExtra.helper.exportTo(coll, dest, { filter, batch, progressFn })
53
- spinner.succeed('%d records successfully exported to \'%s\'', result.count, Path.resolve(result.file))
48
+ spin.succeed('%d records successfully exported to \'%s\'', result.count, Path.resolve(result.file))
54
49
  } catch (err) {
55
50
  console.log(err)
56
- spinner.fail('Error: %s', err.message, { exit: !returnEarly })
51
+ spin.fail('Error: %s', err.message, { exit: config.tool })
57
52
  }
58
53
  }
59
54
 
@@ -1,25 +1,20 @@
1
1
  import Path from 'path'
2
2
 
3
- function makeProgress (spinner) {
3
+ function makeProgress (spin) {
4
4
  return async function ({ batchNo, data } = {}) {
5
- spinner.setText('Batch %d (%d records)', batchNo, data.length)
5
+ spin.setText('Batch %d (%d records)', batchNo, data.length)
6
6
  }
7
7
  }
8
8
 
9
- async function importFrom ({ path, args, returnEarly }) {
10
- const { importPkg, print, importModule, getConfig } = this.bajo.helper
9
+ async function importFrom ({ path, args }) {
10
+ const { importPkg, print, importModule, getConfig, spinner } = this.bajo.helper
11
11
  const { isEmpty, map } = await importPkg('lodash-es')
12
12
  const [input, select, confirm] = await importPkg('bajo-cli:@inquirer/input',
13
13
  'bajo-cli:@inquirer/select', 'bajo-cli:@inquirer/confirm')
14
- if (!this.bajoDb) {
15
- print.fail('Bajo DB isn\'t loaded', { exit: !returnEarly })
16
- if (returnEarly) return
17
- }
14
+ const config = getConfig()
15
+ if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
18
16
  const schemas = map(this.bajoDb.schemas, 'name')
19
- if (isEmpty(schemas)) {
20
- print.fail('No schema found!', { exit: !returnEarly })
21
- if (returnEarly) return
22
- }
17
+ if (isEmpty(schemas)) return print.fail('No schema found!', { exit: config.tool })
23
18
  let [dest, coll] = args
24
19
  if (isEmpty(dest)) {
25
20
  dest = await input({
@@ -37,12 +32,9 @@ async function importFrom ({ path, args, returnEarly }) {
37
32
  message: print.__('You\'re about to replace ALL records with the new ones. Are you really sure?'),
38
33
  default: false
39
34
  })
40
- if (!answer) {
41
- print.fail('Aborted!', { exit: !returnEarly })
42
- if (returnEarly) return
43
- }
44
- const spinner = print.bora('Importing...').start()
45
- const progressFn = makeProgress.call(this, spinner)
35
+ if (!answer) return print.fail('Aborted!', { exit: config.tool })
36
+ const spin = spinner().start('Importing...')
37
+ const progressFn = makeProgress.call(this, spin)
46
38
  const cfg = getConfig('bajoDb', { full: true })
47
39
  const { batch } = getConfig()
48
40
  const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
@@ -50,9 +42,9 @@ async function importFrom ({ path, args, returnEarly }) {
50
42
  await start.call(this, connection.name)
51
43
  try {
52
44
  const result = await this.bajoExtra.helper.importFrom(dest, coll, { batch, progressFn })
53
- spinner.succeed('%d records successfully imported from \'%s\'', result.count, Path.resolve(result.file))
45
+ spin.succeed('%d records successfully imported from \'%s\'', result.count, Path.resolve(result.file))
54
46
  } catch (err) {
55
- spinner.fail('Error: %s', err.message, { exit: !returnEarly })
47
+ spin.fail('Error: %s', err.message, { exit: config.tool })
56
48
  }
57
49
  }
58
50
 
package/bajoCli/tool.js CHANGED
@@ -1,8 +1,8 @@
1
- async function tool ({ path, args = [], returnEarly }) {
1
+ async function tool ({ path, args = [] }) {
2
2
  const { currentLoc } = this.bajo.helper
3
3
  const { runToolMethod } = this.bajoCli.helper
4
4
  const options = { demonize: ['shell'] }
5
- await runToolMethod({ path, args, dir: `${currentLoc(import.meta).dir}/tool`, options, returnEarly })
5
+ await runToolMethod({ path, args, dir: `${currentLoc(import.meta).dir}/tool`, options })
6
6
  }
7
7
 
8
8
  export default tool
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {