Repository: alexkuz/script-progress Branch: master Commit: dcfc93337f0b Files: 5 Total size: 5.5 KB Directory structure: gitextract_t4k728vv/ ├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Logs logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # nyc test coverage .nyc_output # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Bower dependency directory (https://bower.io/) bower_components # node-waf configuration .lock-wscript # Compiled binary addons (https://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules/ jspm_packages/ # TypeScript v1 declaration files typings/ # Optional npm cache directory .npm # Optional eslint cache .eslintcache # Optional REPL history .node_repl_history # Output of 'npm pack' *.tgz # Yarn Integrity file .yarn-integrity # dotenv environment variables file .env # next.js build output .next ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 Alexander Kuznetsov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # script-progress A simple tool for heavy NPM/Yarn scripts that run for a long but roughly identical time. It's not intended to be precise but gives you some sense of execution time. ### Installation ```sh yarn add script-progress ``` or ```sh npm i script-progress ``` ### Example Usage Change your build script in `package.json`: ```diff - "build": "react-scripts build", + "build-js": "react-scripts build", + "build": "script-progress yarn build-js", ``` or just ```diff - "build": "react-scripts build", + "build": "script-progress react-scripts build", ``` The script will show a progress bar on the second and subsequent runs. ================================================ FILE: index.js ================================================ #! /usr/bin/env node var cp = require('child_process'); var fs = require('fs'); var md5 = require('blueimp-md5'); var progress = require('cli-progress'); var findCacheDir = require('find-cache-dir'); function median(arr) { arr = arr.slice(0); var middle = (arr.length + 1) / 2, sorted = arr.sort(function(a,b) { return a - b }); return (sorted.length % 2) ? sorted[middle - 1] : (sorted[middle - 1.5] + sorted[middle - 0.5]) / 2; }; var bar = new progress.Bar({ format: 'progress [{bar}] {percentage}%', clearOnComplete: true }, progress.Presets.shades_classic); var cacheThunk = findCacheDir({ name: 'script-progress', create: true, thunk: true }); var args = process.argv.slice(2); var cmd = args.shift(); if (!process.stdout.isTTY) { var res = cp.spawnSync(cmd, args, { env: process.env, stdio: [process.stdin, process.stdout, process.stderr] }); process.exit(res.status); } var cacheFileName = md5(args.join(' ')); var DEFAULT_CACHE = { intervals: [] }; var cacheFileContent = fs.readFileSync(cacheThunk(cacheFileName), { flag: 'a+' }); var cache; try { cache = JSON.parse(cacheFileContent.toString() || JSON.stringify(DEFAULT_CACHE)); } catch (e) {} if (!cache || !Array.isArray(cache.intervals)) { cache = DEFAULT_CACHE; } var med = median(cache.intervals); var eta = med ? Math.max.apply(Math, cache.intervals.filter(function(val) { return val / med < 1.5; })) : 0; var refreshInterval; if (eta) { bar.start(eta, 0); refreshInterval = setInterval(function() { bar.update(getDiff()); }, 200); } var time = process.hrtime(); process.env.FORCE_COLOR = true; var ls = cp.spawn(cmd, args, { env: process.env }); function getDiff() { var diff = process.hrtime(time); return (diff[0] + diff[1] / 10E9); } function clear() { bar.terminal.cursorTo(0, null); bar.terminal.clearRight(); } function redraw() { bar.value = getDiff(); bar.lastDrawnString = ''; bar.render(); } ls.stdout.on('data', function (data) { if (eta) { clear(); bar.terminal.cursorSave(); } process.stdout.write(data.toString()); if (eta) redraw(); }); ls.stderr.on('data', function (data) { if (eta) { clear(); bar.terminal.cursorSave(); } process.stderr.write(data.toString()); if (eta) redraw(); }); ls.on('close', (code) => { var diff = getDiff(); if (code) { process.exit(code); } if (!eta || diff / eta > 0.1) { cache.intervals.push(diff); if (cache.intervals.length > 5) { cache.intervals.shift(); } } fs.writeFileSync(cacheThunk(cacheFileName), JSON.stringify(cache)); clearInterval(refreshInterval); if (eta) { bar.terminal.cursorSave(); bar.stop(); } }); ================================================ FILE: package.json ================================================ { "name": "script-progress", "version": "1.0.5", "description": "Estimate script execution time", "main": "index.js", "license": "MIT", "bin": { "script-progress": "./index.js" }, "dependencies": { "blueimp-md5": "^2.10.0", "cli-progress": "^2.0.0", "find-cache-dir": "^2.0.0" } }