bajo-extra 0.2.17 → 0.3.0
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/bajo/helper/download.js +3 -3
- package/bajo/helper/export-to.js +4 -13
- package/bajo/helper/fetch-and-save.js +3 -4
- package/bajo/helper/fetch-bulk.js +4 -4
- package/bajo/helper/fetch.js +2 -2
- package/bajo/helper/gzip.js +12 -15
- package/bajo/helper/import-from.js +2 -3
- package/bajoCli/tool/export-to.js +2 -2
- package/bajoCli/tool/import-from.js +3 -3
- package/package.json +1 -1
package/bajo/helper/download.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
|
|
3
3
|
async function download (url, opts = {}) {
|
|
4
|
-
const { getPluginDataDir, importPkg, error, generateId } = this.bajo.helper
|
|
4
|
+
const { fs, getPluginDataDir, importPkg, error, generateId } = this.bajo.helper
|
|
5
5
|
const { fetch, formatByte, formatPercentage } = this.bajoExtra.helper
|
|
6
|
-
const { isFunction } =
|
|
6
|
+
const { isFunction } = this.bajo.helper._
|
|
7
7
|
if (typeof opts === 'string') opts = { dir: opts }
|
|
8
|
-
const
|
|
8
|
+
const increment = await importPkg('add-filename-increment')
|
|
9
9
|
if (!opts.dir) {
|
|
10
10
|
opts.dir = `${getPluginDataDir('bajoExtra')}/download`
|
|
11
11
|
fs.ensureDirSync(opts.dir)
|
package/bajo/helper/export-to.js
CHANGED
|
@@ -9,8 +9,8 @@ const { DataStream } = scramjet
|
|
|
9
9
|
const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx', '.tsv']
|
|
10
10
|
|
|
11
11
|
async function getFile (dest, ensureDir) {
|
|
12
|
-
const { importPkg, error, getPluginDataDir } = this.bajo.helper
|
|
13
|
-
const
|
|
12
|
+
const { fs, importPkg, error, getPluginDataDir } = this.bajo.helper
|
|
13
|
+
const increment = await importPkg('add-filename-increment')
|
|
14
14
|
let file
|
|
15
15
|
if (path.isAbsolute(dest)) file = dest
|
|
16
16
|
else {
|
|
@@ -51,7 +51,7 @@ async function getData ({ source, filter, count, stream, progressFn }) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, batch = 500, progressFn } = {}, opts = {}) {
|
|
54
|
-
const {
|
|
54
|
+
const { fs, error, getConfig } = this.bajo.helper
|
|
55
55
|
const cfg = getConfig('bajoExtra')
|
|
56
56
|
if (!this.bajoDb) throw error('Bajo DB isn\'t loaded')
|
|
57
57
|
filter.page = 1
|
|
@@ -59,27 +59,18 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
|
|
|
59
59
|
if (batch > cfg.stream.export.maxBatch) batch = cfg.stream.export.maxBatch
|
|
60
60
|
if (batch < 0) batch = 1
|
|
61
61
|
filter.limit = batch
|
|
62
|
+
const { merge } = this.bajo.helper._
|
|
62
63
|
|
|
63
64
|
return new Promise((resolve, reject) => {
|
|
64
65
|
const { getInfo } = this.bajoDb.helper
|
|
65
66
|
let count = 0
|
|
66
|
-
let fs
|
|
67
|
-
let merge
|
|
68
67
|
let file
|
|
69
68
|
let ext
|
|
70
69
|
let stream
|
|
71
70
|
let compress
|
|
72
71
|
let writer
|
|
73
72
|
getInfo(source)
|
|
74
|
-
.then(() => {
|
|
75
|
-
return importPkg('lodash-es')
|
|
76
|
-
})
|
|
77
|
-
.then(l => {
|
|
78
|
-
merge = l.merge
|
|
79
|
-
return importPkg('fs-extra')
|
|
80
|
-
})
|
|
81
73
|
.then(res => {
|
|
82
|
-
fs = res
|
|
83
74
|
return getFile.call(this, dest, ensureDir)
|
|
84
75
|
})
|
|
85
76
|
.then(res => {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
async function handler (rec, bulk) {
|
|
2
|
-
const {
|
|
3
|
-
const { isFunction, set } = await importPkg('lodash-es')
|
|
2
|
+
const { isFunction, set } = this.bajo.helper._
|
|
4
3
|
const { recordCreate, recordFind, recordUpdate } = this.bajoDb.helper
|
|
5
4
|
const save = bulk.save ?? {}
|
|
6
5
|
const current = save.current ?? {}
|
|
@@ -52,9 +51,9 @@ async function handler (rec, bulk) {
|
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
async function fetchAndSave ({ url, bulk, save = {}, opts = {} } = {}) {
|
|
55
|
-
const {
|
|
54
|
+
const { getConfig, importModule } = this.bajo.helper
|
|
56
55
|
const { fetchBulk } = this.bajoExtra.helper
|
|
57
|
-
const { merge } =
|
|
56
|
+
const { merge } = this.bajo.helper._
|
|
58
57
|
merge(bulk, { handler, save })
|
|
59
58
|
const cfgDb = getConfig('bajoDb', { full: true })
|
|
60
59
|
const start = await importModule(`${cfgDb.dir.pkg}/bajo/start.js`)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
async function fetching ({ url, opts, bulk, spin }) {
|
|
2
|
-
const { setImmediate,
|
|
3
|
-
const { isEmpty, isFunction, has } =
|
|
2
|
+
const { setImmediate, print } = this.bajo.helper
|
|
3
|
+
const { isEmpty, isFunction, has } = this.bajo.helper._
|
|
4
4
|
const { validationErrorMessage } = this.bajoDb.helper
|
|
5
5
|
const { fetch } = this.bajoExtra.helper
|
|
6
6
|
const resp = await fetch(url, opts ?? {})
|
|
@@ -49,8 +49,8 @@ async function fetching ({ url, opts, bulk, spin }) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
async function fetchBulk (url, bulk = {}, opts = {}) {
|
|
52
|
-
const { print, spinner,
|
|
53
|
-
const { isFunction } =
|
|
52
|
+
const { print, spinner, error } = this.bajo.helper
|
|
53
|
+
const { isFunction } = this.bajo.helper._
|
|
54
54
|
opts.params = opts.params ?? {}
|
|
55
55
|
bulk.maxStep = bulk.maxStep ?? 0
|
|
56
56
|
if (!isFunction(bulk.handler)) throw error('A function handler must be provided')
|
package/bajo/helper/fetch.js
CHANGED
|
@@ -3,8 +3,8 @@ import http from 'http'
|
|
|
3
3
|
import https from 'https'
|
|
4
4
|
|
|
5
5
|
async function fetch (url, opts = {}, ext = {}) {
|
|
6
|
-
const {
|
|
7
|
-
const { has, isPlainObject, cloneDeep, isEmpty } =
|
|
6
|
+
const { getConfig } = this.bajo.helper
|
|
7
|
+
const { has, isPlainObject, cloneDeep, isEmpty } = this.bajo.helper._
|
|
8
8
|
const cfg = getConfig('bajoExtra')
|
|
9
9
|
if (isPlainObject(url)) {
|
|
10
10
|
ext = cloneDeep(opts)
|
package/bajo/helper/gzip.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
import { createGzip, createGunzip } from 'zlib'
|
|
2
2
|
|
|
3
3
|
function gzip (file, deleteOld, expand) {
|
|
4
|
+
const { fs } = this.bajo.helper
|
|
4
5
|
return new Promise((resolve, reject) => {
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (deleteOld) fs.unlinkSync(file)
|
|
17
|
-
resolve()
|
|
18
|
-
})
|
|
19
|
-
})
|
|
6
|
+
const newFile = expand ? file.slice(0, file.length - 3) : (file + '.gz')
|
|
7
|
+
const reader = fs.createReadStream(file)
|
|
8
|
+
const writer = fs.createWriteStream(newFile)
|
|
9
|
+
const method = expand ? createGunzip() : createGzip()
|
|
10
|
+
reader.pipe(method).pipe(writer)
|
|
11
|
+
writer.on('error', reject)
|
|
12
|
+
writer.on('finish', err => {
|
|
13
|
+
if (err) return reject(err)
|
|
14
|
+
if (deleteOld) fs.unlinkSync(file)
|
|
15
|
+
resolve()
|
|
16
|
+
})
|
|
20
17
|
})
|
|
21
18
|
}
|
|
22
19
|
|
|
@@ -8,13 +8,12 @@ const { DataStream } = scramjet
|
|
|
8
8
|
const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx', '.tsv']
|
|
9
9
|
|
|
10
10
|
async function importFrom (source, dest, { trashOld = true, batch = 1, progressFn, converterFn, useHeader = true, fileType, createOpts = {} } = {}, opts = {}) {
|
|
11
|
-
const {
|
|
11
|
+
const { fs, error, getConfig, getPluginDataDir } = this.bajo.helper
|
|
12
12
|
if (dest !== false) {
|
|
13
13
|
if (!this.bajoDb) throw error('Bajo DB isn\'t loaded')
|
|
14
14
|
await this.bajoDb.helper.getInfo(dest)
|
|
15
15
|
}
|
|
16
|
-
const { merge } =
|
|
17
|
-
const fs = await importPkg('fs-extra')
|
|
16
|
+
const { merge } = this.bajo.helper._
|
|
18
17
|
const cfg = getConfig('bajoExtra')
|
|
19
18
|
|
|
20
19
|
let file
|
|
@@ -12,8 +12,8 @@ function makeProgress (spin) {
|
|
|
12
12
|
|
|
13
13
|
async function exportTo ({ path, args }) {
|
|
14
14
|
const { importPkg, print, dayjs, getConfig, importModule, spinner } = this.bajo.helper
|
|
15
|
-
const { isEmpty, map } =
|
|
16
|
-
const [input, select] = await importPkg('
|
|
15
|
+
const { isEmpty, map } = this.bajo.helper._
|
|
16
|
+
const [input, select] = await importPkg('bajoCli:@inquirer/input',
|
|
17
17
|
'bajo-cli:@inquirer/select')
|
|
18
18
|
const config = getConfig()
|
|
19
19
|
if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
|
|
@@ -11,9 +11,9 @@ function makeProgress (spin) {
|
|
|
11
11
|
|
|
12
12
|
async function importFrom ({ path, args }) {
|
|
13
13
|
const { importPkg, print, importModule, getConfig, spinner } = this.bajo.helper
|
|
14
|
-
const { isEmpty, map } =
|
|
15
|
-
const [input, select, confirm] = await importPkg('
|
|
16
|
-
'
|
|
14
|
+
const { isEmpty, map } = this.bajo.helper._
|
|
15
|
+
const [input, select, confirm] = await importPkg('bajoCli:@inquirer/input',
|
|
16
|
+
'bajoCli:@inquirer/select', 'bajoCli:@inquirer/confirm')
|
|
17
17
|
const config = getConfig()
|
|
18
18
|
if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
|
|
19
19
|
const schemas = map(this.bajoDb.schemas, 'name')
|