diff --git a/src/base.ts b/src/base.ts index 4bea26d..22b9676 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,14 +1,15 @@ import { clColor, clToken, clUpdate, clFilter, clOutput, clUtil, clApi } from '@commercelayer/cli-core' import type { ApiMode, KeyValRel, KeyValString } from '@commercelayer/cli-core' import { Command, Flags, Args } from '@oclif/core' -import { existsSync, readFileSync } from 'fs' +import { existsSync, readFileSync, writeFileSync } from 'fs' import axios from 'axios' import { type InputType, gunzipSync } from 'zlib' -import commercelayer, { type CommerceLayerClient, CommerceLayerStatic } from '@commercelayer/sdk' +import commercelayer, { type CommerceLayerClient, CommerceLayerStatic, type Export } from '@commercelayer/sdk' import { rename } from 'fs/promises' import type { CommandError } from '@oclif/core/lib/interfaces' import notifier from 'node-notifier' import * as cliux from '@commercelayer/cli-ux' +import { join } from 'path' const pkg: clUpdate.Package = require('../package.json') @@ -177,6 +178,24 @@ export abstract class ExportCommand extends BaseCommand { } + protected async singleExportFile(exp: Export, flags: any): Promise { + + const tmpDir = this.config.cacheDir + const format = this.getFileFormat(flags) + + // Export just completed, no need to refresh attachment url + const fileExport = await this.getExportedFile(exp.attachment_url, flags) + + const tmpFile = join(tmpDir, `${exp.id}-tmp.${format}`) + + writeFileSync(tmpFile, fileExport, { encoding }) + if (flags.keep) writeFileSync(tmpFile.replace('-tmp', ''), fileExport, { encoding }) + + return tmpFile + + } + + protected async saveOutput(tempFile: string, flags: any): Promise { try { diff --git a/src/commands/exports/all.ts b/src/commands/exports/all.ts index 069a64a..811e724 100644 --- a/src/commands/exports/all.ts +++ b/src/commands/exports/all.ts @@ -436,24 +436,6 @@ export default class ExportsAll extends ExportCommand { } - private async singleExportFile(exp: Export, flags: any): Promise { - - const tmpDir = this.config.cacheDir - const format = this.getFileFormat(flags) - - // Export just completed, no need to refresh attachment url - const fileExport = await this.getExportedFile(exp.attachment_url, flags) - - const tmpFile = join(tmpDir, `${exp.id}-tmp.${format}`) - - writeFileSync(tmpFile, fileExport, { encoding }) - if (flags.keep) writeFileSync(tmpFile.replace('-tmp', ''), fileExport, { encoding }) - - return tmpFile - - } - - private async mergeExportFiles(exports: Export[], flags: any): Promise { const tmpDir = this.config.cacheDir diff --git a/src/commands/exports/create.ts b/src/commands/exports/create.ts index 11560c6..2e60521 100644 --- a/src/commands/exports/create.ts +++ b/src/commands/exports/create.ts @@ -152,7 +152,8 @@ export default class ExportsCreate extends ExportCommand { if (exp.status === 'completed') this.log(`\nExported ${clColor.yellowBright(exp.records_count || 0)} ${resDesc}`) else this.error(`Export ${exp?.id} ended with errors`) - const outputFile = await this.saveOutput(exp, flags) + const tmpOutputFile = await this.singleExportFile(exp, flags) + const outputFile = await this.saveOutput(tmpOutputFile, flags) // Notification const finishMessage = `Export of ${exp.records_count} ${resDesc} is finished!`