bajo-extra 1.0.1 → 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/bajo/intl/en-US.json +17 -0
- package/bajo/intl/id.json +17 -0
- package/bajo/method/download.js +3 -3
- package/bajo/method/fetch-bulk.js +12 -12
- package/bajoCli/applet/download.js +3 -3
- package/package.json +1 -1
- package/bajoI18N/resource/id.json +0 -5
|
@@ -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
|
+
}
|
package/bajo/method/download.js
CHANGED
|
@@ -11,7 +11,7 @@ async function download (url, opts = {}, extra = {}) {
|
|
|
11
11
|
extra.dir = `${getPluginDataDir('bajoExtra')}/download`
|
|
12
12
|
fs.ensureDirSync(extra.dir)
|
|
13
13
|
}
|
|
14
|
-
if (!fs.existsSync(extra.dir)) throw this.error('
|
|
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 this.error('
|
|
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,7 +31,7 @@ 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 ?? '
|
|
34
|
+
extra.spinText = extra.spinText ?? 'downloading'
|
|
35
35
|
if (total === 0) extra.spin.setText(`${extra.spinText} %s`, this.formatByte(length))
|
|
36
36
|
else extra.spin.setText(`${extra.spinText} %s of %s (%s)`, this.formatByte(length), this.formatByte(total), this.formatPercentage(length / total))
|
|
37
37
|
}
|
|
@@ -4,7 +4,7 @@ async function fetching ({ url, opts, bulk, spin }) {
|
|
|
4
4
|
const { validationErrorMessage } = this.app.bajoDb
|
|
5
5
|
const resp = await this.fetch(url, opts ?? {})
|
|
6
6
|
if (isEmpty(resp)) {
|
|
7
|
-
spin.fatal('
|
|
7
|
+
spin.fatal('noServerResponse')
|
|
8
8
|
return -1
|
|
9
9
|
}
|
|
10
10
|
if (bulk.abort) {
|
|
@@ -20,10 +20,10 @@ async function fetching ({ url, opts, bulk, spin }) {
|
|
|
20
20
|
if (bulk.printCount === true) bulk.printCount = 100
|
|
21
21
|
const data = isFunction(bulk.dataKey) ? await bulk.dataKey.call(this, resp) : resp[bulk.dataKey]
|
|
22
22
|
if (data.length === 0) {
|
|
23
|
-
print.warn('
|
|
23
|
+
print.warn('noRecordToProcess')
|
|
24
24
|
return 0
|
|
25
25
|
}
|
|
26
|
-
spin.setText('
|
|
26
|
+
spin.setText('gotRecordsProcessing%d', data.length)
|
|
27
27
|
for (let r of data) {
|
|
28
28
|
await setImmediate()
|
|
29
29
|
if (bulk.converter) r = await bulk.converter.call(this, r, bulk)
|
|
@@ -35,15 +35,15 @@ async function fetching ({ url, opts, bulk, spin }) {
|
|
|
35
35
|
const result = await bulk.handler.call(this, r, bulk)
|
|
36
36
|
if (result && has(stat, result)) stat[result]++
|
|
37
37
|
if (bulk.printCount && bulk.printCount < count && (count % bulk.printCount === 0)) print.succeed('[%s] Processed %d/%d', spin.getElapsed(), count, data.length)
|
|
38
|
-
else if (!spin.opts.isLog) spin.setText('
|
|
38
|
+
else if (!spin.opts.isLog) spin.setText('rec%d%d', count, data.length)
|
|
39
39
|
count++
|
|
40
40
|
} catch (err) {
|
|
41
41
|
console.log(err)
|
|
42
42
|
spin.setText(validationErrorMessage(err) + ', continue')
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
print.succeed('
|
|
46
|
-
if (!bulk.noStat) print.succeed('
|
|
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)
|
|
47
47
|
return data.length
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -51,24 +51,24 @@ async function fetchBulk (url, bulk = {}, opts = {}) {
|
|
|
51
51
|
const { isFunction } = this.bajo.lib._
|
|
52
52
|
opts.params = opts.params ?? {}
|
|
53
53
|
bulk.maxStep = bulk.maxStep ?? 0
|
|
54
|
-
if (!isFunction(bulk.handler)) throw this.error('
|
|
54
|
+
if (!isFunction(bulk.handler)) throw this.error('handlerMustBeProvided')
|
|
55
55
|
if (isFunction(bulk.paramsIncFn)) {
|
|
56
|
-
this.print.info('
|
|
57
|
-
const spin = this.print.spinner({ showCounter: true }).start('
|
|
56
|
+
this.print.info('bulkFetchStarting')
|
|
57
|
+
const spin = this.print.spinner({ showCounter: true }).start('fetchingStarts')
|
|
58
58
|
let step = 1
|
|
59
59
|
for (;;) {
|
|
60
|
-
this.print.info('
|
|
60
|
+
this.print.info('batch%s%d', spin.getElapsed(), step)
|
|
61
61
|
const newOpts = await bulk.paramsIncFn.call(this, { url, bulk, opts })
|
|
62
62
|
if (newOpts) opts = newOpts
|
|
63
63
|
const length = await fetching.call(this, { url, bulk, opts, spin })
|
|
64
64
|
if (length === 0 || (bulk.maxStep > 0 && step >= bulk.maxStep)) {
|
|
65
|
-
this.print.info('
|
|
65
|
+
this.print.info('allDone')
|
|
66
66
|
break
|
|
67
67
|
}
|
|
68
68
|
step++
|
|
69
69
|
}
|
|
70
70
|
} else {
|
|
71
|
-
const spin = this.print.spinner({ showCounter: true }).start('
|
|
71
|
+
const spin = this.print.spinner({ showCounter: true }).start('fetchingStarts')
|
|
72
72
|
await fetching.call(this, { url, bulk, opts, spin })
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
async function download (...args) {
|
|
2
2
|
const [url] = args
|
|
3
|
-
const spinText = '
|
|
3
|
+
const spinText = this.print.write('downloading%s', this.print.write('file'))
|
|
4
4
|
const spin = this.print.spinner({ showCounter: true }).start(spinText)
|
|
5
5
|
|
|
6
6
|
let dest
|
|
7
7
|
try {
|
|
8
8
|
dest = await this.download(url, undefined, { spin, spinText })
|
|
9
9
|
} catch (err) {
|
|
10
|
-
spin.fatal('
|
|
10
|
+
spin.fatal('error%s', err.message)
|
|
11
11
|
}
|
|
12
|
-
spin.succeed('
|
|
12
|
+
spin.succeed('savedAs%s%s', this.print.write('file'), dest)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export default download
|
package/package.json
CHANGED