@commercelayer/cli-plugin-imports
Advanced tools
Comparing version 4.0.0-rc.4 to 4.0.0-rc.5
@@ -19,5 +19,5 @@ import type { ImportCreate } from '@commercelayer/sdk'; | ||
}; | ||
declare const splitImports: (imp: ImportCreate, size?: number) => Chunk[]; | ||
declare const splitImports: (imp: ImportCreate, format: 'csv' | 'json', size?: number) => Chunk[]; | ||
declare const splitChunks: (chunks: Chunk[], size: number) => Batch[]; | ||
export { splitImports, splitChunks }; | ||
export type { Chunk, Batch }; |
@@ -5,3 +5,3 @@ "use strict"; | ||
const cli_core_1 = require("@commercelayer/cli-core"); | ||
const splitImports = (imp, size) => { | ||
const splitImports = (imp, format, size) => { | ||
const chunks = []; | ||
@@ -11,2 +11,3 @@ if (!imp?.inputs || (imp.inputs.length === 0)) | ||
const chunkSize = size || cli_core_1.clConfig.imports.max_size; | ||
const header = (format === 'csv') ? imp.inputs.shift() : undefined; | ||
const allInputs = imp.inputs; | ||
@@ -16,4 +17,9 @@ const totalItems = imp.inputs.length; | ||
let chunkNum = 0; | ||
while (allInputs.length > 0) | ||
while (allInputs.length > 0) { | ||
const inputs = allInputs.splice(0, chunkSize); | ||
const inputsCount = inputs.length; | ||
if ((format === 'csv') && header) | ||
inputs.unshift(header); | ||
chunks.push({ | ||
format, | ||
chunk_number: ++chunkNum, | ||
@@ -27,8 +33,9 @@ resource_type: imp.resource_type, | ||
total_items: totalItems, | ||
inputs: allInputs.splice(0, chunkSize), | ||
inputs, | ||
group_id: groupId, | ||
items_count: 0, | ||
items_count: inputsCount, | ||
total_batch_chunks: 0, | ||
total_batch_items: totalItems, | ||
total_batch_items: totalItems | ||
}); | ||
} | ||
return chunks.map(c => { | ||
@@ -38,3 +45,3 @@ c.start_item = ((c.chunk_number - 1) * chunkSize) + 1; | ||
c.total_chunks = chunks.length; | ||
c.items_count = c.inputs.length; | ||
// c.items_count = c.inputs.length | ||
return c; | ||
@@ -41,0 +48,0 @@ }); |
@@ -106,7 +106,8 @@ "use strict"; | ||
this.error(`Unsupported resource type: ${cli_core_1.clColor.style.error(type)}`); | ||
const format = flags.csv ? 'csv' : 'json'; | ||
const parentId = flags.parent; | ||
const inputFile = cli_core_1.clUtil.specialFolder(flags.inputs); | ||
const monitor = !flags.blind; | ||
const inputs = await (0, input_1.generateInputs)(inputFile, flags).catch(error => this.error(error.message)); | ||
const inputsLength = inputs.length; | ||
const inputs = await (0, input_1.generateInputs)(inputFile, format).catch(error => this.error(error.message)); | ||
const inputsLength = (format === 'csv') ? Math.max(0, inputs.length - 1) : inputs.length; | ||
// Check import size | ||
@@ -133,4 +134,4 @@ const humanized = type.replace(/_/g, ' '); | ||
// cleanup_records: flags.cleanup, | ||
inputs, | ||
}); | ||
inputs | ||
}, format); | ||
// Split chunks | ||
@@ -210,7 +211,9 @@ const batches = (0, chunk_1.splitChunks)(chunks, MAX_CHUNKS); | ||
async createImport(chunk) { | ||
const inputs = (chunk.format === 'json') ? chunk.inputs : chunk.inputs.join('\n'); // fix inputs resource type issue | ||
return this.cl.imports.create({ | ||
format: chunk.format, | ||
resource_type: chunk.resource_type, | ||
parent_resource_id: chunk.parent_resource_id, | ||
// cleanup_records: chunk.cleanup_records, | ||
inputs: chunk.inputs, | ||
inputs, | ||
reference: `${chunk.group_id}-${String(chunk.chunk_number).padStart(4, '0')}`, | ||
@@ -217,0 +220,0 @@ reference_origin: 'cli-plugin-imports', |
@@ -35,3 +35,3 @@ "use strict"; | ||
static args = { | ||
id: base_1.Args.string({ name: 'id', description: 'unique id of the import', required: true, hidden: false }), | ||
id: base_1.Args.string({ name: 'id', description: 'unique id of the import', required: true, hidden: false }) | ||
}; | ||
@@ -66,3 +66,3 @@ async run() { | ||
if (flags.inputs) { | ||
const inputs = (!imp.inputs && imp.attachment_url) ? await this.getInputs(imp.attachment_url) : imp.inputs; | ||
const inputs = (!imp.inputs && imp.attachment_url) ? await this.getInputs(imp) : imp.inputs; | ||
this.showInputs(inputs); | ||
@@ -80,5 +80,10 @@ if (flags['save-inputs'] && inputs) | ||
} | ||
async getInputs(attachmentUrl) { | ||
const inputs = await axios_1.default.get(attachmentUrl, { responseType: 'arraybuffer' }); | ||
return inputs ? JSON.parse((0, node_zlib_1.gunzipSync)(inputs.data).toString()) : []; | ||
async getInputs(imp) { | ||
if (!imp.attachment_url) | ||
return []; | ||
const inputs = await axios_1.default.get(imp.attachment_url, { responseType: 'arraybuffer' }); | ||
if (!inputs) | ||
return []; | ||
const unzipped = (0, node_zlib_1.gunzipSync)(inputs.data).toString(); | ||
return (imp.format === 'csv') ? unzipped.split('\n') : JSON.parse(unzipped); | ||
} | ||
@@ -85,0 +90,0 @@ saveInputs(flags, inputs) { |
@@ -1,2 +0,2 @@ | ||
declare const generateInputs: (filePath: string, flags?: any) => Promise<any[]>; | ||
declare const generateInputs: (filePath: string, fileFormat: 'csv' | 'json') => Promise<any[]>; | ||
export { generateInputs }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateInputs = void 0; | ||
const tslib_1 = require("tslib"); | ||
const node_fs_1 = require("node:fs"); | ||
const csv = tslib_1.__importStar(require("@fast-csv/parse")); | ||
const cli_core_1 = require("@commercelayer/cli-core"); | ||
const generateInputsCSV = async (filePath, delimiter) => { | ||
const inputs = []; | ||
const parseOptions = { headers: true, ignoreEmpty: true }; | ||
if (delimiter) | ||
parseOptions.delimiter = delimiter; | ||
return new Promise((resolve, reject) => { | ||
(0, node_fs_1.createReadStream)(filePath) | ||
.pipe(csv.parse(parseOptions)) | ||
.on('error', error => { reject(error); }) | ||
.on('data', row => inputs.push(row)) | ||
.on('end', (_rowCount) => { resolve(inputs); }); | ||
}); | ||
const generateInputsCSV = async (filePath) => { | ||
try { | ||
const data = (0, node_fs_1.readFileSync)(filePath, { encoding: 'utf-8' }); | ||
const csv = data.split('\n'); | ||
return Promise.resolve(csv); | ||
} | ||
catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
@@ -33,13 +28,11 @@ const generateInputJSON = async (filePath) => { | ||
}; | ||
const generateInputs = async (filePath, flags) => { | ||
const generateInputs = async (filePath, fileFormat) => { | ||
if (!(0, node_fs_1.existsSync)(filePath)) | ||
return Promise.reject(new Error('Unable to find file ' + cli_core_1.clColor.style.path(filePath))); | ||
if (flags?.csv) { | ||
let delimiter = flags.delimiter || ','; | ||
if (delimiter && (delimiter === 'TAB')) | ||
delimiter = '\t'; | ||
return generateInputsCSV(filePath, delimiter); | ||
switch (fileFormat) { | ||
case 'csv': return generateInputsCSV(filePath); | ||
case 'json': | ||
default: return generateInputJSON(filePath); | ||
} | ||
return generateInputJSON(filePath); | ||
}; | ||
exports.generateInputs = generateInputs; |
@@ -756,3 +756,3 @@ { | ||
}, | ||
"version": "4.0.0-rc.4" | ||
"version": "4.0.0-rc.5" | ||
} |
{ | ||
"name": "@commercelayer/cli-plugin-imports", | ||
"description": "Commerce Layer CLI Imports plugin", | ||
"version": "4.0.0-rc.4", | ||
"version": "4.0.0-rc.5", | ||
"author": "Pierluigi Viti <pierluigi@commercelayer.io>", | ||
@@ -56,6 +56,6 @@ "homepage": "https://github.com/commercelayer/commercelayer-cli-plugin-imports", | ||
"devDependencies": { | ||
"@commercelayer/cli-dev": "^3.0.2", | ||
"@commercelayer/cli-dev": "^3.0.3", | ||
"@commercelayer/eslint-config-ts": "^1.4.5", | ||
"@oclif/plugin-help": "^6.0.21", | ||
"@oclif/test": "^3.2.10", | ||
"@oclif/test": "^3.2.11", | ||
"@semantic-release/changelog": "^6.0.3", | ||
@@ -71,3 +71,3 @@ "@semantic-release/git": "^10.0.1", | ||
"nyc": "^15.1.0", | ||
"oclif": "^4.8.5", | ||
"oclif": "^4.8.8", | ||
"semantic-release": "^23.0.8", | ||
@@ -81,3 +81,3 @@ "tsx": "^4.7.2", | ||
"@fast-csv/parse": "^4.3.6", | ||
"@oclif/core": "^3.26.3", | ||
"@oclif/core": "^3.26.4", | ||
"axios": "^1.6.8", | ||
@@ -84,0 +84,0 @@ "cli-progress": "^3.12.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94726
2178
Updated@oclif/core@^3.26.4