pk

1.1.0 • Public • Published

npm npm Build Status GitHub issues GitHub stars GitHub license Twitter

pk logo

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 bashcompgenerates 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

Get the main field

$ pk main
index.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 scripts
start   node server.js
build   webpack .

Just the script names (object keys -k):

$ pk scripts -k
start
build

Just the values:

$ pk scripts -v
node server.js
webpack .

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 server
start   node server.js

Nested objects

package.json:

{
  ...
  config: {
    port: 8080
  }
}

Get a particular config (port in this case):

$ pk config.port
8080

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 keywords
node
cli
config
CI

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 | sort
CI
cli
config
node

Get the type of something:

$ pk -t keywords
array

Or the type of an element:

$ pk -t keywords[0]
string

If a field doesn't exist, undefined will be printed:

$ pk -t license
undefined

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
{
    "name""Alex"
    "city""Stockholm",
    "keywords": [
        "javascript",
        "golang",
        "vuejs"
    ]
}

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

Count the number of devDependencies

package.json:

{
    "devDependencies": {
         "mocha": "*",
         "babel": "*",
         "micromustache": "*",
         "webpack": "*",
    }
}
$ pk devDependencies -c
4

package-lock.json is nutorious!

$ pk -i package-lock.json dependencies -c
2739

If you're referring to an array, it'll return the size of the array:

$ pk -c keywords
3

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
{
  "type""git",
  "url""git+https://github.com/userpixel/pk.git"
}

Working with versions

package.json:

{
    "version": "1.2.3"
}

Just get the version string:

$ pk version
1.2.3

Parse it as semver:

$ pk -s version
major   1
minor   2
patch   3

You can actually omit "version" part if that's where it is:

$ pk -s
major   1
minor   2
patch   3

Yep you can get it in JSON format if you want:

$ pk -s version
{
  "major": 0,
  "minor": 2,
  "patch": 4
}

It understands watever semver can parse. So if the version was "4.3.2-beta.2+build1000"

$ pk -s
major 4
minor 3
patch 2
build ["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

Dependents (0)

Package Sidebar

Install

npm i pk

Weekly Downloads

53

Version

1.1.0

License

MIT

Unpacked Size

17.8 kB

Total Files

7

Last publish

Collaborators

  • simenb
  • userpixel