These days I spend most of my working day writing TypeScript/Node/React apps. I still love (and work with) Scala but in the main, it's the faster-moving Javascript world where most of the changes are taking place. One of the best things about the NPM/Yarn workflow that these apps all share, is the ability to declare "scripts" to shortcut common development tasks. It's not new (make has entered the chat) but it's very flexible and powerful. The only downside is, there's no definitive convention for naming the tasks.
One project might use start (i.e. yarn start) to launch the application in development mode (e.g. with hot-reload and suchlike) while another might use run:local (i.e. yarn run:local) for a similar thing. The upshot being, a developer ends up opening package.json in some way, scrolling down to the scripts stanza and looking for their desired task, before carefully typing it in at the command prompt. Can we do better?
Phase 1: The 's' alias
Utilising the wonderful jq, we can very easily get a very nice first pass at streamlining the flow:alias s='cat package.json | jq .scripts'
This eliminates scrolliing past all the unwanted noise of the package.json (dependencies, jest configuration, etc etc) and just gives a nice list of the scripts:
john$ s { "build": "rm -rf dist && yarn compile && node scripts/build.js ", "compile": "tsc -p .", "compile:watch": "tsc --watch", "lint": "yarn eslint . --ext .ts", "start:dev": "source ./scripts/init_dev.sh && concurrently \"yarn compile:watch\" \"nodemon\"", "start": "source ./scripts/init_dev.sh && yarn compile && node dist/index", "test": "NODE_ENV=test jest --runInBand", "test:watch": "yarn test --watch", "test:coverage": "yarn test --coverage" } john$A nice start. But now while you can see the list of targets, you've still got to (ugh) type one in.
What if ...
Phase 2: Menu-driven
TIL about the select BASH built-in command which will make an interactive menu out of a list of options. So let's do it!~/bin/menufy_package_json.sh
#!/bin/bash # Show the scripts in alphabetical order, so as to match the # numbered options shown later cat package.json | jq '.scripts | to_entries | sort_by(.key) | from_entries' SCRIPTS=$(cat package.json | jq '.scripts | keys | .[]' --raw-output) select script in $SCRIPTS do yarn $script break doneI've got that aliased to sm (for "script menu") so here's what the flow looks like now:
john$ sm { "build": "rm -rf dist && yarn compile && node scripts/build.js ", "compile": "tsc -p .", "compile:watch": "tsc --watch", "lint": "yarn eslint . --ext .ts", "start": "source ./scripts/init_dev.sh && yarn compile && node dist/index", "start:dev": "source ./scripts/init_dev.sh && concurrently \"yarn compile:watch\" \"nodemon\"", "test": "NODE_ENV=test jest --runInBand", "test:coverage": "yarn test --coverage", "test:watch": "yarn test --watch" } 1) build 4) lint 7) test 2) compile 5) start 8) test:coverage 3) compile:watch 6) start:dev 9) test:watch #? 9 yarn run v1.21.1 $ yarn test --watch $ NODE_ENV=test jest --runInBand --watch... and away it goes. For a typical command like yarn test:watch I've gone from 15 keystrokes plus [Enter] to sm[Enter]9[Enter] => five keystrokes, and that's not even including the time/keystroke saving of showing the potential targets in the first place instead of opening package.json in some way and scrolling. For something I might do tens of times a day, I call that a win!
No comments:
Post a Comment
Comments welcome - spam is not. Spam will be detected, deleted and the source IP blocked.