bajo-extra 0.3.7 → 1.0.2

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.
package/README.md CHANGED
@@ -1 +1,23 @@
1
- # bajo-extra
1
+ # bajo-extra
2
+
3
+ Plugin name: **bajoExtra**, alias: **extra**
4
+
5
+ ![GitHub package.json version](https://img.shields.io/github/package-json/v/ardhi/bajo-extra) ![NPM Version](https://img.shields.io/npm/v/bajo-extra)
6
+
7
+ > <br />**Attention**: I do NOT accept any pull request at the moment, thanks!<br /><br />
8
+
9
+ Extra tools & support for [Bajo](https://github.com/ardhi/bajo)
10
+
11
+ ## Installation
12
+
13
+ Goto your ```<bajo-base-dir>``` and type:
14
+
15
+ ```bash
16
+ $ npm install bajo-extra
17
+ ```
18
+
19
+ Now open your ```<bajo-data-dir>/config/.plugins``` and put ```bajo-extra``` in it
20
+
21
+ ## License
22
+
23
+ [MIT](LICENSE)
package/bajo/.alias ADDED
@@ -0,0 +1 @@
1
+ extra
package/bajo/config.json CHANGED
@@ -1,5 +1,4 @@
1
1
  {
2
- "alias": "x",
3
2
  "fetch": {
4
3
  "agent": {}
5
4
  }
@@ -0,0 +1,17 @@
1
+ {
2
+ "downloading%s": "Downloading %s...",
3
+ "noServerResponse": "No response from server",
4
+ "noRecordToProcess": "No record to process",
5
+ "gotRecordsProcessing": "Got %d records, processing...",
6
+ "rec%d%d": "Record %d/%d...",
7
+ "recProcessed%s%d%d": "[%s] %d/%d records processed",
8
+ "createdUpdatedSkipped%s%d%d%d": "[%s] Created: %d, Updated: %d, Skipped: %d",
9
+ "handlerMustBeProvided": "A function handler must be provided",
10
+ "bulkFetchStarting": "Bulk fetch starting",
11
+ "fetchingStarts": "Fetching starts...",
12
+ "batch%s%d": "[%s] Batch #%d",
13
+ "allDone": "All done!",
14
+ "dlDirNotExists%s": "Download dir '%s' doesn't exist",
15
+ "gettingStatus%s": "Getting %s status",
16
+ "downloading": "Downloading..."
17
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "downloading%s": "Pengunduhan %s...",
3
+ "noServerResponse": "Tidak ada respon dari server",
4
+ "noRecordToProcess": "Tidak ada data yang perlu diproses",
5
+ "gotRecordsProcessing": "Mendapatkan %d data, diproses...",
6
+ "rec%d%d": "Data %d/%d...",
7
+ "recProcessed%s%d%d": "[%s] %d/%d data diproses",
8
+ "createdUpdatedSkipped%s%d%d%d": "[%s] Dibuat: %d, Diubah: %d, Lompati: %d",
9
+ "handlerMustBeProvided": "Sebuah fungsi harus diberikan",
10
+ "bulkFetchStarting": "Bulk fetch dimulai",
11
+ "fetchingStarts": "Fetching dimulai...",
12
+ "batch%s%d": "[%s] Batch #%d",
13
+ "allDone": "Selesai semua!",
14
+ "dlDirNotExists%s": "Dir unduh '%s' belum ada",
15
+ "gettingStatus%s": "Mendapatkan status %s",
16
+ "downloading": "Pengunduhan..."
17
+ }
@@ -1,7 +1,6 @@
1
1
  // taken from: https://stackoverflow.com/a/41439945
2
- import fs from 'fs'
3
-
4
2
  function countFileLines (file) {
3
+ const { fs } = this.app.bajo.lib
5
4
  return new Promise((resolve, reject) => {
6
5
  let lineCount = 0
7
6
  fs.createReadStream(file)
@@ -2,16 +2,16 @@ import path from 'path'
2
2
  import { Readable } from 'node:stream'
3
3
 
4
4
  async function download (url, opts = {}, extra = {}) {
5
- const { fs, getPluginDataDir, importPkg, error, generateId } = this.bajo.helper
6
- const { fetch, formatByte, formatPercentage } = this.bajoExtra.helper
7
- const { isFunction, merge } = this.bajo.helper._
5
+ const { getPluginDataDir, importPkg, generateId } = this.app.bajo
6
+ const { fs } = this.app.bajo.lib
7
+ const { isFunction, merge } = this.app.bajo.lib._
8
8
  if (typeof opts === 'string') extra = { dir: opts }
9
9
  const increment = await importPkg('add-filename-increment')
10
10
  if (!extra.dir) {
11
11
  extra.dir = `${getPluginDataDir('bajoExtra')}/download`
12
12
  fs.ensureDirSync(extra.dir)
13
13
  }
14
- if (!fs.existsSync(extra.dir)) throw error('Download dir \'%s\' doesn\'t exists', extra.dir)
14
+ if (!fs.existsSync(extra.dir)) throw this.error('dlDirNotExists%s', extra.dir)
15
15
  if (extra.randomFileName) {
16
16
  const ext = path.extname(url)
17
17
  extra.fileName = `${generateId()}${ext}`
@@ -22,7 +22,7 @@ async function download (url, opts = {}, extra = {}) {
22
22
  const { headers, body, ok, status } = await fetch(url, opts, merge({}, extra, { rawResponse: true }))
23
23
  if (!ok) {
24
24
  fs.removeSync(file)
25
- throw error('Getting %s status', status)
25
+ throw this.error('gettingStatus%s', status)
26
26
  }
27
27
  const total = headers['content-length'] ?? 0
28
28
  const data = Readable.fromWeb(body)
@@ -31,9 +31,9 @@ async function download (url, opts = {}, extra = {}) {
31
31
  length += chunk.length
32
32
  if (isFunction(extra.progressFn)) extra.progressFn.call(this, length, total)
33
33
  else if (extra.spin) {
34
- extra.spinText = extra.spinText ?? 'Downloading...'
35
- if (total === 0) extra.spin.setText(`${extra.spinText} %s`, formatByte(length))
36
- else extra.spin.setText(`${extra.spinText} %s of %s (%s)`, formatByte(length), formatByte(total), formatPercentage(length / total))
34
+ extra.spinText = extra.spinText ?? 'downloading'
35
+ if (total === 0) extra.spin.setText(`${extra.spinText} %s`, this.formatByte(length))
36
+ else extra.spin.setText(`${extra.spinText} %s of %s (%s)`, this.formatByte(length), this.formatByte(total), this.formatPercentage(length / total))
37
37
  }
38
38
  })
39
39
  data.pipe(writer)
@@ -1,6 +1,6 @@
1
1
  async function handler (rec, bulk) {
2
- const { isFunction, set } = this.bajo.helper._
3
- const { recordCreate, recordFind, recordUpdate } = this.bajoDb.helper
2
+ const { isFunction, set } = this.app.bajo.lib._
3
+ const { recordCreate, recordFind, recordUpdate } = this.app.bajoDb
4
4
  const save = bulk.save ?? {}
5
5
  const current = save.current ?? {}
6
6
  let existing
@@ -51,13 +51,12 @@ async function handler (rec, bulk) {
51
51
  }
52
52
 
53
53
  async function fetchAndSave ({ url, bulk, save = {}, opts = {} } = {}) {
54
- const { startPlugin } = this.bajo.helper
55
- const { fetchBulk } = this.bajoExtra.helper
56
- const { merge } = this.bajo.helper._
54
+ const { startPlugin } = this.bajo
55
+ const { merge } = this.bajo.lib._
57
56
  merge(bulk, { handler, save })
58
57
  await startPlugin('bajoDb')
59
58
 
60
- await fetchBulk(url, bulk, opts)
59
+ await this.fetchBulk(url, bulk, opts)
61
60
  }
62
61
 
63
62
  export default fetchAndSave
@@ -1,11 +1,10 @@
1
1
  async function fetching ({ url, opts, bulk, spin }) {
2
- const { setImmediate, print } = this.bajo.helper
3
- const { isEmpty, isFunction, has } = this.bajo.helper._
4
- const { validationErrorMessage } = this.bajoDb.helper
5
- const { fetch } = this.bajoExtra.helper
6
- const resp = await fetch(url, opts ?? {})
2
+ const { setImmediate, print } = this.app.bajo
3
+ const { isEmpty, isFunction, has } = this.app.bajo.lib._
4
+ const { validationErrorMessage } = this.app.bajoDb
5
+ const resp = await this.fetch(url, opts ?? {})
7
6
  if (isEmpty(resp)) {
8
- spin.fatal('No result from server, aborted!')
7
+ spin.fatal('noServerResponse')
9
8
  return -1
10
9
  }
11
10
  if (bulk.abort) {
@@ -21,10 +20,10 @@ async function fetching ({ url, opts, bulk, spin }) {
21
20
  if (bulk.printCount === true) bulk.printCount = 100
22
21
  const data = isFunction(bulk.dataKey) ? await bulk.dataKey.call(this, resp) : resp[bulk.dataKey]
23
22
  if (data.length === 0) {
24
- print.warn('No records to process, abort')
23
+ print.warn('noRecordToProcess')
25
24
  return 0
26
25
  }
27
- spin.setText('Got %d records, processing...', data.length)
26
+ spin.setText('gotRecordsProcessing%d', data.length)
28
27
  for (let r of data) {
29
28
  await setImmediate()
30
29
  if (bulk.converter) r = await bulk.converter.call(this, r, bulk)
@@ -36,41 +35,40 @@ async function fetching ({ url, opts, bulk, spin }) {
36
35
  const result = await bulk.handler.call(this, r, bulk)
37
36
  if (result && has(stat, result)) stat[result]++
38
37
  if (bulk.printCount && bulk.printCount < count && (count % bulk.printCount === 0)) print.succeed('[%s] Processed %d/%d', spin.getElapsed(), count, data.length)
39
- else if (!spin.opts.isLog) spin.setText('Record %d/%d...', count, data.length)
38
+ else if (!spin.opts.isLog) spin.setText('rec%d%d', count, data.length)
40
39
  count++
41
40
  } catch (err) {
42
41
  console.log(err)
43
42
  spin.setText(validationErrorMessage(err) + ', continue')
44
43
  }
45
44
  }
46
- print.succeed('[%s] %d/%d records processed', spin.getElapsed(), count, data.length)
47
- if (!bulk.noStat) print.succeed('[%s] Created: %d, Updated: %d, Skipped: %d', spin.getElapsed(), stat.created, stat.updated, stat.skipped)
45
+ print.succeed('recProcessed%s%d%d', spin.getElapsed(), count, data.length)
46
+ if (!bulk.noStat) print.succeed('createdUpdatedSkipped%s%d%d%d', spin.getElapsed(), stat.created, stat.updated, stat.skipped)
48
47
  return data.length
49
48
  }
50
49
 
51
50
  async function fetchBulk (url, bulk = {}, opts = {}) {
52
- const { print, spinner, error } = this.bajo.helper
53
- const { isFunction } = this.bajo.helper._
51
+ const { isFunction } = this.bajo.lib._
54
52
  opts.params = opts.params ?? {}
55
53
  bulk.maxStep = bulk.maxStep ?? 0
56
- if (!isFunction(bulk.handler)) throw error('A function handler must be provided')
54
+ if (!isFunction(bulk.handler)) throw this.error('handlerMustBeProvided')
57
55
  if (isFunction(bulk.paramsIncFn)) {
58
- print.info('Bulk fetch starting')
59
- const spin = spinner({ showCounter: true }).start('Fetching starts...')
56
+ this.print.info('bulkFetchStarting')
57
+ const spin = this.print.spinner({ showCounter: true }).start('fetchingStarts')
60
58
  let step = 1
61
59
  for (;;) {
62
- print.info('[%s] Batch #%d', spin.getElapsed(), step)
60
+ this.print.info('batch%s%d', spin.getElapsed(), step)
63
61
  const newOpts = await bulk.paramsIncFn.call(this, { url, bulk, opts })
64
62
  if (newOpts) opts = newOpts
65
63
  const length = await fetching.call(this, { url, bulk, opts, spin })
66
64
  if (length === 0 || (bulk.maxStep > 0 && step >= bulk.maxStep)) {
67
- print.info('All done!')
65
+ this.print.info('allDone')
68
66
  break
69
67
  }
70
68
  step++
71
69
  }
72
70
  } else {
73
- const spin = spinner({ showCounter: true }).start('Fetching starts...')
71
+ const spin = this.print.spinner({ showCounter: true }).start('fetchingStarts')
74
72
  await fetching.call(this, { url, bulk, opts, spin })
75
73
  }
76
74
  }
@@ -2,9 +2,9 @@ import path from 'path'
2
2
  import { fetch, Agent } from 'undici'
3
3
 
4
4
  async function fetchUrl (url, opts = {}, extra = {}) {
5
- const { getConfig, isSet, fs } = this.bajo.helper
6
- const { has, isArray, isPlainObject, isString, cloneDeep, isEmpty, merge } = this.bajo.helper._
7
- const cfg = getConfig('bajoExtra')
5
+ const { isSet } = this.app.bajo
6
+ const { fs } = this.app.bajo.lib
7
+ const { has, isArray, isPlainObject, isString, cloneDeep, merge } = this.app.bajo.lib._
8
8
  if (isPlainObject(url)) {
9
9
  extra = cloneDeep(opts)
10
10
  opts = cloneDeep(url)
@@ -20,8 +20,8 @@ async function fetchUrl (url, opts = {}, extra = {}) {
20
20
  delete opts.params
21
21
  if (!has(extra, 'cacheBuster')) extra.cacheBuster = true
22
22
  if (extra.cacheBuster) opts.query[extra.cacheBusterKey ?? '_'] = Date.now()
23
- if (!isEmpty(cfg.fetch.agent)) {
24
- opts.dispatcher = new Agent(cfg.fetch.agent)
23
+ if (this.config.fetch.agent || extra.agent) {
24
+ opts.dispatcher = new Agent(extra.agent ?? this.config.fetch.agent)
25
25
  }
26
26
  if (opts.body && extra.formData) {
27
27
  const formData = new FormData()
@@ -1,7 +1,7 @@
1
1
  import { createGzip, createGunzip } from 'zlib'
2
2
 
3
3
  function gzip (file, deleteOld, expand) {
4
- const { fs } = this.bajo.helper
4
+ const { fs } = this.app.bajo.lib
5
5
  return new Promise((resolve, reject) => {
6
6
  const newFile = expand ? file.slice(0, file.length - 3) : (file + '.gz')
7
7
  const reader = fs.createReadStream(file)
@@ -0,0 +1,15 @@
1
+ async function download (...args) {
2
+ const [url] = args
3
+ const spinText = this.print.write('downloading%s', this.print.write('file'))
4
+ const spin = this.print.spinner({ showCounter: true }).start(spinText)
5
+
6
+ let dest
7
+ try {
8
+ dest = await this.download(url, undefined, { spin, spinText })
9
+ } catch (err) {
10
+ spin.fatal('error%s', err.message)
11
+ }
12
+ spin.succeed('savedAs%s%s', this.print.write('file'), dest)
13
+ }
14
+
15
+ export default download
@@ -0,0 +1 @@
1
+ export default 'default'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "0.3.7",
3
+ "version": "1.0.2",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,12 +26,12 @@
26
26
  },
27
27
  "homepage": "https://github.com/ardhi/bajo-extra#readme",
28
28
  "dependencies": {
29
- "async": "^3.2.4",
29
+ "async": "^3.2.6",
30
30
  "bcrypt": "^5.1.1",
31
- "fast-jwt": "^4.0.1",
32
- "fast-xml-parser": "^4.3.6",
31
+ "fast-jwt": "^5.0.2",
32
+ "fast-xml-parser": "^4.5.1",
33
33
  "numbro": "^2.5.0",
34
34
  "performant-array-to-tree": "^1.11.0",
35
- "undici": "^6.16.1"
35
+ "undici": "^7.2.1"
36
36
  }
37
37
  }
@@ -1,17 +0,0 @@
1
- async function download ({ path, args }) {
2
- const { spinner } = this.bajo.helper
3
- const { download } = this.bajoExtra.helper
4
- const url = args[0]
5
- const spinText = 'Downloading file...'
6
- const spin = spinner({ showCounter: true }).start(spinText)
7
-
8
- let dest
9
- try {
10
- dest = await download(url, undefined, { spin, spinText })
11
- } catch (err) {
12
- spin.fatal('Error: %s', err.message)
13
- }
14
- spin.succeed('File saved as \'%s\'', dest)
15
- }
16
-
17
- export default download
package/bajoCli/tool.js DELETED
@@ -1 +0,0 @@
1
- export default 'defCliHandler'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes