jack-cli
Version:
Chop through that Git log wicked fast!
187 lines • 9.83 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());
});
};
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 __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var notification_1 = require("../interface/notification");
var actions_1 = require("../state/actions");
var store_1 = require("../state/store");
var colors_1 = require("./colors");
var commands_def_1 = require("./commands-def");
var config_util_1 = require("./config-util");
var git_util_1 = require("./git-util");
var log_util_1 = require("./log-util");
var logger_1 = require("./logger");
var promisify_child_process_1 = require("./promisify-child-process");
var util_functions_1 = require("./util-functions");
var util_functions_2 = require("./util-functions");
var validators_1 = require("./validators");
var declaredCommands;
var validateCommands = function (commands) {
var errorMessages = [];
for (var _i = 0, commands_1 = commands; _i < commands_1.length; _i++) {
var command = commands_1[_i];
var errors = validators_1.validateCommand(command);
if (errors.length) {
errorMessages.push({ messages: errors, command: command });
}
}
if (errorMessages.length > 0) {
logger_1.logger.error('\nOne or more errors while registering commands from your .jack.json file:');
logger_1.logger.info("Config file path: " + config_util_1.getConfigFilePath());
for (var _a = 0, errorMessages_1 = errorMessages; _a < errorMessages_1.length; _a++) {
var errorMessage = errorMessages_1[_a];
logger_1.logger.log('\n' + colors_1.colors.emphasis('-'.repeat(40) + '\n'));
for (var _b = 0, _c = errorMessage.messages; _b < _c.length; _b++) {
var message = _c[_b];
logger_1.logger.error("ERROR: " + message);
}
logger_1.logger.warn(JSON.stringify(errorMessage.command, null, ' '));
}
process.exit(1);
}
};
exports.getCommands = function () {
if (declaredCommands)
return declaredCommands;
declaredCommands = __spreadArrays(commands_def_1.COMMANDS, config_util_1.getCommands());
validateCommands(declaredCommands);
declaredCommands = util_functions_2.uniqByLast(declaredCommands, 'key');
return declaredCommands;
};
// tslint:disable-next-line:max-line-length
exports.registerCommand = function (screen, command) { return __awaiter(void 0, void 0, void 0, function () {
var _a, markedSHA, SHA, sorted, _b, commandString, _c, _d, _e, commandArray_1, spawnOpts_1, messages, error_1, onErrorCommand, onErrorCommandArray, cleanUpError_1;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
_a = store_1.store.getState(), markedSHA = _a.markedSHA, SHA = _a.SHA;
_f.label = 1;
case 1:
_f.trys.push([1, 10, , 15]);
if (!markedSHA) return [3 /*break*/, 3];
return [4 /*yield*/, git_util_1.sortSHAs(markedSHA, SHA)];
case 2:
_b = _f.sent();
return [3 /*break*/, 4];
case 3:
_b = [SHA, SHA];
_f.label = 4;
case 4:
sorted = _b;
commandString = command.command;
commandString = commandString.replace(commands_def_1.Placeholder.SHA_SINGLE, SHA);
commandString = commandString.replace(commands_def_1.Placeholder.SHA_RANGE, sorted[0] + "^.." + sorted[1]);
commandString = commandString.replace(commands_def_1.Placeholder.SHA_SINGLE_OR_RANGE, markedSHA ? sorted[0] + "^.." + sorted[1] : SHA);
_d = (_c = commandString).replace;
_e = [commands_def_1.Placeholder.COMMIT_MESSAGE];
return [4 /*yield*/, git_util_1.gitCommitMessage(SHA)];
case 5:
commandString = _d.apply(_c, _e.concat([_f.sent()]));
commandArray_1 = util_functions_1.stringToCommandArray(commandString);
spawnOpts_1 = { shell: commandArray_1.includes('|') };
messages = [];
if (command.description) {
messages.push(command.description);
}
messages.push("Running command \"" + commandString + "\"");
notification_1.notifier.info(messages.join('\n'));
if (!command.foreground) return [3 /*break*/, 7];
return [4 /*yield*/, new Promise(function (resolve, reject) {
screen.exec(commandArray_1[0], commandArray_1.slice(1),
// @ts-ignore since 'spawnOpts' is passed to spawn and not
// exec, it will still function
spawnOpts_1, function (err, ok) {
if (err) {
reject(err);
}
else if (!ok) {
reject(new Error('The command exited with a error code.'));
}
else {
resolve();
}
});
})];
case 6:
_f.sent();
return [3 /*break*/, 9];
case 7: return [4 /*yield*/, promisify_child_process_1.spawnPromise(commandArray_1[0], commandArray_1.slice(1), spawnOpts_1)];
case 8:
_f.sent();
_f.label = 9;
case 9:
if (command.refreshOnComplete) {
log_util_1.generateLog(screen);
}
notification_1.notifier.success("Command finished: \"" + commandString + "\"");
return [3 /*break*/, 15];
case 10:
error_1 = _f.sent();
notification_1.notifier.error(error_1.message);
onErrorCommand = command.onErrorCommand;
if (!onErrorCommand) return [3 /*break*/, 14];
notification_1.notifier.info("Performing clean-up command \"" + onErrorCommand + "\"");
_f.label = 11;
case 11:
_f.trys.push([11, 13, , 14]);
onErrorCommandArray = util_functions_1.stringToCommandArray(onErrorCommand);
return [4 /*yield*/, promisify_child_process_1.spawnPromise(onErrorCommandArray[0], onErrorCommandArray.slice(1))];
case 12:
_f.sent();
return [3 /*break*/, 14];
case 13:
cleanUpError_1 = _f.sent();
notification_1.notifier.error(cleanUpError_1.message);
return [3 /*break*/, 14];
case 14: return [3 /*break*/, 15];
case 15:
if (markedSHA) {
actions_1.Actions.markSHA(null);
notification_1.notifier.info('Unmarked commit');
}
return [2 /*return*/];
}
});
}); };
//# sourceMappingURL=command-util.js.map