sindresorhus / read-package-up

Read the closest package.json file
MIT License
258 stars 18 forks source link

Proposal: add support for path argument instead of object #4

Closed DaniGuardiola closed 5 years ago

DaniGuardiola commented 6 years ago

This is the current behavior:

Current behavior

Code

// example-module/example-dir/example.js
const readPkg = require('read-pkg')
const readPkgUp = require('read-pkg-up')
const pkgUp = require('pkg-up')

const test = async () => {
  console.log('__dirname\n')
  console.log(__dirname)

  console.log('\n=====================\n')

  console.log('pkgUp\n')
  await pkgUp(__dirname).then(console.log)

  console.log('\n=====================\n')

  console.log('pkgUp + readPkg\n')
  await pkgUp(__dirname).then(readPkg).then(console.log)

  console.log('\n=====================\n')

  console.log('readPkgUp\n')
  await readPkgUp(__dirname).then(console.log)
}

test()

Output

__dirname

/home/dani/projects/example-module/example-dir

=====================

pkgUp

/home/dani/projects/example-module/package.json

=====================

pkgUp + readPkg

{ name: 'example-module,
  version: '1.0.0',
  description: 'Example project',
  main: 'main.js',
  readme: 'ERROR: No README data found!',
  _id: 'example-module@1.0.0' }

=====================

readPkgUp

{}

Proposal

In my view, readPkgUp should behave just like pkgUp + readPkg for consistency, and both executions above should return the correct package.json object.

This could be implemented as a non-breaking change, because the options object can still work fine but the module could detect whether the input argument is an object or a string and work with both formats.

sindresorhus commented 6 years ago

This module accepts an object with options, not a string. See the docs.

DaniGuardiola commented 6 years ago

I see, well my bad and thanks for pointing it out. However, this is not coherent with the other modules such as readPkg or pkgUp, I suggest changing it (or adding support for a string argument additionally to the the options object) for consistency.

DaniGuardiola commented 6 years ago

In fact, I'll change this to make the issue a proposal instead of a bug.

sindresorhus commented 6 years ago

readPkg has a string argument as it's reads better readPkg(packagePath), while readPkgUp finds the package path for you, so it's different. The inconsistent one here is pkgUp, which should accept and object with options instead of string.

I think this module should at least validate the input argument though. That would have caught your issue.