@microsoft/azure-data-factory-utilities
Version:
Azure Data Factory utilities library
81 lines (80 loc) • 3.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BundleManager = void 0;
var fs = require("fs");
var https = require("https");
var path = require("path");
var child_process_1 = require("child_process");
var utils_1 = require("./utils");
var BundleManager = /** @class */ (function () {
function BundleManager(_bundleUrl, _bundlePath, _nodeOptions, _preview) {
if (_bundleUrl === void 0) { _bundleUrl = BundleManager.defaultBundleUrl; }
this._bundleUrl = _bundleUrl;
this._bundlePath = _bundlePath;
this._nodeOptions = _nodeOptions;
this._preview = _preview;
if (_preview) {
this._preview = '--preview';
}
}
BundleManager.prototype.invokeBundle = function (args) {
var _this = this;
if (this._bundlePath) {
(0, utils_1.printToConsole)("Using downloaded bundle from path: ".concat(this._bundlePath));
this._runCommand(this._bundlePath, args);
return;
}
if (!fs.existsSync(BundleManager.defaultBundleDir)) {
fs.mkdirSync(BundleManager.defaultBundleDir);
}
var file = fs.createWriteStream(BundleManager.defaultBundleFilePath);
(0, utils_1.printToConsole)("Downloading bundle from: ".concat(this._bundleUrl));
(0, utils_1.printToConsole)("Process cwd: ".concat(process.cwd()));
https.get(this._bundleUrl, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close();
(0, utils_1.printToConsole)("Bundle downloaded successfully, saved in: ".concat(BundleManager.defaultBundleFilePath));
_this._runCommand(BundleManager.defaultBundleFilePath, args);
});
});
};
BundleManager.prototype._runCommand = function (bundlePath, args) {
var argsToInsert = args.join(' ');
var escapedBundleFilePath = (0, utils_1.escapeArgIfNeeded)(bundlePath);
(0, utils_1.printToConsole)('Executing bundle...');
if (this._nodeOptions) {
(0, utils_1.printToConsole)("Using the following node options: ".concat(this._nodeOptions));
}
if (this._preview) {
(0, utils_1.printToConsole)('Using the option: ' + this._preview);
}
(0, utils_1.printToConsole)("Inserting the following arguments: ".concat(argsToInsert));
var execCommand = ['node', this._nodeOptions, escapedBundleFilePath, argsToInsert, this._preview].join(' ');
return this._execShellCommand(execCommand);
};
BundleManager.prototype._execShellCommand = function (cmd) {
return new Promise(function (resolve) {
(0, utils_1.printToConsole)("Executing bundle file, full command:\n\n".concat(cmd, "\n\n"));
var bundleProcess = (0, child_process_1.exec)(cmd, function (error, stdout) {
(0, utils_1.printToConsole)(stdout);
if (error) {
(0, utils_1.printToConsole)("\n=====ERROR=====\n".concat(error.toString()));
}
(0, utils_1.printToConsole)("Execution finished....");
resolve();
});
bundleProcess.on('close', function (exitCode) {
if (exitCode !== 0) {
throw "Execution failed with exit code: ".concat(exitCode);
}
});
});
};
BundleManager.defaultBundleUrl = 'https://adf.azure.com/assets/cmd-api/main.js';
BundleManager.defaultBundleDir = 'downloads';
BundleManager.defaultBundleName = 'main.js';
BundleManager.defaultBundleFilePath = path.join(process.cwd(), BundleManager.defaultBundleDir, BundleManager.defaultBundleName);
return BundleManager;
}());
exports.BundleManager = BundleManager;
;