Repository URL to install this package:
|
Version:
3.6.0-rc.2-68fa2bf8110fe4 ▾
|
#!/usr/bin/env node
/**
* RUNBO, FRONTEND-CONFIG COMMAND RUNNER
*
* This script is exposed as binary and allows you to run custom commands
* in the frontend-config's /commands folder or in your project's /commands folder.
*
* How to use:
* $ runbo <command-name> <arg1> <arg2>=<arg2-value> ... <argN>
*
* The <command-name> is the name of the command's script file without ".js" but
* including the path. For example, if I want to run the command /commands/foo/bar.js
* I need to write:
* $ runbo foo/bar
*
* A command in the frontend-config module can be overridden by creating a command
* script file with the same path in the project folder.
*
* Commands need to expose a function which takes a map of arguments as parameter.
* Arguments can have a value if they are provided in the form <key>=<value>.
* If a value is not provided, it will be set to "true" by default.
*
* For example, I could run:
* $ runbo examples/my-command foo bar="Test bar"
* This will expose the following arguments object:
* { foo: true, bar: "Test bar" }
*
* Following is an example of how the command could be defined:
*
* @example
* // ./commands/examples/my-command.js
* module.exports = ({ foo = false, bar = 'hello'}) => {
* if (foo) {
* console.log(bar);
* }
* };
*/
const { existsSync } = require('fs');
const path = require('path');
const [, , commandName, ...args] = process.argv;
if (!commandName) {
throw new Error('Error: You must provide the name of the command to run.');
}
// Convert the arguments map into an object
const argsMap = args.reduce((accumulator, currentArg) => {
const [argumentName, argumentValue] = currentArg.split('=');
// If there is no value set, default to true
accumulator[argumentName] = argumentValue || true;
return accumulator;
}, {});
// Commands into the project's path override the command from the frontend config module
const projectCommandPath = `${path.resolve('commands', commandName)}.js`;
const frontendConfigCommandPath = `${path.resolve(__dirname, commandName)}.js`;
if (existsSync(projectCommandPath)) {
// eslint-disable-next-line import/no-dynamic-require, global-require
require(projectCommandPath)(argsMap);
} else if (existsSync(frontendConfigCommandPath)) {
// eslint-disable-next-line import/no-dynamic-require, global-require
require(frontendConfigCommandPath)(argsMap);
} else {
throw new Error(`Command not found: ${commandName}`);
}