Skip to content

Instantly share code, notes, and snippets.

View sompylasar's full-sized avatar

Ivan Babak sompylasar

View GitHub Profile
@sompylasar
sompylasar / formatProgress.js
Last active January 11, 2019 19:43
Progress tracking and time estimate for a queue of similarly sized tasks.
const progressDefaultColor = typeof chalk !== 'undefined' ? chalk.magenta : (x) => x;
const progressErrorColor = typeof chalk !== 'undefined' ? chalk.red : (x) => x;
function formatProgress(startTime, queuedCount, finishedCount, errorsCount) {
const progressPercent = queuedCount > 0 ? (100 * finishedCount) / queuedCount : 100;
const errorsPercent = queuedCount > 0 ? (100 * errorsCount) / queuedCount : 0;
const elapsedTimeSeconds = (Date.now() - startTime) / 1000;
const itemsPerSecond = elapsedTimeSeconds > 0 ? finishedCount / elapsedTimeSeconds : 0;
const estimatedTimeSeconds = itemsPerSecond > 0 ? Math.max(0, (queuedCount - finishedCount) / itemsPerSecond) : 0;
return (
progressDefaultColor(
@sompylasar
sompylasar / nodeGracefulSigint.js
Last active December 19, 2018 05:40
Node.js process graceful interruption from SIGINT. Force termination after receiving certain number of SIGINTs.
const EventEmitter = require('events');
function gracefulSigint({ forceAttempts } = {}) {
let state = {
isEnabled: false,
isTerminating: false,
isTerminatingForce: forceAttempts >= 0 ? forceAttempts : 5,
};
let emitter = new EventEmitter();
let sigintHandler = () => {
@sompylasar
sompylasar / eslintParserForTypeScriptAndJavaScript.js
Last active December 17, 2018 22:13
Branching ESLint parser to consume both TypeScript (via typescript-eslint-parser) and JavaScript (via babel-eslint). Usage: in .eslintrc, "parser": "./eslintParserForTypeScriptAndJavaScript.js"
/* eslint-disable func-names, prefer-arrow-callback */
function isTypeScriptFilePath(filePath) {
return /\.tsx?$/.test(filePath);
}
function isTypeScriptCode(text) {
return (
/^\s*((declare\s+module)|(interface))\s+/m.test(text)
);
@sompylasar
sompylasar / runExternalProcessAsync.js
Created December 19, 2018 05:52
`Promise` wrapper around `require('child_process').spawn` that runs a process to completion with a given `stdin` and collects `stdout`, `stderr`, and the exit code.
const spawn = require('child_process').spawn;
async function runExternalProcessAsync({ procExecutable, procArgs, procStdinString }) {
const proc = await new Promise((resolve) => {
resolve(spawn(procExecutable, procArgs));
});
try {
return await new Promise((resolve, reject) => {
const procStdoutParts = [];
const procStderrParts = [];
@sompylasar
sompylasar / github-wiki-table-of-contents.js
Created January 22, 2019 23:09
While in the Wiki Edit (`/_edit`) Preview mode, run this snippet in the browser console to get a Markdown of a Table of contents. https://github.com/isaacs/github/issues/215
console.log(
'\n\n\n> **Table of contents**\n> \n' +
Array.from(document.querySelectorAll('h1 > a, h2 > a, h3 > a')).map((a) => (
{'H1':'> * ','H2':'> * ','H3':'> - '}[a.parentNode.tagName] +
`[${a.parentNode.innerText.trim()}](${a.hash})`
)).join('\n') +
'\n\n\n'
);