Skip to content

Feature request: automatically use name, version and description fields in nearest package.json #2346

Open
@zanminkian

Description

@zanminkian

Support a method named usePackageJson. When it's called, commander use will use the nearest package.json file, and use its name, version and description filds as commander's name, version and description.

const program = new Command();
program.usePackageJson(import.meta.url, {name: true, version:true, descrition: true});

// Equivalent to:
const packageJson = getProjectPackageJson();
const program = new Command();
program.name(packageJson.name).version(packageJson.name).description(packageJson.description);

Why?

  1. Name and version are commonly used. Almost every cli tool need them. Commander is expected to simplify it.
  2. Using import packageJson from '../package.json' seems not a good prectice for developers. I think the source code should not rely on a json config outside of the source code base. For example, package.json is in the root, while the cli.js is in <root>/src directory. the code inside src rely a config outside of src. It seems break the edge.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions