@crestron/ch5-shell-utilities-cli
Version:
CH5 Shell Utilities CLI for command scripts
339 lines (338 loc) • 17.3 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 __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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.Ch5UpgradeProjectCli = void 0;
var Ch5BaseClassForCliUpgrade_1 = require("../Ch5BaseClassForCliUpgrade");
var Ch5CliProjectConfig_1 = require("../Ch5CliProjectConfig");
var https = require('https');
var zl = require("zip-lib");
var path = require('path');
var fs = require("fs");
var fsExtra = require("fs-extra");
var editJsonFile = require("edit-json-file");
var Ch5UpgradeProjectCli = /** @class */ (function (_super) {
__extends(Ch5UpgradeProjectCli, _super);
/**
* Constructor
*/
function Ch5UpgradeProjectCli(showOutputMessages) {
if (showOutputMessages === void 0) { showOutputMessages = true; }
var _this = _super.call(this, "upgrade-project") || this;
_this.showOutputMessages = showOutputMessages;
_this.SHELL_FOLDER = path.normalize(path.join(__dirname, "../../", "shell"));
_this.PROJECT_CONFIG_JSON_PATH = path.normalize("/app/project-config.json");
_this.temporaryPath = './temp';
_this.node_modules = './node_modules';
_this.templatePath = './app/template';
_this.assetsPath = './app/project/assets';
_this.contractPath = './config/contract.cse2j';
_this.vscodePath = './.vscode';
_this.licensePath = './LICENSE.txt';
_this.copyrightPath = './copyright.txt';
_this.packagePath = './package.json';
_this.readmePath = './README.md';
_this.packageLockPath = './package-lock.json';
_this.appConfigPath = './app.config.js';
_this.webpackCommonPath = './webpack.common.js';
_this.webpackDevPath = './webpack.dev.js';
_this.webpackProdPath = './webpack.prod.js';
_this.indexHtmlPath = './app/index.html';
_this.oldShellUtilitiesPath = './shell-utilities';
_this.hardButtonsPath = './app/hard-buttons.json';
_this.cliProjectConfig = new Ch5CliProjectConfig_1.Ch5CliProjectConfig();
return _this;
}
/**
* Implement this component's main purpose
*/
Ch5UpgradeProjectCli.prototype.processRequest = function () {
return __awaiter(this, void 0, void 0, function () {
var err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.logger.start("processRequest");
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
// STEP 1: Update new project-config.json
this.processProjectConfig();
this.processThemesDifferences();
this.processHeaderDifferences();
this.processPagesDifferences();
// STEP 2: update directories
return [4 /*yield*/, this.processDirectories()];
case 2:
// STEP 2: update directories
_a.sent();
this.logger.printSuccess(this.getText('SUCCESS_MESSAGE'));
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
if (err_1.message.toLowerCase().includes('no such file')) {
this.logger.printWarning(this.getText("INVALID_STRUCTURE.MISSING_FILE"));
}
else if (err_1.message.toLowerCase().includes('operation not permitted')) {
this.logger.printWarning(this.getText("INVALID_STRUCTURE.INVALID_PERMISSIONS"));
}
return [3 /*break*/, 4];
case 4:
this.logger.end();
return [2 /*return*/];
}
});
});
};
Ch5UpgradeProjectCli.prototype.processDirectoriesOld = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var v2URL = 'https://siproducts.blob.core.windows.net/ch5-test/05042022/Ch5ShellTemplate-2.0.26-alpha.27.zip';
https.get(v2URL, function (res) {
// create temporary folder
fs.mkdirSync("".concat(_this.temporaryPath));
var filePath = fs.createWriteStream("".concat(_this.temporaryPath, "/v2.zip"));
res.pipe(filePath);
filePath.on('finish', function () {
// download file to V2.zip
filePath.close();
// extract v2.zip to v2
zl.extract("".concat(_this.temporaryPath, "/v2.zip"), "".concat(_this.temporaryPath, "/v2")).then(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
try {
this.processCode();
resolve(true);
}
catch (err) {
reject(err);
}
return [2 /*return*/];
});
}); }, function (err) {
reject(err);
});
});
});
});
};
Ch5UpgradeProjectCli.prototype.processProjectConfig = function () {
var jsonProjectConfig = this.cliProjectConfig.getJson();
if (!jsonProjectConfig.projectType) {
this.cliProjectConfig.saveOverrideAttributeToJSON("projectType", "shell-template");
}
if (!jsonProjectConfig.forceDeviceXPanel) {
this.cliProjectConfig.saveOverrideAttributeToJSON("forceDeviceXPanel", false);
}
var customSignalDefaultValue = {
receiveStateTheme: "templateTheme",
sendEventTheme: "templateTheme",
receiveStateLanguage: "templateLanguage",
sendEventLanguage: "templateLanguage"
};
if (!jsonProjectConfig.customSignals) {
this.cliProjectConfig.saveOverrideAttributeToJSON("customSignals", customSignalDefaultValue);
}
if (!jsonProjectConfig.header.displayTheme) {
this.cliProjectConfig.saveOverrideAttributeToJSON("header.displayTheme", true);
}
};
Ch5UpgradeProjectCli.prototype.processDirectories = function () {
var pathForV2 = path.resolve(path.join(this.temporaryPath, "v2"));
this.logger.log("pathForV2", pathForV2);
// create temporary folder
try {
fs.mkdirSync(pathForV2, { recursive: true });
// Move v2 to temporary path
fsExtra.copySync(this.SHELL_FOLDER, pathForV2);
// Reason for below: https://docs.npmjs.com/cli/v8/configuring-npm/package-lock-json
fs.renameSync(path.resolve(path.join(pathForV2, "packagelock.json")), path.resolve(path.join(pathForV2, "package-lock.json")));
this.processCode();
}
catch (e) {
this.logger.printError(e);
throw new Error(e);
}
};
Ch5UpgradeProjectCli.prototype.processCode = function () {
try {
// delete node_modules
this.utils.deleteFolder(this.node_modules);
// delete old template from v1
this.utils.deleteFolder(this.templatePath);
// delete old assets folder from project
// this.utils.deleteFolder(this.assetsPath);
// delete old contract
// this.utils.deleteFile(this.contractPath);
// delete old vscode path
this.utils.deleteFolder(this.vscodePath);
// delete old shell utilities
this.utils.deleteFolder(this.oldShellUtilitiesPath);
// copy new template from v2
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.templatePath)), this.templatePath);
// copy assets from v2 to v1
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.assetsPath)), this.assetsPath);
// copy new contract
// fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.contractPath)), this.contractPath);
// copy new vscode
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.vscodePath)), this.vscodePath);
// copy license
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.licensePath)), this.licensePath);
// copy copyright
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.copyrightPath)), this.copyrightPath);
// copy readme
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.readmePath)), this.readmePath);
// copy package lock
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.packageLockPath)), this.packageLockPath);
// copy app.config
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.appConfigPath)), this.appConfigPath);
// copy webpack common
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.webpackCommonPath)), this.webpackCommonPath);
// copy webpack dev
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.webpackDevPath)), this.webpackDevPath);
// copy webpack prod
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.webpackProdPath)), this.webpackProdPath);
// copy index.html
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.indexHtmlPath)), this.indexHtmlPath);
// if hard-buttons.json exists
if (!fs.existsSync(path.resolve(this.hardButtonsPath))) {
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.hardButtonsPath)), this.hardButtonsPath);
}
// copy package.json and keep old name from v1
var oldPackageName = (fsExtra.readJSONSync(this.packagePath)).name;
fsExtra.copySync(path.resolve(path.join(this.temporaryPath, "v2", this.packagePath)), this.packagePath);
var packageFile = editJsonFile(this.packagePath);
packageFile.set('name', oldPackageName);
packageFile.save();
}
catch (e) {
throw new Error(e);
}
};
Ch5UpgradeProjectCli.prototype.processThemesDifferences = function () {
var oldThemes = this.cliProjectConfig.getAllThemes();
for (var _i = 0, oldThemes_1 = oldThemes; _i < oldThemes_1.length; _i++) {
var oldTheme = oldThemes_1[_i];
if (!oldTheme.extends) {
oldTheme.extends = oldTheme.name;
}
}
this.cliProjectConfig.saveOverrideAttributeToJSON('themes', oldThemes);
};
Ch5UpgradeProjectCli.prototype.processHeaderDifferences = function () {
var oldHeader = this.cliProjectConfig.getJson().header;
this.cliProjectConfig.saveOverrideAttributeToJSON('header', __assign(__assign({}, oldHeader), { diagnostics: {
logs: {
logAfterProjectLoad: true,
receiveStateLogDiagnostics: "",
logDiagnostics: "none"
}
} }));
};
Ch5UpgradeProjectCli.prototype.processPagesDifferences = function () {
var oldPages = this.cliProjectConfig.getJson().content.pages;
for (var _i = 0, oldPages_1 = oldPages; _i < oldPages_1.length; _i++) {
var oldPage = oldPages_1[_i];
if (oldPage.preloadPage === false) {
oldPage.preloadPage = false;
}
else {
oldPage.preloadPage = true;
}
if (oldPage.cachePage === true) {
oldPage.cachePage = true;
}
else {
oldPage.cachePage = false;
}
delete oldPage.pageProperties;
}
this.cliProjectConfig.saveOverrideAttributeToJSON('content.pages', oldPages);
var oldWidgets = this.cliProjectConfig.getJson().content.widgets;
for (var _a = 0, oldWidgets_1 = oldWidgets; _a < oldWidgets_1.length; _a++) {
var oldWidget = oldWidgets_1[_a];
delete oldWidget.widgetProperties;
}
this.cliProjectConfig.saveOverrideAttributeToJSON('content.widgets', oldWidgets);
};
/**
* Clean up
*/
Ch5UpgradeProjectCli.prototype.cleanUp = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
setTimeout(function () {
// this.utils.deleteFolder(this.temporaryPath);
}, 100);
return [2 /*return*/];
});
});
};
return Ch5UpgradeProjectCli;
}(Ch5BaseClassForCliUpgrade_1.Ch5BaseClassForCliUpgrade));
exports.Ch5UpgradeProjectCli = Ch5UpgradeProjectCli;