
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
command-functions
Advanced tools
Create libraries that function as both CLI and js libraries at the same time
Create your own node libraries that work both as node libraries and CLI tools at the same time
First we create an object that maps our command names to their functions
const commands = {
favoriteColor: () => Math.random() < 0.75 ? 'purple': 'green'
}
First install command functions as a dependency
npm install --save command-functions
Next use the CommandFunctions class to convert your functions into a library
const CommandFunctions = require('command-functions/CommandFunctions')
const commands = {
favoriteColor: () => Math.random() < 0.75 ? 'purple': 'green'
}
const app = new CommandFunctions(commands)
// Detects whether being required or being run by the terminal
if (require.main === module) { // It's being run by the terminal
app.runCLI()
.then(() => {
console.log('Finished')
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
} else { // It's a module
module.exports = app.getExports()
}
When being run from the command line, Command Functions will parse your arguments internally using Minimist. Minimist will automatically parse all your arguments into their closest JSON approximation. Meaning the argument "age=[1,2,3]" will now make your function receive an options object, whose age property will be an Array containing the numbers 1 through 3. Primary arguments will be passed in before the options object, meaning the input syntax for your function should look something like this
function myFunction(firstPrimaryArg, secondPrimaryArg, options={}) {...}
Instead of providing a function as the handler for a given command in your command map, you can wrap your function inside of an object, providing the function as the "handler" property. In this case the rest of the properties are treated as options. Here's an example of what a command using this syntax might look like
const commands = {
favoriteColor: {
handler: () => Math.random() < 0.75 ? 'purple': 'green'
}
}
This is required for every command. It's a function that runs the command.
Set this to true if you would not like your command to accept any options.
A Sandhands format to validate that commands arguments. This can be used to ensure all of your arguments are formatted correctly. Each individual argument can be given it's own format, in which case the command might look like this
{
handler: someFunction,
args: {
name: {
format: String // Gets Angry if the name argument provided is not a string
}
}
}
To ensure you received a valid set of primary arguments, you could provide a format that looks something like this
{
_: [String],
secondaryOption: Boolean,
randomOption: Number
}
FAQs
Create libraries that function as both CLI and js libraries at the same time
The npm package command-functions receives a total of 10 weekly downloads. As such, command-functions popularity was classified as not popular.
We found that command-functions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.