Introduction
pk
is a small utility CLI for querying JSON files like package.json
or manifest.json
etc.
- Get the value of a particular field 💪
pk scripts.start
- Parse semver versions 🐍
pk -s version
- Minify json 🐭
pk -m
- Beautify json 🐘
pk -j
- Get the keys in an object 🔑
pk -k scripts
- Get the size of an array or number of keys in an object 🌮
pk -c dependencies
- Get a part of a json file ✂
pk -j repository
- Check if a path exists in the json and what type it is 🎁
pk -t keywords
- The default output is compatible with Unix text processing utils 👓 (
wc
,sort
,grep
,uniq
,comm
,shuf
,paste
,column
,pr
,fold
,cmp
,diff
, etc.) - Tiny, super quick, 100% Javascript 🦄
- Autocomplete (see 👇)
- Correction suggestion (typos in the path) 😅
By default it operates on package.json where its name comes from but you can specify
any input file using the -i FILE.json
option.
Install
$ npm i -g pk
Now you can run it from the terminal. Check out the command reference to see what you can do with it:
$ pk --help
Install command line completion (optional)
pk bashcomp
generates the bash auto complete command
You need to add this script to your bash initialization:
- On Linux:
$ pk bashcomp >> ~/.bashrc
- On Mac OS X:
$ pk bashcomp >> ~/.bash_profile
- Windows Subsystem for Linux:
$ pk bashcomp >> ~/.bashrc
Then you need to restart a bash session for this to take effect.
Examples
main
field
Get the $ pk mainindex.js
If there is no main field nothing will be returned.
Working with objects
package.json
:
"scripts": "start": "node server.js" "build": "webpack ."
Get the list of all scripts along with their commands:
$ pk scriptsstart node server.jsbuild webpack .
Just the script names (object keys -k
):
$ pk scripts -kstartbuild
Just the values:
$ pk scripts -vnode server.jswebpack .
pk is designed with Unix philosophy in mind and plays nice with other tools. Want to see which script has the word "server" in it? Grep it:
$ pk scripts | grep serverstart node server.js
Nested objects
package.json
:
... config: port: 8080
Get a particular config (port
in this case):
$ pk config.port8080
You can also use autocomplete to see what is available. Just press TABTAB after istalling the command line completion script.
Working with arrays
package.json
:
keywords: "node" "cli" "config" "CI"
Get a particular item:
$ pk keywords[2]config
Get all items:
$ pk keywordsnodecliconfigCI
Get it in json format:
$ pk keywords -j[ "node", "cli", "config", "CI"]
Or even minified:
$ pk keywords -j["node","cli","config","CI"]
By default the output is unix compatible so you can pipe it:
$ pk keywords | sortCIcliconfignode
Get the type of something:
$ pk -t keywordsarray
Or the type of an element:
$ pk -t keywords[0]string
If a field doesn't exist, undefined
will be printed:
$ pk -t licenseundefined
Minify a json file
There's no magic! It just uses native JSON module without padding.
original.json
:
"name": "Alex" "city": "Stockholm"
Minify it and show the output:
$ pk -i original.json -m{"name":"Alex","city":"Stockholm"}
Write the output to a file:
$ pk -i original.json -m > original.min.json
Prettify a minified or badly formatted JSON
original.json
:
"name": "Alex" "city": "Stockholm" "keywords": "javascript" "golang""vuejs"
Show it pretty on screen:
$ pk -i original.json -j
If the output is too big you may wanna browse it on the terminal:
$ pk -i original.json -j | less
Or just write it to a file:
$ pk -i original.json -j > original-prettified.json
Even overwrite the original file:
$ pk -i original.json -j > original.json
devDependencies
Count the number of package.json
:
"devDependencies": "mocha": "*" "babel": "*" "micromustache": "*" "webpack": "*"
$ pk devDependencies -c4
package-lock.json
is nutorious!
$ pk -i package-lock.json dependencies -c2739
If you're referring to an array, it'll return the size of the array:
$ pk -c keywords3
Get part of a JSON file
package.json
:
... "repository": "type": "git" "url": "git+https://github.com/userpixel/pk.git"
Get the value of the repository:
$ pk -j repository
Working with versions
package.json
:
"version": "1.2.3"
Just get the version string:
$ pk version1.2.3
Parse it as semver:
$ pk -s versionmajor 1minor 2patch 3
You can actually omit "version" part if that's where it is:
$ pk -smajor 1minor 2patch 3
Yep you can get it in JSON format if you want:
$ pk -s version
It understands watever semver can parse. So if the version was "4.3.2-beta.2+build1000"
$ pk -smajor 4minor 3patch 2build ["build1000"]prerelease ["beta",2]
Command Substitution
pk is ideal for CI/CD scripts and that was the original motivation for its creation. For example if you want to compress the current directory and version it you can:
$ zip -r `pk name`-`pk version`.zip .
This will zip the current directory to a file that is name NAME-VERSION.zip
where NAME
and VERSION
in the file name come from "name"
and "version"
fields in the local package.json
.
More
There's more. See the help for the command reference
$ pk --help`.
Update
# Check the version $ pk --version # Check if there's a new version $ npm outdated -g pk # Update it if needed $ npm i -g pk@latest`
Uninstall
$ npm un -g pk
License
MIT
Made in Sweden by @alexewerlof