Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pateketrueke/haki

Small generator with will powers
https://github.com/pateketrueke/haki

Last synced: 5 days ago
JSON representation

Small generator with will powers

Awesome Lists containing this project

README

        

# Haki

![Ryou Haki](https://cdn.lanetaneta.com/wp-content/uploads/2020/08/1597113488_One-Piece-revela-un-nuevo-nombre-para-Haki-780x470.webp)

[![NPM version](https://badge.fury.io/js/haki.svg)](http://badge.fury.io/js/haki)
[![travis-ci](https://api.travis-ci.org/pateketrueke/haki.svg)](https://travis-ci.org/pateketrueke/haki)
[![codecov](https://codecov.io/gh/pateketrueke/haki/branch/master/graph/badge.svg)](https://codecov.io/gh/pateketrueke/haki)

Small generator with will powers.

```bash
$ npx haki [-e FILE] [COMMAND] [...]
# or `npm i -g haki`
# or `yarn global add haki`
```

> Run `haki` without arguments to get usage hints.

## Example

Create a file named `Hakifile.js` in your project with this:

```js
module.exports = haki => {
haki.setGenerator('the:truth', {
description: "Display if it's true, or not",
arguments: ['verb', 'value'],
abortOnFail: true,
actions(input) {
const { verb, value } = input;

if (verb === 'is' && parseInt(value, 10) === 42) {
console.log('Gotcha!');
return;
}

throw new Error('Is not true');
},
});
}
```

Now you can execute this task through the CLI:

- `haki the:truth`
- `haki the:truth is 42`

## API

Available methods:

- `load(filepath: String)` — Load a Hakifile from given filepath
- `prompt(options: Object)` — Generic prompting helper, see options below
- `getPrompts()` — Returns [Prompts](https://github.com/terkelg/prompts) instance
- `getLogger()` — Returns [LogPose](https://github.com/pateketrueke/log-pose) instance
- `getPath(destName: String)` — Returns a filepath for output
- `addHelper(name: String, callback: Function)` — Register a [Mustache](https://github.com/janl/mustache.js/) helper for calling on templates
- `getHelperList()` — Retrieve all registered helpers
- `renderString(value: String, data: Object)` — Render template values
- `setGenerator(name: String[, options: Object])` — Register a generator definition, see options below
- `getGenerator(name: String)` — Retrieve a registered generator
- `runGenerator(name: String|Object[, defaults: Object])` — Execute any given generator; given `name` can be an object, see options below
- `hasGenerator(name: String)` — Returns `true` if given generator is defined
- `getGeneratorList([hints: Boolean])` — Retrieve all registered generators, if `hints` is given then descriptions are prefixed with their names
- `chooseGeneratorList([defaults: Object])` — Prompt to execute from registered generators, `defaults` are given as for `runGenerator()`

### Generators

Those can be registered or executed directly.

Valid options are:

- `description: String` — Displayed on `--help`
- `validate: Object|Function` — To validate input
- `arguments: Array` — Map extra `argv` as input
- `actions: Array|Function` — [See below](#actions)
- `prompts: Array|Function` — [See below](#prompts)
- `defaults: Object` — Initial values
- `quiet: Boolean` — Hide output from logs
- `basePath: String` — Resolve sources from here
- `abortOnFail: Boolean` — Abort whole process on failure

> Both `prompts` and `arrays` can be functions, once executed they should return an array to be used or nothing.

#### Actions

Enumerated actions can perform several tasks based on its definition:

- `modify` — Allow to rewrite a file performing regexp replacements.
- `copy` — Allow to between files and directories
- `add` — Allow to write new files
- `exec` — Invokes a shell instruction
- `clean` — Removes a file or directory
- `clone` — Downloads github repository
- `render` — Rewrite dynamic files or directories
- `extend` — Rewrite JSON file through extending it
- `install` — Get your dependencies through NPM or Yarn

Definitions can contain:

- `src: String` — Relative to generator's `basePath`
- `type: String` — Action type: add, copy, clean, etc.
- `dest: String` — Relative to `process.cwd()` for output
- `template: String` — Used when creating files
- `templateFile: String` — Source file used when creating files
- `defaultContent: String` — On `modify`, used if file does not exists yet
- `deleteContent: Boolean` — On `modify`, remove matched code instead of replacing it
- `after: String|RegExp` — As below, used to replace before the match
- `before: String|RegExp` — As below, used to replace after the match (alias of `pattern`)
- `pattern: String|RegExp` — On `modify`, used to match-and-replace
- `unless: String|RegExp` — On `modify`, used to skip matching code
- `content: String` — Like `template` but without Mustache support
- `gitUrl: String` — Used on `clone` actions, relative to github
- `callback: Function` — Used on `extend` actions to merge input
- `command: String` — Used on `exec` actions, as shell command
- `quiet: Boolean` — Override generator's `quiet` value
- `abortOnFail: Boolean` — Override generator's `abortOnFail` value

> Rendering means templates are evaluated from all matching files, including its contents if they're plain text.

Example:

```js
haki.runGenerator({
abortOnFail: true,
actions: [{ clone: 'pateketrueke/empty', dest: '/tmp' }],
});
```

> Notice `{ type: 'clone', src: 'foo/bar' }` and `{ clone: 'foo/bar' }` are equivalents
> — the used value for the given type is taken as its `src` option instead.

#### Prompts

User input is being done by **Prompts**, so any syntax supported is valid, e.g.

- `type: String` — Generator type 1
- `name: String` — Input name
- `message: String` — Optional label

Example:

```js
haki.prompt({
type: 'toggle',
name: 'user_confirmation',
message: 'Enable this feature',
});
```

> 1 Check which [types](https://github.com/terkelg/prompts#-types) are supported by default.

## Global usage

By design `haki` will scan for immediately available Hakifiles from the current working and other special directories.

Say, we are at `$HOME/path/to/project/name` so paths below are used:

- `/etc/.config/haki`
- `/etc/.hakirc`
- `/etc/Hakifile.js`
- `$HOME/.config/haki`
- `$HOME/.hakirc`
- `$HOME/Hakifile.js`
- `$HOME/path/to/project/name/.config/haki`
- `$HOME/path/to/project/name/.hakirc`
- `$HOME/path/to/project/name/Hakifile.js`
- `$HOME/path/to/project/.config/haki`
- `$HOME/path/to/project/.hakirc`
- `$HOME/path/to/project/Hakifile.js`
- `$HOME/path/to/.config/haki`
- `$HOME/path/to/.hakirc`
- `$HOME/path/to/Hakifile.js`
- etc. — scanning stops when `/` or `$HOME` path is reached.

## Gist usage

You can download and run remote gists too:

```bash
$ haki -g [SHA1]
```

List available gists with `haki -g` only.

## GitHub usage

Download github repositories with:

```bash
$ haki
```

> After downloading, haki performs a `render` action on the destination folder so all templates will be rendered as needed.