@gld5000-cli/npx-demo
v1.0.1
Published
A hello world style demo of using NPX within an NPM package
Maintainers
Readme
npx-demo
This repo is a simple demo of how to implement NPX functionality within an NPM package.
Test it out
Hello world:
npx @gld5000-cli/npx-demo
Hello user (passing a variable):
npx @gld5000-cli/npx-demo --name "your user name"
Create your own
Create a repo
E.G. npx-demoCreate a function
mkdir src
touch src/index.js
module.exports = function run(name) {
if (name) {
console.log(`Hello, ${name}`);
} else {
console.log('Hello World');
}
};Create a bin
mkdir bin
touch bin/npxDemo.js#!/usr/bin/env node
function getNameArg() {
const args = process.argv.slice(2);
// --name=Alice
const eqArg = args.find(arg => arg.startsWith('--name='));
if (eqArg) {
return eqArg.split('=')[1];
}
// --name Alice
const nameIndex = args.indexOf('--name');
if (nameIndex !== -1 && args[nameIndex + 1]) {
return args[nameIndex + 1];
}
return null;
}
const name = getNameArg();
require('../src/index')(name);
Make the bin file executable across operating systems
chmod +x bin/npxDemo.jsPublish your NPM package
npm publish --access publicTest it out
npx @gld5000-cli/npx-demo