add emojis to the status command

This commit is contained in:
Yanick Champoux 2021-11-19 17:04:23 -05:00
parent f497fde8ad
commit f3bfe0f905

View File

@ -1,46 +1,67 @@
const Git = require('simple-git');
const inquirer = require('inquirer');
const report = require('yurnalist');
const _ = require('lodash');
const { defaults } = require('lodash/fp');
const u = require('updeep');
const columnify = require('columnify');
const chalk = require('chalk');
const Git = require("simple-git");
const inquirer = require("inquirer");
const report = require("yurnalist");
const _ = require("lodash");
const { defaults } = require("lodash/fp");
const u = require("updeep");
const columnify = require("columnify");
const chalk = require("chalk");
const iconFor = ( branch, branches ) => {
if(branch.done ) return "✅";
for( const dep of branch.dependencies ) {
if( !branches[dep].done ) return "⛔";
}
return "🙋";
}
module.exports = async (yargs) => {
const config = (await Git().listConfig('local')).all;
const config = (await Git().listConfig("local")).all;
let tree = Object.entries(config).reduce(
(accum, [key,value]) => u.updateIn(key,value,accum), {}
(accum, [key, value]) => u.updateIn(key, value, accum),
{}
);
const branches = {};
const normalizeArray = value => Array.isArray(value)?value: [value].filter(x=>x);
const normalizeArray = (value) =>
Array.isArray(value) ? value : [value].filter((x) => x);
const initBranch = config => defaults({
const initBranch = (config) =>
defaults(
{
upstream: [],
dependencies: [],
},config);
},
config
);
const current = (await Git().raw('branch','--show-current')).replace("\n",'');
const current = (await Git().raw("branch", "--show-current")).replace(
"\n",
""
);
for( const branch in tree.branch ) {
for (const branch in tree.branch) {
const entry = tree.branch[branch];
if( !('mikado-upstream' in entry) ) continue;
if (!("mikado-upstream" in entry)) continue;
branches[branch] = initBranch({
name: branch,
upstream: normalizeArray(entry['mikado-upstream']),
base: entry['mikado-base'],
done: entry['mikado-done'],
upstream: normalizeArray(entry["mikado-upstream"]),
base: entry["mikado-base"],
done: entry["mikado-done"],
});
}
for ( const branch of Object.values(branches) ) {
for( const ups of branch['upstream'] ) {
if(!branches[ups]) branches[ups] = initBranch({
for (const branch of Object.values(branches)) {
for (const ups of branch["upstream"]) {
if (!branches[ups])
branches[ups] = initBranch({
name: ups,
upstream: [],
dependencies: [],
@ -50,54 +71,59 @@ module.exports = async (yargs) => {
}
}
if( branches[ current ] ) branches[current].current = true;
if (branches[current]) branches[current].current = true;
const depColor = current => async ( dep ) => {
let color = branches[dep].done ? chalk.green: chalk.red;
const depColor = (current) => async (dep) => {
let color = branches[dep].done ? chalk.green : chalk.red;
let contains = await Git().raw('branch', '--contains', dep);
contains = contains.split("\n").map( x => x.trim().replace('* ','') );
let contains = await Git().raw("branch", "--contains", dep);
contains = contains
.split("\n")
.map((x) => x.trim().replace("* ", ""));
if( contains.includes(current) )
color = color.underline;
if (contains.includes(current)) color = color.underline;
return color(dep);
};
}
const upstreamColor = (current) => async (up) => {
let color = branches[up].done ? chalk.green : chalk.red;
const upstreamColor = current => async ( up ) => {
let color = branches[up].done ? chalk.green: chalk.red;
let contains = await Git().raw("branch", "--contains", current);
contains = contains
.split("\n")
.map((x) => x.trim().replace("* ", ""));
let contains = await Git().raw('branch', '--contains', current);
contains = contains.split("\n").map( x => x.trim().replace('* ','') );
if( contains.includes(up) )
color = color.underline;
if (contains.includes(up)) color = color.underline;
return color(up);
};
console.log("\n=== Mikado status ===\n");
const sorted = _.sortBy(Object.values(branches), ["current"]);
for (const branch of sorted) {
console.log(
iconFor(branch,branches),
chalk.blue(branch.name),
branch.current ? "👷" : ' '
);
console.log(`\tbase: ${chalk.magenta(branch.base ?? "<none>")}`);
if (branch.upstream.length) {
const ups = await Promise.all(
branch.upstream.map(upstreamColor(branch.name))
);
console.log("\tupstream:", ups.join(" "));
}
console.log( "\n=== Mikado status ===\n" );
const sorted = _.sortBy(Object.values(branches), ['current'])
for( const branch of sorted ) {
console.log( chalk.blue(branch.name), branch.current ? "(current branch)" : "", branch.done ? chalk.green.bold('done'):"" );
console.log( `\tbase: ${chalk.magenta(branch.base ?? '<none>')}` );
if(branch.upstream.length) {
const ups = await Promise.all(branch.upstream.map(upstreamColor(branch.name)));
console.log( "\tupstream:", ups.join(" ") );
if (branch.dependencies.length) {
const deps = await Promise.all(
branch.dependencies.map(depColor(branch.name))
);
console.log("\tdependencies:", deps.join(" "));
}
if(branch.dependencies.length) {
const deps = await Promise.all(branch.dependencies.map(depColor(branch.name)));
console.log( "\tdependencies:", deps.join(" ") );
}
console.log("\n\n");
}
}
};