├── package.json ├── README.md ├── LICENSE ├── .gitignore ├── yarn.lock └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "script-progress", 3 | "version": "1.0.5", 4 | "description": "Estimate script execution time", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "bin": { 8 | "script-progress": "./index.js" 9 | }, 10 | "dependencies": { 11 | "blueimp-md5": "^2.10.0", 12 | "cli-progress": "^2.0.0", 13 | "find-cache-dir": "^2.0.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # script-progress 2 | 3 | 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. 4 | 5 | ### Installation 6 | 7 | ```sh 8 | yarn add script-progress 9 | ``` 10 | or 11 | ```sh 12 | npm i script-progress 13 | ``` 14 | 15 | ### Example Usage 16 | 17 | Change your build script in `package.json`: 18 | ```diff 19 | - "build": "react-scripts build", 20 | + "build-js": "react-scripts build", 21 | + "build": "script-progress yarn build-js", 22 | ``` 23 | or just 24 | ```diff 25 | - "build": "react-scripts build", 26 | + "build": "script-progress react-scripts build", 27 | ``` 28 | 29 | The script will show a progress bar on the second and subsequent runs. 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alexander Kuznetsov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | blueimp-md5@^2.10.0: 6 | version "2.10.0" 7 | resolved "https://registry.yarnpkg.com/blueimp-md5/-/blueimp-md5-2.10.0.tgz#02f0843921f90dca14f5b8920a38593201d6964d" 8 | 9 | cli-progress@^2.0.0: 10 | version "2.0.0" 11 | resolved "https://registry.yarnpkg.com/cli-progress/-/cli-progress-2.0.0.tgz#8f6c093aecaf2f55b5714f7005af2ab8ab6eb923" 12 | dependencies: 13 | colors "^1.1.2" 14 | 15 | colors@^1.1.2: 16 | version "1.3.0" 17 | resolved "https://registry.yarnpkg.com/colors/-/colors-1.3.0.tgz#5f20c9fef6945cb1134260aab33bfbdc8295e04e" 18 | 19 | commondir@^1.0.1: 20 | version "1.0.1" 21 | resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" 22 | 23 | find-cache-dir@^2.0.0: 24 | version "2.0.0" 25 | resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-2.0.0.tgz#4c1faed59f45184530fb9d7fa123a4d04a98472d" 26 | dependencies: 27 | commondir "^1.0.1" 28 | make-dir "^1.0.0" 29 | pkg-dir "^3.0.0" 30 | 31 | find-up@^3.0.0: 32 | version "3.0.0" 33 | resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73" 34 | dependencies: 35 | locate-path "^3.0.0" 36 | 37 | locate-path@^3.0.0: 38 | version "3.0.0" 39 | resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e" 40 | dependencies: 41 | p-locate "^3.0.0" 42 | path-exists "^3.0.0" 43 | 44 | make-dir@^1.0.0: 45 | version "1.3.0" 46 | resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c" 47 | dependencies: 48 | pify "^3.0.0" 49 | 50 | p-limit@^2.0.0: 51 | version "2.0.0" 52 | resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.0.0.tgz#e624ed54ee8c460a778b3c9f3670496ff8a57aec" 53 | dependencies: 54 | p-try "^2.0.0" 55 | 56 | p-locate@^3.0.0: 57 | version "3.0.0" 58 | resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4" 59 | dependencies: 60 | p-limit "^2.0.0" 61 | 62 | p-try@^2.0.0: 63 | version "2.0.0" 64 | resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.0.0.tgz#85080bb87c64688fa47996fe8f7dfbe8211760b1" 65 | 66 | path-exists@^3.0.0: 67 | version "3.0.0" 68 | resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" 69 | 70 | pify@^3.0.0: 71 | version "3.0.0" 72 | resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" 73 | 74 | pkg-dir@^3.0.0: 75 | version "3.0.0" 76 | resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-3.0.0.tgz#2749020f239ed990881b1f71210d51eb6523bea3" 77 | dependencies: 78 | find-up "^3.0.0" 79 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | var cp = require('child_process'); 3 | var fs = require('fs'); 4 | var md5 = require('blueimp-md5'); 5 | var progress = require('cli-progress'); 6 | var findCacheDir = require('find-cache-dir'); 7 | 8 | function median(arr) { 9 | arr = arr.slice(0); 10 | var middle = (arr.length + 1) / 2, 11 | sorted = arr.sort(function(a,b) { return a - b }); 12 | return (sorted.length % 2) ? sorted[middle - 1] : (sorted[middle - 1.5] + sorted[middle - 0.5]) / 2; 13 | }; 14 | 15 | var bar = new progress.Bar({ 16 | format: 'progress [{bar}] {percentage}%', 17 | clearOnComplete: true 18 | }, progress.Presets.shades_classic); 19 | var cacheThunk = findCacheDir({ name: 'script-progress', create: true, thunk: true }); 20 | 21 | var args = process.argv.slice(2); 22 | var cmd = args.shift(); 23 | 24 | if (!process.stdout.isTTY) { 25 | var res = cp.spawnSync(cmd, args, { env: process.env, stdio: [process.stdin, process.stdout, process.stderr] }); 26 | process.exit(res.status); 27 | } 28 | 29 | var cacheFileName = md5(args.join(' ')); 30 | 31 | var DEFAULT_CACHE = { intervals: [] }; 32 | var cacheFileContent = fs.readFileSync(cacheThunk(cacheFileName), { flag: 'a+' }); 33 | var cache; 34 | try { 35 | cache = JSON.parse(cacheFileContent.toString() || JSON.stringify(DEFAULT_CACHE)); 36 | } catch (e) {} 37 | 38 | if (!cache || !Array.isArray(cache.intervals)) { 39 | cache = DEFAULT_CACHE; 40 | } 41 | 42 | var med = median(cache.intervals); 43 | 44 | var eta = med ? Math.max.apply(Math, cache.intervals.filter(function(val) { 45 | return val / med < 1.5; 46 | })) : 0; 47 | 48 | var refreshInterval; 49 | 50 | if (eta) { 51 | bar.start(eta, 0); 52 | 53 | refreshInterval = setInterval(function() { 54 | bar.update(getDiff()); 55 | }, 200); 56 | } 57 | 58 | var time = process.hrtime(); 59 | 60 | process.env.FORCE_COLOR = true; 61 | var ls = cp.spawn(cmd, args, { env: process.env }); 62 | 63 | function getDiff() { 64 | var diff = process.hrtime(time); 65 | return (diff[0] + diff[1] / 10E9); 66 | } 67 | 68 | function clear() { 69 | bar.terminal.cursorTo(0, null); 70 | bar.terminal.clearRight(); 71 | } 72 | 73 | function redraw() { 74 | bar.value = getDiff(); 75 | bar.lastDrawnString = ''; 76 | bar.render(); 77 | } 78 | 79 | ls.stdout.on('data', function (data) { 80 | if (eta) { 81 | clear(); 82 | bar.terminal.cursorSave(); 83 | } 84 | process.stdout.write(data.toString()); 85 | if (eta) redraw(); 86 | }); 87 | 88 | ls.stderr.on('data', function (data) { 89 | if (eta) { 90 | clear(); 91 | bar.terminal.cursorSave(); 92 | } 93 | process.stderr.write(data.toString()); 94 | if (eta) redraw(); 95 | }); 96 | 97 | ls.on('close', (code) => { 98 | var diff = getDiff(); 99 | 100 | if (code) { 101 | process.exit(code); 102 | } 103 | 104 | if (!eta || diff / eta > 0.1) { 105 | cache.intervals.push(diff); 106 | if (cache.intervals.length > 5) { 107 | cache.intervals.shift(); 108 | } 109 | } 110 | fs.writeFileSync(cacheThunk(cacheFileName), JSON.stringify(cache)); 111 | 112 | clearInterval(refreshInterval); 113 | if (eta) { 114 | bar.terminal.cursorSave(); 115 | bar.stop(); 116 | } 117 | }); 118 | --------------------------------------------------------------------------------