@crestron/ch5-shell-utilities-cli
Version:
CH5 Shell Utilities CLI for command scripts
234 lines (233 loc) • 12.7 kB
JavaScript
"use strict";
// Copyright (C) 2021 to the present, Crestron Electronics, Inc.
// All rights reserved.
// No part of this software may be reproduced in any form, machine
// or natural, without the express written consent of Crestron Electronics.
// Use of this source code is subject to the terms of the Crestron Software License Agreement
// under which you licensed this source code.
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ch5ExportProjectCli = void 0;
var Ch5BaseClassForCli_1 = require("../Ch5BaseClassForCli");
var fs = require("fs");
var zl = require("zip-lib");
var fsExtra = require("fs-extra");
var path = require('path');
var Ch5ExportProjectCli = /** @class */ (function (_super) {
__extends(Ch5ExportProjectCli, _super);
/*
- "ignoreFilesFolders": Indicates all the files / folders that must not be included in the zip file.
- "zipFileDestinationPath": "./dist/", - This is the path where the zip file will be created. Ensure that it starts with './' and ends with '/'.
*/
function Ch5ExportProjectCli(showOutputMessages) {
if (showOutputMessages === void 0) { showOutputMessages = true; }
var _this = _super.call(this, "export-project") || this;
_this.showOutputMessages = showOutputMessages;
return _this;
}
/**
* Method for exporting project
*/
Ch5ExportProjectCli.prototype.run = function () {
return __awaiter(this, void 0, void 0, function () {
var packageJson, fileName, completeFileName, folderPathName, folderPathActual, excludedFiles, includedFiles, output;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.checkVersionToExecute();
packageJson = JSON.parse(JSON.stringify(fsExtra.readJSONSync("./package.json")));
fileName = this.namingHelper.removeAllSpaces(String(packageJson.name).trim());
this.logger.log("File Name: " + fileName);
completeFileName = path.join(this.getConfigNode("zipFileDestinationPath"), fileName + ".zip");
this.logger.log("Complete File Name: " + completeFileName);
return [4 /*yield*/, this.utils.deleteFile(completeFileName)];
case 1:
_a.sent();
folderPathName = packageJson.name + "-Code-Folder-Temp";
folderPathActual = this.createTempFolder(folderPathName);
this.logger.log("Temp Folder Path: " + folderPathActual);
excludedFiles = this.getConfigNode("ignoreFilesFolders");
excludedFiles.push(folderPathName);
includedFiles = packageJson.files;
includedFiles.push("./");
return [4 /*yield*/, this.copyFiles(folderPathActual, excludedFiles, includedFiles, completeFileName)];
case 2:
output = _a.sent();
return [2 /*return*/, output];
}
});
});
};
/**
* Copy Files
* @param {*} folderPathActual
* @param {*} excludedFiles
* @param {*} includedFiles
* @param {*} completeFileName
*/
Ch5ExportProjectCli.prototype.copyFiles = function (folderPathActual, excludedFiles, includedFiles, completeFileName) {
return __awaiter(this, void 0, void 0, function () {
var out, e_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, fsExtra.copy("./", folderPathActual, {
filter: function (path) {
var isValidOutput = true;
for (var i = 0; i < excludedFiles.length; i++) {
if (path.indexOf(excludedFiles[i]) > -1) {
_this.logger.log("excluded path: ", path);
isValidOutput = false;
}
}
if (isValidOutput === true) {
isValidOutput = false;
for (var i = 0; i < includedFiles.length; i++) {
var pathCheck = _this.utils.replaceAll(includedFiles[i], "/", "");
pathCheck = _this.utils.replaceAll(pathCheck, "*", "");
if (path.indexOf(pathCheck) > -1) {
isValidOutput = true;
break;
}
}
}
return isValidOutput;
}
}).then(function () { return __awaiter(_this, void 0, void 0, function () {
var output;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.logger.info("Copy Done.");
return [4 /*yield*/, this.zipFiles(folderPathActual, completeFileName)];
case 1:
output = _a.sent();
this.logger.info("output.", output);
return [2 /*return*/, output];
}
});
}); }).catch(function (err) {
_this.logger.error(err);
return false;
})];
case 1:
out = _a.sent();
return [2 /*return*/, out];
case 2:
e_1 = _a.sent();
this.logger.error(e_1);
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
});
};
/**
* Zip files
* @param {*} folderPathActual
* @param {*} completeFileName
*/
Ch5ExportProjectCli.prototype.zipFiles = function (folderPathActual, completeFileName) {
return __awaiter(this, void 0, void 0, function () {
var output;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, zl.archiveFolder(folderPathActual, completeFileName).then(function () { return __awaiter(_this, void 0, void 0, function () {
var cleanupPath;
return __generator(this, function (_a) {
this.logger.info("Zip Done.");
try {
cleanupPath = this.getConfigNode("temporaryFilePath");
if (cleanupPath === this.getConfigNode("zipFileDestinationPath")) {
cleanupPath = folderPathActual;
}
this.utils.deleteFolder(cleanupPath);
this.logger.info("Clean up Done for the path: " + cleanupPath);
if (this.showOutputMessages === true) {
this.logger.printSuccess(this.getText("SUCCESS_MESSAGE", completeFileName));
}
return [2 /*return*/, true];
}
catch (e) {
this.logger.log(e);
return [2 /*return*/, false];
}
return [2 /*return*/];
});
}); }, function (err) {
_this.logger.log(err);
return false;
})];
case 1:
output = _a.sent();
return [2 /*return*/, output];
}
});
});
};
/**
* Create Temporary Folder for copy files before zipping
*/
Ch5ExportProjectCli.prototype.createTempFolder = function (folderPathName) {
var folderPathActual = this.getConfigNode("temporaryFilePath") + folderPathName;
fs.mkdirSync(folderPathActual, {
recursive: true,
});
return folderPathActual;
};
return Ch5ExportProjectCli;
}(Ch5BaseClassForCli_1.Ch5BaseClassForCli));
exports.Ch5ExportProjectCli = Ch5ExportProjectCli;