-
Create project
npm init
-
Create exectuable script
mkdir bin
-
Create file cli.js
module.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
-
Update pakcage.json to include the bin file :
"bin": {
"npm-cli-app": "bin/npm-cli-app.js"
},
-
Install project locally for testing
npm link
-
Now run and test your command :
npm-cli-app hello
-
Create file src/app.js
module.exports = {
run : (parameters) => {
console.log("running app with : ", parameters)
}
};
-
Update cli.js
to run app and pass only user provided data
const app = require('./src/app');
module.exports = {
run : (args) => {
app.run(args.slice(2));
}
};
-
Now run out comamnd again
npm-cli-app hello
-
publish to npm
npm login
npm publish