firebase-project-manager
Version:
Create and manage your Firebase projects from the command line or with code
363 lines • 16.7 kB
JavaScript
"use strict";
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 (_) 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 };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var chalk_1 = require("chalk");
var commander_1 = require("commander");
var figlet_1 = require("figlet");
var inquirer = require("inquirer");
exports.noop = function () { return undefined; };
function waitOnOperation(api, operation) {
return __awaiter(this, void 0, void 0, function () {
var name;
var _this = this;
return __generator(this, function (_a) {
name = operation.data.name;
return [2 /*return*/, new Promise(function (res, rej) {
var checkupInterval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, api.operations.get({ name: name })];
case 1:
result = _a.sent();
if (result.data.done) {
clearInterval(checkupInterval);
clearTimeout(timeoutId);
result.data.error ? rej(result.data.error.message) : res(result.data);
}
return [2 /*return*/];
}
});
}); }, 1000);
var timeoutId = setTimeout(function () {
clearInterval(checkupInterval);
rej("Timed out waiting on " + name);
}, 20000);
})];
});
});
}
exports.waitOnOperation = waitOnOperation;
function getRandomHexLen(length) {
var rand = '';
while (rand.length < length) {
rand += Math.floor(Math.random() * 0xf).toString(16);
}
return rand;
}
exports.getRandomHexLen = getRandomHexLen;
var CLI = /** @class */ (function () {
function CLI() {
this.args = process.argv;
this.initialRun = true;
this.prog = new commander_1.Command();
this.isInteractive = this.args.length < 3;
this.currentCmdPathStr = '';
this.currentCommand = this.prog;
this.currentCommandArgs = [];
this.inquirerConfig = {
'': {
choices: [{ name: 'Exit', value: 'exit' }],
type: 'list'
},
exit: function () {
process.exit();
}
};
}
CLI.prototype.run = function () {
return __awaiter(this, void 0, void 0, function () {
var configPath, header, config;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.isInteractive) return [3 /*break*/, 6];
this.formalizeCommandTree();
if (!this.inquirerConfig) return [3 /*break*/, 5];
configPath = '';
_a.label = 1;
case 1:
if (!(typeof this.inquirerConfig[configPath] !== 'function')) return [3 /*break*/, 3];
console.clear();
header = figlet_1.textSync('Firebase PM', 'Slant');
console.log(chalk_1.default.yellowBright(header));
config = this.inquirerConfig[configPath];
return [4 /*yield*/, inquirer.prompt(__assign(__assign({}, config), { name: 'value' }))];
case 2:
configPath = (_a.sent()).value;
return [3 /*break*/, 1];
case 3: return [4 /*yield*/, this.inquirerConfig[configPath]()];
case 4:
_a.sent();
_a.label = 5;
case 5: return [3 /*break*/, 7];
case 6:
if (this.initialRun) {
this.initialRun = false;
this.prog.parse(this.args);
}
_a.label = 7;
case 7: return [2 /*return*/];
}
});
});
};
CLI.prototype.command = function (commandStr) {
this.parseCommandString(commandStr);
if (this.inquirerConfig[this.currentCmdPathStr] === undefined) {
var parentPath = this.getParentCmdPath();
var parent_1 = this.inquirerConfig[parentPath];
if (typeof parent_1 !== 'function') {
parent_1.choices.push({
name: this.currentCmdPathStr,
value: this.currentCmdPathStr
});
}
this.inquirerConfig[this.currentCmdPathStr] = {
choices: [{ name: 'Back', value: parentPath }],
type: 'list'
};
}
return this;
};
CLI.prototype.menu = function (menuName) {
return this.command(menuName);
};
CLI.prototype.root = function () {
return this.command('');
};
CLI.prototype.description = function (description) {
var _this = this;
var parent = this.inquirerConfig[this.getParentCmdPath()];
if (typeof parent !== 'function') {
var choice = parent.choices.find(function (elt) { return elt.value === _this.currentCmdPathStr; });
choice.name = description;
}
this.currentCommand.description(description);
return this;
};
CLI.prototype.title = function (title) {
this.inquirerConfig[this.currentCmdPathStr].message = title;
return this;
};
CLI.prototype.action = function (fn) {
var _this = this;
var cmdArgsCopy = this.currentCommandArgs.slice();
if (this.isInteractive) {
this.inquirerConfig[this.currentCmdPathStr] = function () { return __awaiter(_this, void 0, void 0, function () {
var argValues, _loop_1, cmdArgsCopy_1, cmdArgsCopy_1_1, arg, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
argValues = {};
_loop_1 = function (arg) {
var choices, type, initialValue, config, _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
choices = arg.initialValue && arg.initialValue.includes(',') ? arg.initialValue.split(',') : undefined;
type = choices ? 'list' : 'input';
initialValue = choices ? choices[0] : arg.initialValue;
config = {
choices: choices,
default: initialValue,
message: arg.message,
name: arg.name,
validate: function (answer) {
return !arg.required || (arg.required && answer) ? true : 'This is required';
},
type: type
};
_a = argValues;
_b = arg.name;
return [4 /*yield*/, inquirer.prompt(config)];
case 1:
_a[_b] = (_c.sent())[arg.name];
return [2 /*return*/];
}
});
};
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 8]);
cmdArgsCopy_1 = __values(cmdArgsCopy), cmdArgsCopy_1_1 = cmdArgsCopy_1.next();
_b.label = 2;
case 2:
if (!!cmdArgsCopy_1_1.done) return [3 /*break*/, 5];
arg = cmdArgsCopy_1_1.value;
return [5 /*yield**/, _loop_1(arg)];
case 3:
_b.sent();
_b.label = 4;
case 4:
cmdArgsCopy_1_1 = cmdArgsCopy_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (cmdArgsCopy_1_1 && !cmdArgsCopy_1_1.done && (_a = cmdArgsCopy_1.return)) _a.call(cmdArgsCopy_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/, fn(argValues)];
}
});
}); };
}
else {
this.inquirerConfig[this.currentCmdPathStr] = fn;
this.currentCommand.action(function () {
var fnArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
fnArgs[_i] = arguments[_i];
}
var argMap = {};
cmdArgsCopy.forEach(function (arg, idx) {
argMap[arg.name] = fnArgs[idx];
});
fn(argMap);
});
}
return this;
};
CLI.prototype.parseCommandString = function (cmdStr) {
var cmdArgs = [];
var reqArgReg = /\<([^\>=]*)(?:=([^\>]*))?\>(?:\(([^)]*)\))?/g;
var optArgReg = /\[([^\]=]*)(?:=([^\]]*))?\](?:\(([^)]*)\))?/g;
cmdStr = cmdStr.replace(reqArgReg, function () {
var matches = [];
for (var _i = 0; _i < arguments.length; _i++) {
matches[_i] = arguments[_i];
}
var _a = __read(matches.slice(1, -2), 3), name = _a[0], initialValue = _a[1], message = _a[2];
cmdArgs.push({ name: name, initialValue: initialValue, message: message, required: true });
return "<" + name + ">";
});
cmdStr = cmdStr.replace(optArgReg, function () {
var matches = [];
for (var _i = 0; _i < arguments.length; _i++) {
matches[_i] = arguments[_i];
}
var _a = __read(matches.slice(1, -2), 3), name = _a[0], initialValue = _a[1], message = _a[2];
cmdArgs.push({ name: name, initialValue: initialValue, message: message, required: false });
return "[" + name + "]";
});
this.currentCommand = this.prog.command(cmdStr);
this.currentCommandArgs = cmdArgs;
this.currentCmdPathStr = cmdStr
.split('<')[0]
.split('[')[0]
.trim()
.replace(' ', '.');
};
CLI.prototype.getParentCmdPath = function () {
var pathArr = this.currentCmdPathStr.split('.');
if (pathArr.length <= 1) {
return '';
}
else {
pathArr.pop();
return pathArr.join('.');
}
};
CLI.prototype.formalizeCommandTree = function () {
var _this = this;
Object.entries(this.inquirerConfig).forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
if (typeof value === 'object') {
if (value.choices.length === 0) {
_this.inquirerConfig[key] = function () { };
}
else {
var config = _this.inquirerConfig[key];
config.choices = config.choices.sort(function (a, b) {
var aWeight = ['Exit', 'Back'].includes(a.name) ? -999 : 0;
var bWeight = ['Exit', 'Back'].includes(b.name) ? -999 : 0;
return bWeight - aWeight;
});
}
}
});
};
return CLI;
}());
exports.CLI = CLI;
//# sourceMappingURL=util.js.map