@iotize/cli
Version:
IoTize command line interface
142 lines • 4.82 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
require("../module-alias-setup");
require("./b-setup-global");
require("./configure-log");
require("@iotize/device-client.js/ext/configurator");
require("@iotize/device-client.js/ext/factory-reset");
require("@iotize/device-client.js/ext/firmware-update");
require("@iotize/testing-grammar.js/ext");
require("reflect-metadata");
const clime_1 = require("clime");
const fs_1 = require("fs");
const path_1 = require("path");
const rxjs_1 = require("rxjs");
const ui_1 = require("../ui");
function addIfExist(commands, info) {
if (fs_1.existsSync(info.path)) {
commands.push(info);
}
}
process
.on('SIGTERM', shutdown('SIGTERM'))
.on('SIGINT', shutdown('SIGINT'))
.on('SIGQUIT', shutdown('SIGQUIT'))
.on('uncaughtException', shutdown('uncaughtException'));
function shutdown(signal) {
return (err) => {
ui_1.display.warn(`Shutdown signal ${signal}...`);
if (err && err.stack) {
if (process.env['DEBUG']) {
console.warn('debug error: ', err);
}
}
setTimeout(() => {
ui_1.display.message('...waited 1s, exiting.');
process.exit(err ? 1 : 0);
}, 1000).unref();
};
}
// Global exposure
const appName = 'iotize';
// in App.test.tsx
const WebSocket = require('ws');
global.WebSocket = WebSocket;
// The second parameter is the path to folder that contains command modules.
const commandsDirectory = path_1.resolve(__dirname, 'commands');
const commands = [
{
label: 'Build in',
path: commandsDirectory
},
{
label: 'Tools',
path: path_1.resolve(__dirname, 'commands-tools')
},
{
label: 'TapNPass',
path: path_1.resolve(__dirname, 'commands-tapnpass')
}
];
addIfExist(commands, {
label: 'App',
path: path_1.resolve(__dirname, 'commands-app')
});
addIfExist(commands, {
label: 'Cloud',
path: path_1.resolve(__dirname, 'commands-firebase')
});
addIfExist(commands, {
label: 'Experimental',
path: path_1.resolve(__dirname, 'commands-experimental')
});
addIfExist(commands, {
label: 'Gateway',
path: path_1.resolve(__dirname, 'commands-gateway')
});
addIfExist(commands, {
label: 'Cayenne',
path: path_1.resolve(__dirname, 'commands-cayenne')
});
const cli = new clime_1.CLI(appName, commands);
// Clime in its core provides an object-based command-line infrastructure.
// To have it work as a common CLI, a shim needs to be applied:
// let shim = new Shim(cli);
function execute() {
return __awaiter(this, void 0, void 0, function* () {
let result = cli.execute(process.argv.slice(2));
// console.log(`execute result type ${typeof result}`, result);
if (result instanceof rxjs_1.Observable) {
result = result.toPromise();
}
const outputResult = yield result;
yield displayResult(outputResult);
});
}
function displayResult(outputResult) {
return __awaiter(this, void 0, void 0, function* () {
if (!!outputResult && 'print' in outputResult && typeof outputResult.print === 'function') {
yield outputResult.print(process.stdout, process.stderr);
}
else if (outputResult !== undefined) {
console.log(outputResult);
}
});
}
try {
execute()
.then(() => {
// console.log(`Promise resolved!`);
return require('./teardown');
})
.catch(onError);
}
catch (err) {
onError(err);
}
function isTruthy(val) {
return val && !(val === "false" || val === "0");
}
function onError(err) {
if (isTruthy(process.env.DEBUG)) {
console.error('debug error', err);
}
if (err instanceof clime_1.HelpInfo) {
err.print(process.stdout, process.stderr);
}
else {
ui_1.display.error(`${err.message}`);
}
require('./teardown');
}
//# sourceMappingURL=main.js.map