Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/sayjava/phake

Effortlessly generate realistic mock data with Ease using Handlebars and fakerjs
https://github.com/sayjava/phake

fake-data faker-generator handlebars mock mocking-utility sample-data sample-data-generator

Last synced: about 11 hours ago
JSON representation

Effortlessly generate realistic mock data with Ease using Handlebars and fakerjs

Awesome Lists containing this project

README

        

Phake


Effortlessly Generate Realistic Mock Data with Ease using Handlebars and Fakerjs


Try it online

## Features

- Utilize the power of declarative templates using
[Handlebars](https://handlebarsjs.com).
- Seamlessly integrate with [Faker.js](https://fakerjs.dev) for generating
realistic and randomized data.
- Enjoy the simplicity of a command-line interface for streamlined usage.
- Incorporate the tool into your development process and easily version your
templates.
- Promote code reuse and maintainability by creating DRY (Don't Repeat Yourself)
templates, allowing you to reuse template sections across multiple templates.
- Leverage custom Handlebars helpers to simplify data creation and manipulation,
further enhancing the flexibility and functionality of your templates.

## Quick Start

Create a template file named `sample.json.hbs` with the following content

```template filename="sample.json.hbs"
{
"id": {{faker 'number.int' 10}},
"node_id": "{{faker 'string.alpha' 25}}",
"name": "{{faker 'word.noun'}}",
"language": "{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
"forks_count": {{faker 'number.int' max=10000}},
"size": {{faker 'number.float' max=10000 precision=0.2}},
"default_branch": "{{randomize 'main' 'master'}}",
"open_issues_count": {{faker 'number.int' max=50}},
"is_template": {{randomize false true}},
"description": "{{faker 'lorem.sentences' max=3}}",
"topics": [
{{#repeat 5}}
"{{faker 'string.alpha' 5}}"
{{/repeat}}
]
}
```

Run the `phake-cli` command to compile the template into user.json

```shell
npx @sayjava/phake-cli compile -t sample.json.hbs
```

```json
{
"id": 7,
"node_id": "HSUWMxwXsVKxycdaVOiJFnlNe",
"name": "factory",
"language": "javascript",
"forks_count": 4558,
"size": 8800,
"default_branch": "master",
"open_issues_count": 9,
"is_template": true,
"description": "Aliquam sunt eveniet quam",
"topics": [
"UiqiJ",
"GdyVl",
"LUiDO",
"QEHha",
"Ksvjv"
]
}
```

## Phake Compile

The Phake CLI uses [Handlebars](https://handlebarsjs.com) and
[FakerJS](https://fakerjs.dev) to generate sample data for development purposes.
It allows you to define data templates using Handlebars syntax and generate
realistic sample data based on those templates, incorporating fake data
generated by [FakerJS](https://fakerjs.dev).

### Installation

```shell
npm i -g @sayjava/phake-cli
```

### CLI Options

| Flag | Default | Description |
| -------------- | ------- | --------------------------------------- |
| -t, --template | - | file/directory containing `.hbs` files |
| -o, --output | cwd | The directory to write the output files |

## File Naming

Improve the functionality of the `phake` CLI with the following enhancements:

- The `phake` CLI now intelligently uses file extensions to name the output
files that are written to the output directory.
- If no file extension is supplied, the output file will still be created but
without any file extension.
- This flexible approach allows you to specify the desired file extension for
the output, providing compatibility with various file formats and ensuring
clarity in file naming.
- You can now generate output files with the appropriate extensions, making it
easier to identify and work with the generated files.
- This enhancement adds versatility to the `phake` CLI, catering to a wider
range of use cases and preferences.

Examples

| Input | Output |
| --------------- | ----------- |
| sample.json.hbs | sample.json |
| sample.yaml.hbs | sample.yaml |
| sample.hbs | sample |

### Usage

```shell
phake compile -t name-of-template.hbs
```

Compile a folder containing `.hbs` files

```shell
phake -t dir/containing-hbs-files -o folder/to/write/output
```

Compile a single `[name].hbs` file

```shell
phake -t dir/containing/single.hbs -o folder/to/write/output
```

Compile a single `[name].json.hbs` file with an explicit file extension. This
will create `[name].json`

```shell
phake -t dir/containing/single.json.hbs -o folder/to/write/output
```

## DRY Templates with Handlebar's Partials

- Enable easy reuse of template sections by naming them as `[name].partial.hbs`.
- Partial files are not compiled individually but instead registered as
Handlebar partials. This allows them to be referenced and included within
other template files seamlessly.
- By adopting this approach, you can efficiently organize and manage your
templates, promoting modular and reusable code.
- The registration of partials ensures that changes made to a partial file
reflect automatically across all templates that reference it.
- This method of partial registration enhances maintainability and reduces
duplication of code within your project.

Example

```hbs
{
"title": "{{ faker "company.name" }}",
"price": "{{ faker "commerce.price" min=100 max=200 dec=0 symbol='£' }}"
}
```

The partial can then be referenced from other template files

```hbs
{
"items": [
{{#repeat 3}}
{{> item }}
{{/repeat}}
],
}
```

## Template Helpers

`phake` ships with some handy helpers that eases data generation. All
[Handlebar Helpers](https://handlebarsjs.com/guide/builtin-helpers.html) are
also supported.

| Helper | Description | Example | options |
| ----------- | ----------------------------------------------------------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------- |
| repeat | Generates a comma separated list for creating arrays | `[{{#repeat 5}} index-{{@index}} {{/repeat}}]` | `number` Iteration count |
| for | Repeat items for the specified range | `{{#for 1 5}} index-{{@index}} {{/for}}` | `number` Iteration count |
| randomize | Randomize an array | `{{randomize "german shepard" "golden retriever" "pug"}}` | `values` to be randomized |
| var | Register a variable to be used later | `{{#var 'myVar' 5 }}` then accessed later as `{{myVar}}` | `name` of the variable, `value` of the variable |
| imageURI | Generate a URI encoded random patterned image using a seed string | `{{imageURI 'seedString' }}` | `string` to use to seed the generated pattern, `color` to use for the pattern |
| imageURL | Generate a URL encoded random patterned image using a seed string | `{{imageURL 'seedString' #00FF00 }}` | `string` to use to seed the generated pattern, `color` to use for the pattern |
| imageSVG | Generate a SVG random patterned image using a seed string | `{{imageSVG 'seedString' }}` | `string` to use to seed the generated pattern, `color` to use for the pattern |
| imageBase64 | Generate a base64 encoded random patterned image | `{{imageBase64 'seedString' }}` | `string` to use to seed the generated pattern, `color` to use for the pattern |