1.0.0 • Published 5 years ago
npm-cli-app v1.0.0
Creating and publishing cli apps with npm
Create project
npm initCreate exectuable script
mkdir binCreate file
cli.jsmodule.exports = { run : (args) => { console.log("running command with ", args); } };Create file
bin/npm-cli-app.js#!/usr/bin/env node require('../cli').run(process.argv);Run and check the script file
node bin/npm-cli-app.js hello # running command with [ # '/Users/me/.nvm/versions/node/v14.5.0/bin/node', # '/Users/me/projects/npm-cli-app/bin/npm-cli-app.js', # 'hello' # ]Update pakcage.json to include the bin file :
"bin": { "npm-cli-app": "bin/npm-cli-app.js" },Install project locally for testing
npm link # /Users/me/.nvm/versions/node/v14.5.0/bin/npm-cli-app -> /Users/me/.nvm/versions/node/v14.5.0/lib/node_modules/npm-cli-app/bin/npm-cli-app.js # /Users/me/.nvm/versions/node/v14.5.0/lib/node_modules/npm-cli-app -> /Users/me/projects/npm-cli-appNow run and test your command :
npm-cli-app hello # running command with [ # '/Users/dawn/.nvm/versions/node/v14.5.0/bin/node', # '/Users/dawn/.nvm/versions/node/v14.5.0/bin/npm-cli-app', # 'hello' # ]Create file
src/app.jsmodule.exports = { run : (parameters) => { console.log("running app with : ", parameters) } };Update
cli.jsto run app and pass only user provided dataconst app = require('./src/app'); module.exports = { run : (args) => { app.run(args.slice(2)); } };Now run out comamnd again
npm-cli-app hello # running app with : [ 'hello' ]publish to npm
npm login # Enter you username/password npm publish
1.0.0
5 years ago