{"name":"@maartennnn/cli-builder","version":"2.2.0","description":"A cli builder tool","main":"src/index.js","scripts":{"test":"NODE_ENV=testing mocha --exit","test:watch":"NODE_ENV=testing mocha -w","publish":"npm publish --access public --scope=@maartennnn","jsdoc2md":"jsdoc2md src/index.js 2>&1| tee API.md","jsdoc":"jsdoc src/index.js","preversion":"npm run test && npm run jsdoc2md && git add . && git commit -m 'Generated API Docs'","postversion":"git push && git push --follow-tags && npm run publish"},"bin":{"cli-builder":"bin/example-cli.js"},"repository":{"type":"git","url":"git+https://github.com/maarteNNNN/cli-builder.git"},"bugs":{"url":"https://github.com/maarteNNNN/cli-builder/issues"},"homepage":"https://github.com/maarteNNNN/cli-builder#readme","keywords":["cli","command"],"author":{"name":"Maarten Coppens"},"license":"MIT","dependencies":{"fs-extra":"^10.1.0","inquirer":"^8.2.4","minimist":"^1.2.6"},"devDependencies":{"chai":"^4.3.6","chai-like":"^1.1.1","chai-things":"^0.2.0","jsdoc-to-markdown":"^7.1.1","mocha":"^10.0.0"},"gitHead":"cc08dacb0dd4337bf1b15f4851d08aa97d96b3a3","_id":"@maartennnn/cli-builder@2.2.0","_nodeVersion":"16.17.0","_npmVersion":"8.18.0","dist":{"integrity":"sha512-zWX/bfAAGscUsL2y/tinDpdKtaSZGMOSg4oi4cQrbW1EPITm4ROXZthbrDn+US+dQGQg7gH5/6ocyR+H6/eLYg==","shasum":"eda2a5eed7ebcfbd45f85e679039b1a6fef27138","tarball":"http://registry-npm.ywwl.com/@maartennnn/cli-builder/download/@maartennnn/cli-builder-2.2.0.tgz","fileCount":13,"unpackedSize":55254,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICg1f4hKG/w8stL/bbmvnZXaSnvxbe2RbHc4g50jBDXlAiEA173XILJK9VAiSefzQKY1BeKq8l0ehTSbiL7AVHzTKEI="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjyWzhACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpgSxAAj9VRN/bNA0SE/AyEyLHD6zlmuZh1rnHbutGXqMqPp5SW543h\r\nAyaZO2yo5YYElCJTWIKkEeIr8jIrSJioRSf+aNuxzNtfaEFtfkL8zel4vyGx\r\nz5XDnRyk4b/2kW/DIfY1jqU6FaV2j4atO0tJ8K4s0vQ+24Yaz54/ZAWuX1Eo\r\nAIEC9BYxnJ5Fl8ynhytCPTePeKEDoR3k/SNlwM63D/chE51SMqnMzOHiKadb\r\nq/NxxzupeWOqGqzAsMbNfdSUZogU5XExyLy6OXmIK47xzn9QabNPnIHAktul\r\nDgOmQP0/6dBFDgkgmv2CahXCNjV/NUyjtkdKAuGrDQcTisQvh6fvYypHKnlq\r\n6dloLwGj0XWPC25VlEW8BpYANnJQb1fh6DmZNYDUwx1PuHOOBNXblx6O3wJv\r\ninoTUTWtu82sLEnVtJqFqH5pSgelg5wpqZ4+/JULm1/Ta7lNWqL/JMgsrJtM\r\nXtEbyvLAV5u/kPyc3jl6u8C6arccDkIXHRCRcE4yXk8kh6hbdGPSK9xHZrnf\r\n9vdAetDRr+Gr7jHYyJxrBeSBzCcfsmm/18yROm96whccEbJvl+mzQiEp4Eyd\r\nTHw/QEwfEIeBlnWgA/SizNr4tAwKxcj2F5PJ7qgRkbM+I4L8YK2Lnsj2wndD\r\n7t83sTdOPIPepU+2UGLasx74T4UishnPC6Q=\r\n=GURV\r\n-----END PGP SIGNATURE-----\r\n","size":14177,"noattachment":false,"key":"/@maartennnn/cli-builder/-/@maartennnn/cli-builder-2.2.0.tgz"},"_npmUser":{"name":"maartennnn","email":"martinuske@gmail.com"},"directories":{},"maintainers":[{"name":"maartennnn","email":"martinuske@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/cli-builder_2.2.0_1674144992990_0.2979775104374818"},"_hasShrinkwrap":false,"readme":"# CLI-Builder\n\n[API Docs](API.md)\n\n## Trying out an example\n\nThis example can be found in `bin/example-cli.js`\n\n```sh\ngit clone https://github.com/maarteNNNN/cli-builder.git\nnpm install\nnpm link\ncli-builder help\nnpm unlink # to delete the cli-builder bin\n```\n\n## Installing\n\n```\nnpm install cli-builder\n```\n\nBe sure to take a look at `bin/example-cli.js`\n\nInstantiate a new CLI with `new REPLClient({ ...options })` `REPL` stands for `Read Eval Print Loop`\n\ndefining an `commands` Object as shown below in [below](#commands-example)\n\n```js\ncli.run(commands);\n```\n\n## Commands example\n\n```js\n// DO NOT ADD HELP TO THE ROOT OBJECT. THIS IS DYNAMICALLY MOUNTED\nconst commands = {\n  test: {\n    execute: () => console.log('this is the test run'),\n    help: 'help of test',\n    testing: {\n      execute: ({ argument, options }) =>\n        console.log('ARGUMENT: ', argument, '\\nOPTIONS: ', options),\n      help: 'testing help',\n      input: '<arg-to-pass-to-execute-function>', // argument and options are passed as an object passed to execute function\n      // cmd test testing IAMPASSEDTOFUNCTION -f\n      options: [{ option: 'f', help: 'Follow the logs' }],\n    },\n    testing2: {\n      execute: () => console.log('executing testing2'),\n      help: 'testing2 help',\n    },\n  },\n  test2: {\n    execute: () => console.log('this is the test2 run'),\n    help: 'help of test2',\n  },\n  deep: {\n    nesting: {\n      works: {\n        as: {\n          // Without help\n          command: () =>\n            console.log('to run this type `deep nesting works as command`'),\n          // Or with help\n          // command: {\n          //   // this get executed as `deep nesting works as command`\n          //   execute: () => console.log('to run this type `deep nesting works as command`'),\n          //   // this get executed as `deep nesting works as command help`\n          //   help: 'help of command'\n          // }\n        },\n      },\n    },\n  },\n  runSomeFunction: async () => {\n    // DO SOME INSANE LOGIC HERE\n  },\n  // A more comprehensive example below titled: Defining help with only one execute function\n  exampleWithOneExecute: {\n    knownCommand: {\n      help: 'Help for this known command',\n    },\n    // Show the user there are other commands available\n    'any-yet-unknown-property': {\n      help: 'Help for ANY UNKOWN PROPERTY',\n    },\n    async execute({ argument, options }) {\n      console.log(this); // Mounts the REPLClient dynamically\n      console.log(param); // Passes the last given argument/param dynamically\n      console.log(options); // Passes the last given options/flags dynamically\n    },\n  },\n};\n```\n\n---\n\n<span style=\"color:orange\">**WARNING**</span>\n\nHelp is dynamically mounted to the `commands` object. It generates a function which will mount all child `help` properties.\n\n---\n\n---\n\n**NOTE**\n\nRunning is going through the object and should be written as arguments the following way: `deep nesting works as command` for the [example above](#commands-example) (It executes the function or the `execute` child function in case help wants to be added).\n`deep nesting works as command --help` (executes `--help or -h` child function in case it's available)\n\n---\n\n---\n\n<span style=\"color:orange\">**WARNING**</span>\n\nfunctions are added as camelCase but are transformed to kebab-case:\n`run-some-function` will call result in `runSomeFunction`.\n\n---\n\n---\n\n<span style=\"color:red\">**ADVANCED EXAMPLE**</span>\n\n`actions` can be used to integrate imported files. You can check out the example of a full-fledged cli implementation in [ldpos-commander](https://github.com/Leasehold/ldpos-commander/) or [SocketCluster](https://github.com/SocketCluster/socketcluster).\n\nThe CLI (`REPLCient`) object is passed to the actions functions by [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)\n\nThe `bindActionArgs` are passed via [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) as well. Except if an argument is passed in the commands object function Eg.\n\n---\n\n```js\nsomeFunctionCmd: {\n  // CLI as this will be available but bindActionArgs won't be as argument replaces them\n  execute: (argument) => cli.actions.someFunction(argument),\n}\n```\n\n---\n\n## A more apprehensive example\n\n```js\nconst actions = {\n  getUserData = async (id, someFunction, aString, aNumber) => {\n    try {\n      const data = await axios.get(`user/${id}`)\n\n      console.log(this.argv) // logs all passed arguments with - and --\n\n      // this references to the cli object as it's bound\n      this.successLog(data)\n\n      someFunction()\n\n      console.log(aString, aNumber)\n    } catch (e) {\n      throw new Error(e)\n    }\n  }\n}\n\n// Binding below array to `options.bindActionArgs` in `new REPLClient({ ...options })`\nconst options = {\n  bindActionArgs = [123, () => console.log('function executed'), 'a string', 23123]\n}\n\nconst cli = new REPLClient(options)\n\nconst commands = {\n  // No arguments are passed here, they are mounted dynamically\n  anActionTest: async () => await cli.actions.getUserData()\n}\n\ncli.run(commands)\n```\n\nusing `cli-builder an-action-test` will execute the `getUserData` function with the `bindActionArgs` parameters bound to it.\n\n## Defining `help` with only one `execute` function\n\nWhen you want to dynamically output to the console Eg. get a JSON Object from an API with undefined structure but you want to be able to log any of those properties to the console it can be done via:\n\n```js\nconst commands = {\n  entrypoint: {\n    any: {\n      help: 'Help for any',\n    },\n    extra: {\n      help: 'Help for extra',\n    },\n    helping: {\n      help: 'Help for helping',\n    },\n    commands: {\n      help: 'Help for commands',\n    },\n    'any-unknown-property': {\n      help: 'Help for ANY UNKOWN PROPERTY',\n    },\n    async execute(param) {\n      param = cli.kebabCaseToCamel(param);\n\n      // We don't want the paging info in this case\n      const {\n        data: { data },\n      } = axios.get('https://reqres.in/api/users?page=2');\n\n      // If null it could be accessible, if undefined we know for sure it isn't\n      if (!data[param] === undefined)\n        throw new Error('Custom property not found.');\n\n      // The `REPLCient` is object is dynamically mounted\n      this.successLog(data[param], `${param}:`);\n    },\n  },\n};\n```\n","publish_time":1674144993165,"_cnpm_publish_time":1674144993165,"dist-tags":{"latest":"2.3.0"}}