Git Product home page Git Product logo

finepack's Introduction

finepack

Last version Coverage Status NPM Status

Organizes and maintains your JSON files readable.

Finepack is a tool to keep your JSON files organized, especially if you are creating an open source project and want to be sure that your files have all the information that is required or recommended by the main package management systems (like bower or npm). This is what it can do:

  • Lints the JSON to be sure that it is in a valid format.
  • Validates the keys to make sure of the existence of required keys such as name or version, and other important keys such as homepage, main, license...
  • Organizes the JSON by moving the most important properties to the top.
  • Sorts the rest of the keys alphabetically and recursively using the JavaScript sort function (elements are sorted by converting them to strings and comparing strings in Unicode code point order).
  • Can be configured not to sort the arrays or objects at one or more user specified keys.
  • Can use a user-provided compare function to define the sort order.

You can use Finepack as a CLI tool or from NodeJS as a library. Based on fixpack but with a little more ♥.

Install

npm install finepack -g

Usage

CLI

$ finepack

  Organizes and maintains your JSON files readable.

  Usage
    $ finepack <fileJSON> [options]

    options:
     --no-validate             disable validation mode.
     --no-color                disable colors in the output.
     --sort-ignore-object-at   don't sort object(s) at these comma separated key(s).
     --sort-ignore-array-at    don't sort array(s) at these comma separated key(s).
     --version                 output the current version.

    examples:
     finepack package.json
     finepack bower.json --no-validate

API

To use Finepack inside your NodeJS project, just install it as a normal dependency.

const fs = require('fs')
const path = require('path')
const finepack = require('finepack')
const filepath = path.resolve('./package.json')
const filename = path.basename(filepath)
const filedata = fs.readFileSync(filepath, { encoding: 'utf8' })

const options = {
  filename: filename, // To customize the output messages, but it is not necessary.
  validate: false, // To enable (or not) keys validation (false by default).
  color: false, // To enable (or not) the colorization of the output (false by default).
  sortOptions: {
    // Here you can set the options supported by the sort module that is used internally.
    // SEE: https://github.com/Kikobeats/sort-keys-recursive#options
  }
}

finepack(filedata, options, function (err, output, messages) {
  if (err) throw err
  // if your JSON is malformed then you have an err
})

License

MIT © Kiko Beats

finepack's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar greenkeeperio-bot avatar iamstolis avatar kikobeats avatar matteocng avatar pdehaan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

finepack's Issues

An in-range update of acho is breaking the build 🚨

Version 4.0.2 of acho was just published.

Branch Build failing 🚨
Dependency acho
Current Version 4.0.1
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

acho is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 8 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Verify file exists before trying to lint it

In typing up the previous bug report (#11), I mistyped the command as $ finepack package (without the ".json" extension because I had multiple package* files in my directory and I was lazily using CLI auto-complete). I got the following error:

$ finepack package # instead of the correct "package.json"

fs.js:439
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^
Error: ENOENT, no such file or directory '/Users/pdehaan/dev/tmp/finepack-test2/package'
  at Object.fs.openSync (fs.js:439:18)
  at Object.fs.readFileSync (fs.js:290:15)
  at Object.<anonymous> (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/bin/index.js:31:19)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:929:3

$ echo $?
8

If I'm reading it correctly, it's coming from here:

/* 29: */ var filepath = path.resolve(cli.input[0]);
/* 30: */ var filename = path.basename(filepath);
/* 31: */ var filedata = fs.readFileSync(filepath, {encoding: 'utf8'});

We may want to verify that the file exists before attempting to open it barfing all over the Terminal.
Either checking if the file exists using fs.existsSync() or maybe wrapping the fs.readFileSync() in a try...catch block.


UPDATE: It looks like just wrapping the fs.readFileSync() in a try...catch block may be the answer (per Node docs):

fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.
In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there.
fs.exists() will be deprecated.
...
fs.existsSync() will be deprecated.

— via https://nodejs.org/api/fs.html#fs_fs_exists_path_callback

Cannot find module 'superb'

I tried installing finepack globally and running it on a local directory and got the following error:

Error: Cannot find module 'superb'

Steps to reproduce

$ npm i finepack --global
/Users/pdehaan/.npm-packages/bin/finepack -> /Users/pdehaan/.npm-packages/lib/node_modules/finepack/bin/index.js
[email protected] /Users/pdehaan/.npm-packages/lib/node_modules/finepack
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])


$ finepack

module.js:340
    throw err;
          ^
Error: Cannot find module 'superb'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/lib/Finepack.coffee:7:21)
  at Object.<anonymous> (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/lib/Finepack.coffee:1:1)
  at Module._compile (module.js:456:26)
  at Object.loadFile (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/node_modules/coffee-script/lib/coffee-script/register.js:16:19)
  at Module.load (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (/Users/pdehaan/.npm-packages/lib/node_modules/finepack/bin/index.js:6:16)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:929:3

I didn't see the superb dependency listed in ./package.json, but I did notice a reference in the stack trace to a require() in ./lib/Finepack.coffee:7.

An in-range update of acho is breaking the build 🚨

The dependency acho was updated from 4.0.2 to 4.0.3.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

acho is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 9 commits.

  • 34d858f Release 4.0.3
  • 1137ff3 Migrate husky
  • d2ca4e9 Fix lint
  • d189dce Merge pull request #51 from achojs/greenkeeper/pretty-ms-4.0.0
  • 8789d17 Update package.json
  • 9c05e2c fix(package): update pretty-ms to version 4.0.0
  • b94bda8 Improve meta
  • 7e2c32e Merge pull request #50 from achojs/greenkeeper/pretty-ms-3.2.0
  • f40bbe7 fix(package): update pretty-ms to version 3.2.0

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of acho is breaking the build 🚨

Version 4.0.1 of acho was just published.

Branch Build failing 🚨
Dependency acho
Current Version 4.0.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

acho is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • coverage/coveralls Coverage pending from Coveralls.io Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 20 commits.

  • 418e18f Release 4.0.1
  • f131dfa Tweaks
  • 788c60a Merge pull request #48 from achojs/decaffeinate
  • 6f27b6f Merge branch 'master' into decaffeinate
  • 366ff8b Drop node 4 support
  • e69b73f Remove coffeecript hook
  • 071f664 Remove coffeescript dependency
  • fe63678 Merge pull request #47 from achojs/greenkeeper/pretty-ms-3.1.0
  • d19b814 fix(package): update pretty-ms to version 3.1.0
  • 3517bbb Merge pull request #46 from achojs/greenkeeper/chalk-2.3.0
  • 84ed02f fix(package): update chalk to version 2.3.0
  • b022263 Merge pull request #45 from achojs/greenkeeper/chalk-2.2.0
  • 75bd815 fix(package): update chalk to version 2.2.0
  • 26596f2 Prefer HTTPS connections
  • e2da41a Update _coverpage.md

There are 20 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Disable chalk colors when using API?

I'm using the API example from the README file, and notice that my output has all the chalk color codes sprinkled through the messages. Not sure if there is a flag or setting to disable the colors when I'm using the API instead of having to pass the --no-color flag via the CLI.

$ node index
[ error: [],
  warning: [ '\u001b[33mwarning\u001b[39m: \u001b[90mmissing \'bugs\'\u001b[39m',
    '\u001b[33mwarning\u001b[39m: \u001b[90mmissing \'keywords\'\u001b[39m',
    '\u001b[33mwarning\u001b[39m: \u001b[90mmissing \'homepage\'\u001b[39m',
    '\u001b[33mwarning\u001b[39m: \u001b[90mmissing \'repository\'\u001b[39m' ],
  success: [],
  info: [ '\u001b[90minfo\u001b[39m: \u001b[1mpackage.json\u001b[22m is near to be \u001b[1mfine\u001b[22m. Check the file and run again.' ] ]

Versus:

$ node index --no-color
[ error: [],
  warning: [ 'warning: missing \'bugs\'',
    'warning: missing \'keywords\'',
    'warning: missing \'homepage\'',
    'warning: missing \'repository\'' ],
  success: [],
  info: [ 'info: package.json is near to be fine. Check the file and run again.' ] ]

Output looks the same with and without `-l` flag

Steps to reproduce:

$ finepack package.json



warning: missing 'bugs'
warning: missing 'keywords'
warning: missing 'homepage'
warning: missing 'repository'


info: package.json is near to be fine. Check the file and run again.

$ finepack package.json -l



warning: missing 'bugs'
warning: missing 'keywords'
warning: missing 'homepage'
warning: missing 'repository'


info: package.json is near to be fine. Check the file and run again.

Data gets lost when using JSON.stringify() to display messages

I'm seeing an issue where if I use the finepack module API and try and do console.log(JSON.stringify(messages) then I get an empty array back instead of a formatted object with error, warning, success, and info keys.

Steps to reproduce:

Here's my index.js file:

var fs = require('fs');
var path     = require('path');
var finepack = require('finepack');

var filename = path.basename(filepath);
var filepath = path.resolve('./package.json');
var filedata = fs.readFileSync(filepath).toString();

var options = {
  filename: filename,
  lint: true
};

finepack(filedata, options, function(err, output, messages){
  if (err) {
    console.error('err', err);
  }
  console.log('\nraw:');
  console.log(messages);
  console.log('\nJSON.stringify():');
  console.log(JSON.stringify(messages, null, 2));
});

And I run it via the CLI (and have to specify --no-color otherwise the output gets chalk data in the messages):

$ node index --no-color
err true

raw:
[ error: [],
  warning: [ 'warning: missing \'bugs\'',
    'warning: missing \'keywords\'',
    'warning: missing \'homepage\'',
    'warning: missing \'repository\'' ],
  success: [],
  info: [ 'info: undefined is near to be fine. Check the file and run again.' ] ]

JSON.stringify():
[]

As you can see, when I use JSON.stringify() on the messages, I get back an empty array. As close as I can tell, this is a slight issue in ./lib/Logger.coffee:10 where the constructor method sets @messages to an array (@messages = []), where we probably want that to be an object ({}). Changing that locally in my ./node_modules/ directory seems to have solved the issue for me.

lint `main` and `bin` file paths

I've seen this in a few of our project's lately, so it'd be nice if the finepack tool could check if the file specified in main and bin paths are valid (something like fs.existsSync() or whatever).

For example, I think by default npm sets the main script to "index.js". In my case, the index file is set to "indexxxx.js" which doesn't exist. It'd be nice if finepack could display a warning or error.

{
  "name": "espree-test",
  "description": "",
  "version": "1.0.0",
  "author": "peter",
  "dependencies": {
    "canonical-json": "0.0.4",
    "espree": "1.11.0"
  },
  "license": "WTFPL",
  "main": "indexxxx.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  }
}

An in-range update of acho-skin-cli is breaking the build 🚨

Version 2.0.1 of acho-skin-cli just got published.

Branch Build failing 🚨
Dependency acho-skin-cli
Current Version 2.0.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

acho-skin-cli is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪

Status Details
  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 2 commits.

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organiszation’s settings page, under Installed GitHub Apps.

properties aren't being sorted

Not sure if this is a regression, or expected or...

Steps to reproduce

  1. Go to a clean folder
  2. npm init
  3. Fill in all the fields.
  4. finepack package.json

Actual results:

Before finepack:

{
  "name": "finepack-test2",
  "version": "1.0.0",
  "description": "this is a desc",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "eslint ."
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/git/repo"
  },
  "keywords": [
    "keywords"
  ],
  "author": "Peter deHaan <[email protected]> (http://nodeexamples.com/)",
  "license": "WTFPL",
  "bugs": {
    "url": "https://github.com/git/repo/issues"
  },
  "homepage": "https://github.com/git/repo"
}

After finepack:

{
  "name": "finepack-test2",
  "description": "this is a desc",
  "homepage": "https://github.com/git/repo",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Peter deHaan <[email protected]> (http://nodeexamples.com/)",
  "repository": {
    "type": "git",
    "url": "https://github.com/git/repo"
  },
  "bugs": {
    "url": "https://github.com/git/repo/issues"
  },
  "keywords": [
    "keywords"
  ],
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "test": "eslint ."
  },
  "license": "WTFPL"
}

Expected results:

Sorted properties (per the documentation: "Sorts the rest of the keys alphabetically and recursively."):

{
  "name": "finepack-test2",
  "description": "this is a desc",
  "version": "1.0.0",
  "author": "Peter deHaan <[email protected]> (http://nodeexamples.com/)",
  "bugs": {
    "url": "https://github.com/git/repo/issues"
  },
  "dependencies": {},
  "devDependencies": {},
  "homepage": "https://github.com/git/repo",
  "keywords": [
    "keywords"
  ],
  "license": "WTFPL",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/git/repo"
  },
  "scripts": {
    "test": "eslint ."
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.