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?
- Name and version are commonly used. Almost every cli tool need them. Commander is expected to simplify it.
- 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.