diff --git a/api/src/modules/import-data/file.service.ts b/api/src/modules/import-data/file.service.ts index 6ef8606d3..58b9e3aee 100644 --- a/api/src/modules/import-data/file.service.ts +++ b/api/src/modules/import-data/file.service.ts @@ -4,7 +4,7 @@ import * as XLSX from 'xlsx'; import { WorkBook } from 'xlsx'; import { difference } from 'lodash'; import { Worker } from 'worker_threads'; -import * as path from 'path'; +import { getWorkerConfig } from 'modules/import-data/utils'; @Injectable() export class FileService> { @@ -12,17 +12,13 @@ export class FileService> { async transformToJsonInWorker(filePath: string, sheetMap: any): Promise { await this.isFilePresentInFs(filePath); - const workerFile: string = - process.env.NODE_ENV === 'test' - ? path.resolve(__dirname, './workers/xlsx.worker.ts') // En tests usa el archivo TypeScript - : path.resolve(__dirname, './workers/xlsx.worker.js'); + const { workerPath, execArgv } = getWorkerConfig('xlsx.worker'); this.logger.log(`Starting worker to parse ${filePath}...`); try { const parsedSheet: any = await new Promise((resolve, reject) => { - const worker: Worker = new Worker(workerFile, { + const worker: Worker = new Worker(workerPath, { workerData: { filePath, sheetMap }, - execArgv: - process.env.NODE_ENV === 'test' ? ['-r', 'ts-node/register'] : [], + execArgv, }); worker.on('message', (data: T) => { diff --git a/api/src/modules/import-data/utils.ts b/api/src/modules/import-data/utils.ts new file mode 100644 index 000000000..ddb227739 --- /dev/null +++ b/api/src/modules/import-data/utils.ts @@ -0,0 +1,21 @@ +import * as path from 'path'; + +interface WorkerConfig { + workerPath: string; + execArgv: string[]; +} + +// Since node workers do not support ts out of the box, we need to specify the JS file for the app, but TS file for the tests. + +export const getWorkerConfig = (fileName: string): WorkerConfig => { + const isTestEnvironment: boolean = process.env.NODE_ENV === 'test'; + const workerPath: WorkerConfig['workerPath'] = isTestEnvironment + ? path.resolve(__dirname, `./workers/${fileName}.ts`) // En tests usa TypeScript + : path.resolve(__dirname, `./workers/${fileName}.js`); // En prod/dev usa JavaScript + + const execArgv: WorkerConfig['execArgv'] = isTestEnvironment + ? ['-r', 'ts-node/register'] + : []; + + return { workerPath, execArgv }; +};