Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

pk

1.0.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 👇)

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 $ pk bashcomp >> ~/.bash_profile

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 scripts along with their commands:

$ pk scripts
start   node server.js
build   webpack .

Just the script names (the autocomplete feature comes handy here):

$ pk scripts -k
start
build

In JSON format:

$ pk scripts -j
{
    "start""node server.js",
    "build""webpack ."
}

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]

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

install

npm i pk

Downloadsweekly downloads

14

version

1.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability