Skip to content

Instantly share code, notes, and snippets.

Created December 25, 2017 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/57e98d1dc695d10792e1c9a067271a3d to your computer and use it in GitHub Desktop.
Save anonymous/57e98d1dc695d10792e1c9a067271a3d to your computer and use it in GitHub Desktop.
Task chunking CL tool for node.js

How to use

  • download files, put them in a directory
  • make a subdir called "problems"
node app.js

To use a db (essentially a flat file in ./problems) ie problem namespace

use dbFileNameWhateverYouNameIt

To make an issue

issue issueDescriptionGoesHere

To solve an issue

fix solutionDescriptionGoesHere

To see your db:

db

To update your db file:

use dbFileName
const Problem = require('./Problematic.js');
const Flow = require('./Flow.js');
if((function(repl = false)
{
// for debugging purposes
repl
?
require('repl').start('>> '):null;
return !repl;
})())
Flow.listen((input)=>
{
// define problem namespace
Flow.route(input.match(/^use\s(.+)$/), (snapshot)=>
{
Problem.init(snapshot);
});
// break out if not init
if(!Problem.db) return;
// db name
Flow.route(input.match(/^db$/), (ok)=>
{
console.log(`Using ${Problem.name}`);
})
// user defines problem
Flow.route(input.match(/^issue\s(.+)$/), (description)=>
{
Problem.create(description);
});
// user solves problem
Flow.route(input.match(/^fix\s(.+)$/), (description)=>
{
Problem.solve(description);
});
// show current problem
Flow.route(input.match(/^show$/), ()=>
{
console.log(Problem.db[Problem.current()]);
});
});
/**
* Input -> output
*/
class Flow
{
/**
* listen for commands
*
* @param {Function} callback The callback
*/
listen (callback)
{
let conduit = require('readline');
conduit = conduit.createInterface(
{
input: process.stdin,
output: process.stdout,
prompt: '#: '
});
// initial prompt
conduit.prompt();
conduit.on('line', (line)=>
{
callback(line);
// subsequent prompts
conduit.prompt();
});
}
/**
* route commands
*
* @param {Object} hits Result of String.match method
* @param {Function} callback The callback
*/
route (hits, callback)
{
if(hits)
{
callback(hits[1]);
}
}
}
module.exports = new Flow;
/*
* Luna. Orbits things, makes tides.
*/
class Luna
{
orbit(procedure)
{
this.cycle = setInterval(()=>
{
procedure.run();
}, procedure.scale);
}
halt()
{
clearInterval(this.cycle);
}
}
module.exports = new Luna;
const Luna = require('./Luna.js'),
fs = require('fs');
/**
* Problematic
*
* @class Problem (name)
*/
class Problematic
{
init (name)
{
this.changed = this.name ? this.name !== name : 1;
this.name = encodeURIComponent(name);
// init or use db
this.db = !this.changed ? this.db :
[
// assume data structure as
// [parent, problem, time, (**solution)]
// **optional
[ 0, `init db: ${name}`, 0 ]
];
// sync with snapshot
this.sync();
// hook self-update
Luna.halt(); // one moon is more than enough
Luna.orbit(
{
run:()=>{this.ticktock();},
scale:1000*60
});
}
/**
* figure out current problem
*
* @return {integer} problem index
*/
current ()
{
let i = (this.db.length - 1);
for(i; i > 0; i--)
{
if(!this.db[i][3])
{
break;
}
}
return i;
}
/**
* create a problem
*
* @param {string} description self-explanatory
*/
create (description)
{
this.db.push([this.current(), description, 0]);
}
/**
* solve current problem
*
* @param {string} by doing what
*/
solve (by)
{
this.db[this.current()][3] = by;
}
/**
* time update
*/
ticktock ()
{
this.db[this.current()][2]++;
}
/**
* take/make snapshot of problem tree
*/
sync ()
{
// preparation
let name = `${this.name}.json`,
storage = './problems/',
file = `${storage}${name}`,
// resolution
exists = fs.readdirSync(storage).indexOf(name) !== -1,
loaded,
instruction = 'update';
if(this.changed && exists)
{
// load file for comparison
loaded = JSON.parse(fs.readFileSync(file, 'utf8'));
// is newer
if(loaded.length > this.db.length)
instruction = 'load';
}
// modification
if(instruction === 'load')
{
this.db = loaded;
}
if(instruction === 'update')
{
fs.writeFileSync(file, JSON.stringify(this.db))
}
}
}
module.exports = new Problematic;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment