# @keg-hub/cli-utils

> Utility methods for writing Custom Tasks consumed by the Keg-CLI

Latest version **0.9.0** (published 2023-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @keg-hub/cli-utils
pnpm add @keg-hub/cli-utils
yarn add @keg-hub/cli-utils
bun add @keg-hub/cli-utils
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2023-08-03 |
| First published | 2021-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 16.0.0 |
| Dependencies | 7 |
| Unpacked size | 152.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lance Tipton |
| Maintainers | lktipton |

## Links

- npm: https://www.npmjs.com/package/@keg-hub/cli-utils
- Repository: https://github.com/keg-hub/keg-cli.git#master
- Homepage: https://github.com/keg-hub/keg-cli/tree/master#readme
- Issues: https://github.com/keg-hub/keg-cli/issues
- npm.io page: https://npm.io/package/@keg-hub/cli-utils

## Dependencies (7)

- [esbuild](https://npm.io/package/esbuild.md) 0.17.6
- [fs-extra](https://npm.io/package/fs-extra.md) 9.0.1
- [app-root-path](https://npm.io/package/app-root-path.md) 3.0.0
- [@keg-hub/jsutils](https://npm.io/package/@keg-hub/jsutils.md) >=9.6.1
- [esbuild-register](https://npm.io/package/esbuild-register.md) 3.4.2
- [@keg-hub/spawn-cmd](https://npm.io/package/@keg-hub/spawn-cmd.md) >=0.3.2
- [@keg-hub/args-parse](https://npm.io/package/@keg-hub/args-parse.md) >=10.0.1

## Recent versions

- 0.9.0 (latest) — 2023-08-03
- 0.8.0 — 2023-07-29
- 0.8.0-alpha1 — 2023-03-14
- 0.8.0-alpha — 2023-03-14
- 0.7.0 — 2022-07-17
- 0.6.1 — 2022-06-05
- 0.6.0 — 2022-03-10
- 0.5.1 — 2022-01-14
- 0.5.0 — 2022-01-05
- 0.4.2 — 2021-11-21
- 0.4.1 — 2021-11-13
- 0.4.0 — 2021-10-29
- 0.3.0 — 2021-06-04
- 0.2.2 — 2021-05-26
- 0.2.1 — 2021-05-18
- … 4 more at https://npm.io/package/@keg-hub/cli-utils/versions

## README

# Keg-Hub Cli-Utils
* Common utility methods for developing Node CLI's and writing custom tasks
* This makes the process faster easier, and removes code duplication across projects
## Install
With **yarn**
```bash
yarn add @keg-hub/cli-utils
```
With **npm**
```bash
npm install @keg-hub/cli-utils
```
### CLI task setup
* `runTask` - Find and execute a task
  * This method allows resolving a task based on the passed in tasks object and arguments
  * It's recommended to add a call it from a node script within your project
  * Then add a reference to that script in your `package.json - scripts` section
    * This allows calling it from a package manager such as `yarn` or `npm` like this =>
      ```js
        // In your package.json file
        "scripts": {
          // ...other scripts
          "task": "node ./tasks/runTask.js",
        }
        
        // In tasks/runTask.js
        const { runTask } = require('@keg-hub/cli-utils')
        const taskDefinitions = require('./taskDefinitions')

        runTask(
          // Object containing all tasks definitions for the project
          taskDefinitions,
          // Default params passed to all tasks
          { env: process.env.NODE_ENV || 'local' }
        )
      ```
    * This above script can then be called like this => `yarn task <task-name> <task-options>`
      * Assuming a `start` task definition exists, running `yarn task start` will call the start task
  * This method will handle
    * Loading the global config defined at `~/.kegConfig/cli.config.json` if it exists
    * Parsing options relative to a task-definitions `options` property
      * They are then passed to the task as the `params` key of the `args` object  
* `setAppRoot` - Register a taps root directory
  * When calling custom tasks outside of the `keg-cli`, calling this method is **recommended**
    * It ensures that in `mono-repo` or `sym-linked` situations, the project root can be properly resolved
    * That said, in **most** cases calling this method is **NOT** needed
      * But, since nothing is inversely affected by it, it's recommended to set it and forget it
      * Add it to the top of a tasks script file like this =>
        ```js
          const { setAppRoot } = require('@keg-hub/cli-utils')
          setAppRoot(appRoot)

          module.exports = { customTask: { ...taskDefinition } }
        ```
      * See [tap-vistapps-app](https://github.com/keg-hub/tap-visitapps-app/blob/master/tasks/index.js) `tasks/index.js` file for an example
* `registerTasks` - Register Custom Tasks
  * In most situations calling `registerTasks` is **NOT** needed
    * It could be used when passing the tasks in the first argument of `runTask` is not possible
      * For example in a third-party package installed as a node module 
    * It must be called prior to the call to the `runTask` method to ensure the tasks are loaded
      * It takes a single `Object` argument that should contain key/value pairs task name/definitions
        For example => `registerTasks({ taskName: { ...taskDefinition } })`

---
_Source: https://npm.io/package/@keg-hub/cli-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
