closer-cli 0.5.5 → 1.0.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/cmds/importStock.js +213 -0
- package/helpers.js +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
const fs = require('fs')
|
|
5
|
+
const chalk = require('chalk')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
|
|
8
|
+
const Ora = require('ora')
|
|
9
|
+
const prompts = require('prompts')
|
|
10
|
+
const {
|
|
11
|
+
importCsvFromFile,
|
|
12
|
+
getExecutionTime,
|
|
13
|
+
callAfterDataImportProcess,
|
|
14
|
+
ALLOWED_CSV_FILES,
|
|
15
|
+
download,
|
|
16
|
+
validateCsvFromFile,
|
|
17
|
+
getArchiveFolder,
|
|
18
|
+
getGoogleSheetCsvUrl
|
|
19
|
+
} = require('../helpers')
|
|
20
|
+
|
|
21
|
+
exports.command = 'import:stock'
|
|
22
|
+
|
|
23
|
+
exports.desc = 'Import Stock Availability from CSV file'
|
|
24
|
+
|
|
25
|
+
exports.builder = yargs => {
|
|
26
|
+
yargs
|
|
27
|
+
.usage(`Usage: ${chalk.cyan('$0 import:stock')} [options]`)
|
|
28
|
+
.option('fromFile', {
|
|
29
|
+
alias: 'f',
|
|
30
|
+
describe: 'File to import',
|
|
31
|
+
type: 'string'
|
|
32
|
+
})
|
|
33
|
+
.option('fromUrl', {
|
|
34
|
+
alias: 'u',
|
|
35
|
+
describe: 'Remote URL file to import',
|
|
36
|
+
type: 'string'
|
|
37
|
+
})
|
|
38
|
+
.option('fromDir', {
|
|
39
|
+
alias: 'd',
|
|
40
|
+
describe: 'Directory path that contains file to import',
|
|
41
|
+
type: 'string'
|
|
42
|
+
})
|
|
43
|
+
.option('fromGoogleDocId', {
|
|
44
|
+
alias: 'g',
|
|
45
|
+
describe: 'Google Doc ID that contains sheet to import',
|
|
46
|
+
type: 'string'
|
|
47
|
+
})
|
|
48
|
+
// .conflicts('fromFile', 'fromUrl', 'fromDir', 'fromGoogleDocId')
|
|
49
|
+
.check(argv => {
|
|
50
|
+
if (!argv.fromFile && !argv.fromUrl && !argv.fromDir && !argv.fromGoogleDocId) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
'Missing required argument: fromFile OR fromUrl OR fromDir OR fromGoogleDocId'
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (argv.fromFile) {
|
|
57
|
+
fs.accessSync(argv.fromFile, fs.constants.F_OK)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (argv.fromDir) {
|
|
61
|
+
fs.accessSync(argv.fromDir, fs.constants.F_OK)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return true
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
exports.handler = async argv => {
|
|
69
|
+
// eslint-disable-next-line no-param-reassign
|
|
70
|
+
argv.out = argv.out || getArchiveFolder(argv.endpoint)
|
|
71
|
+
|
|
72
|
+
const spinner = new Ora()
|
|
73
|
+
const hrstart = process.hrtime()
|
|
74
|
+
|
|
75
|
+
const report = {
|
|
76
|
+
start: new Date(),
|
|
77
|
+
end: null,
|
|
78
|
+
time: null,
|
|
79
|
+
endpoint: argv.endpoint,
|
|
80
|
+
mode: null,
|
|
81
|
+
type: [],
|
|
82
|
+
result: []
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const ws = {
|
|
86
|
+
endpoint: argv.endpoint,
|
|
87
|
+
adminSecret: argv['admin-secret']
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const interactive = !argv.nonInteractive
|
|
91
|
+
|
|
92
|
+
const csvType = 'stockAvailability'
|
|
93
|
+
|
|
94
|
+
let file
|
|
95
|
+
if (argv.fromDir) {
|
|
96
|
+
file = {
|
|
97
|
+
file: path.resolve(argv.fromDir, ALLOWED_CSV_FILES.stockAvailability),
|
|
98
|
+
csvType
|
|
99
|
+
}
|
|
100
|
+
} else if (argv.fromGoogleDocId) {
|
|
101
|
+
file = {
|
|
102
|
+
url: getGoogleSheetCsvUrl(argv.fromGoogleDocId, csvType),
|
|
103
|
+
csvType
|
|
104
|
+
}
|
|
105
|
+
} else if (argv.fromFile) {
|
|
106
|
+
file = {
|
|
107
|
+
file: argv.fromFile,
|
|
108
|
+
csvType
|
|
109
|
+
}
|
|
110
|
+
} else if (argv.fromUrl) {
|
|
111
|
+
file = {
|
|
112
|
+
url: argv.fromUrl,
|
|
113
|
+
csvType
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let mode
|
|
118
|
+
if (interactive && !argv.mode) {
|
|
119
|
+
const answers = await prompts(
|
|
120
|
+
[
|
|
121
|
+
{
|
|
122
|
+
type: 'select',
|
|
123
|
+
name: 'mode',
|
|
124
|
+
message: 'Import Mode',
|
|
125
|
+
choices: [
|
|
126
|
+
{
|
|
127
|
+
title: 'Incremental',
|
|
128
|
+
value: 'incremental'
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
title: 'Full',
|
|
132
|
+
description: 'Before starting, all database records will be logically deleted',
|
|
133
|
+
value: 'full'
|
|
134
|
+
}
|
|
135
|
+
],
|
|
136
|
+
hint: ' '
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
{ onCancel: () => process.exit() }
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
mode = answers.mode
|
|
143
|
+
} else {
|
|
144
|
+
mode = argv.mode
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (interactive) {
|
|
148
|
+
console.log('')
|
|
149
|
+
const { confirmed } = await prompts(
|
|
150
|
+
[
|
|
151
|
+
{
|
|
152
|
+
type: 'confirm',
|
|
153
|
+
name: 'confirmed',
|
|
154
|
+
message: 'Continue?',
|
|
155
|
+
initial: false
|
|
156
|
+
}
|
|
157
|
+
],
|
|
158
|
+
{ onCancel: () => process.exit() }
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
if (!confirmed) {
|
|
162
|
+
process.exit()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.log('')
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let filepath = argv.fromFile
|
|
169
|
+
|
|
170
|
+
if (argv.fromUrl || argv.fromGoogleDocId) {
|
|
171
|
+
spinner.start('Downloading file...')
|
|
172
|
+
filepath = await download(file.url, argv.out, `${csvType}.csv`)
|
|
173
|
+
spinner.succeed('Downloaded file')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const validation = await validateCsvFromFile(filepath, csvType, ws, argv.csvDelimiter)
|
|
177
|
+
|
|
178
|
+
if (validation.error) {
|
|
179
|
+
console.log(`ERROR => [${chalk.red(validation.inputFile)}] ${validation.error}`)
|
|
180
|
+
process.exit()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const csvReport = await importCsvFromFile(
|
|
184
|
+
filepath,
|
|
185
|
+
csvType,
|
|
186
|
+
argv.out,
|
|
187
|
+
spinner,
|
|
188
|
+
ws,
|
|
189
|
+
argv.csvDelimiter
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
//
|
|
193
|
+
// Report
|
|
194
|
+
//
|
|
195
|
+
|
|
196
|
+
report.end = new Date()
|
|
197
|
+
report.time = getExecutionTime(hrstart)
|
|
198
|
+
report.mode = mode
|
|
199
|
+
report.type = [csvType]
|
|
200
|
+
|
|
201
|
+
const reportRows = []
|
|
202
|
+
|
|
203
|
+
reportRows.push({
|
|
204
|
+
type: 'import.csv',
|
|
205
|
+
payload: csvReport
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
report.result = reportRows
|
|
209
|
+
|
|
210
|
+
await callAfterDataImportProcess(spinner, ws, report)
|
|
211
|
+
|
|
212
|
+
process.exit()
|
|
213
|
+
}
|
package/helpers.js
CHANGED
|
@@ -31,6 +31,7 @@ exports.ALLOWED_CSV_FILES = {
|
|
|
31
31
|
label: 'labels.csv',
|
|
32
32
|
filterPosition: 'filterPosition.csv',
|
|
33
33
|
fieldConfiguration: 'fieldConfigurations.csv',
|
|
34
|
+
stockAvailability: 'stockAvailability.csv',
|
|
34
35
|
userCustomer: 'userCustomers.csv'
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -49,7 +50,6 @@ exports.importCsvFromFile = (file, csvType, outputDir, spinner, ws, delimiter =
|
|
|
49
50
|
let readStream = null
|
|
50
51
|
|
|
51
52
|
if (!fs.existsSync(outputDir)) {
|
|
52
|
-
// process.umask(0o000)
|
|
53
53
|
fs.mkdirSync(outputDir, { recursive: true })
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -59,15 +59,14 @@ exports.importCsvFromFile = (file, csvType, outputDir, spinner, ws, delimiter =
|
|
|
59
59
|
const skippedCsvFile = path.resolve(outputDir, `${csvType}_skipped.csv`)
|
|
60
60
|
const skippedFileStream = fs.createWriteStream(skippedCsvFile)
|
|
61
61
|
|
|
62
|
-
const filename = path.basename(file)
|
|
63
|
-
// const dirname = path.dirname(errorCsvFile)
|
|
64
|
-
|
|
65
62
|
let header = ''
|
|
66
63
|
let lineNumber = 0
|
|
67
64
|
let countImported = 0
|
|
68
65
|
let countFailed = 0
|
|
69
66
|
let countSkipped = 0
|
|
70
67
|
|
|
68
|
+
const filename = path.basename(file)
|
|
69
|
+
|
|
71
70
|
spinner.start('Loading...')
|
|
72
71
|
|
|
73
72
|
const importRowSuccess = () => {
|
|
@@ -420,6 +419,7 @@ exports.refreshOrders = (spinner, ws) =>
|
|
|
420
419
|
const { type, payload } = JSON.parse(packet)
|
|
421
420
|
switch (type) {
|
|
422
421
|
case 'refreshDraftOrdersResponse': {
|
|
422
|
+
console.log(JSON.stringify({ type, payload }))
|
|
423
423
|
spinner.text = this.printRefreshedOrders({
|
|
424
424
|
...payload,
|
|
425
425
|
executionTime: this.getExecutionTime(hrstart)
|