@rxap/n8n-nodes-tar
Version:
130 lines • 5.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tar = void 0;
const tslib_1 = require("tslib");
const n8n_utilities_1 = require("@rxap/n8n-utilities");
const n8n_workflow_1 = require("n8n-workflow");
const path_1 = require("path");
const stream_1 = require("stream");
const tar_1 = require("tar");
const tmp_promise_1 = require("tmp-promise");
class Tar {
constructor() {
this.description = {
version: 1,
description: 'Interact with tar files',
defaults: { name: 'Tar' },
name: 'Tar',
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
displayName: 'Tar',
group: ['transform'],
icon: 'file:Tar.svg',
properties: [
{
name: 'operation',
displayName: 'Operation',
default: 'extract',
type: 'options',
required: true,
options: [
{
name: 'Extract',
value: 'extract',
},
],
},
{
name: 'inputDateProperty',
displayName: 'Input Date Property',
type: 'string',
default: 'data',
description: 'The name of the binary property which contains the zip file',
required: true,
},
{
name: 'globalFilePathPrefix',
displayName: 'Global File Path Prefix',
type: 'string',
default: '',
description: 'The prefix for the file path. If set, the file path will be prefixed with the prefix. If not set, the file path will be relative to the root of the zip file.',
required: false,
},
{
name: 'fileList',
displayName: 'List of files to extract',
type: 'string',
default: [],
required: true,
typeOptions: {
multipleValues: true,
},
}
],
};
}
execute() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const items = this.getInputData();
const results = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
const operation = this.getNodeParameter('operation', i);
const inputDateProperty = this.getNodeParameter('inputDateProperty', i);
const globalFilePathPrefix = this.getNodeParameter('globalFilePathPrefix', i);
const fileList = this.getNodeParameter('fileList', i, undefined);
let buffer;
const { path: workDir, cleanup, } = yield (0, tmp_promise_1.dir)({ unsafeCleanup: true });
try {
switch (operation) {
case 'extract': {
buffer = yield this.helpers.getBinaryDataBuffer(i, inputDateProperty);
const bufferStream = stream_1.Readable.from(buffer);
yield new Promise((resolve, reject) => {
let x;
if (fileList === null || fileList === void 0 ? void 0 : fileList.length) {
x = (0, tar_1.extract)({
cwd: workDir,
sync: false,
}, fileList.map(file => (0, path_1.join)(globalFilePathPrefix, file)));
}
else {
x = (0, tar_1.extract)({
cwd: workDir,
sync: false,
});
}
bufferStream
.pipe(x)
.on('error', reject)
.on('end', resolve);
});
break;
}
}
const result = {
json: item.json,
binary: {},
};
yield (0, n8n_utilities_1.addFilesToResults)((0, path_1.join)(workDir, globalFilePathPrefix), (0, path_1.join)(workDir, globalFilePathPrefix), result, this.helpers.prepareBinaryData.bind(this.helpers));
results[i] = result;
}
catch (e) {
results[i] = Object.assign(Object.assign({}, item), { error: new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to extract tar file. Error: ${e.message}`) });
}
finally {
yield cleanup();
}
}
return [results];
});
}
}
exports.Tar = Tar;
tslib_1.__decorate([
(0, n8n_utilities_1.CaptureExecutionError)(),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", []),
tslib_1.__metadata("design:returntype", Promise)
], Tar.prototype, "execute", null);
//# sourceMappingURL=Tar.node.js.map