# @compgen/core

> This documentation will help you get started with writing your own generators. You can start writing your own generators in matter of minutes.

Latest version **0.9.0** (published 2021-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @compgen/core
pnpm add @compgen/core
yarn add @compgen/core
bun add @compgen/core
```

Provides the command `core`.

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.0 |
| Published | 2021-03-30 |
| First published | 2020-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=14 |
| Dependencies | 11 |
| Unpacked size | 60.4 KB |
| Known vulnerabilities | 0 (+12 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | michal.jarnot |

## Links

- npm: https://www.npmjs.com/package/@compgen/core
- npm.io page: https://npm.io/package/@compgen/core

## Dependencies (11)

- [ora](https://npm.io/package/ora.md) 5.1.0
- [chalk](https://npm.io/package/chalk.md) 4.1.0
- [ramda](https://npm.io/package/ramda.md) 0.27.1
- [yargs](https://npm.io/package/yargs.md) 16.1.0
- [figlet](https://npm.io/package/figlet.md) 1.5.0
- [rollbar](https://npm.io/package/rollbar.md) ^2.21.0
- [shelljs](https://npm.io/package/shelljs.md) 0.8.4
- [inquirer](https://npm.io/package/inquirer.md) 7.3.3
- [through2](https://npm.io/package/through2.md) 4.0.2
- [handlebars](https://npm.io/package/handlebars.md) 4.7.6
- [recursive-copy](https://npm.io/package/recursive-copy.md) 2.0.11

## Recent versions

- 0.9.0 (latest) — 2021-03-30
- 0.8.0 — 2021-03-27
- 0.7.3 — 2021-02-21
- 0.7.2 — 2021-02-21
- 0.7.1 — 2021-02-21
- 0.7.0 — 2021-02-21
- 0.6.0 — 2021-02-21
- 0.5.0 — 2021-02-20
- 0.4.0 — 2021-02-20
- 0.3.1 — 2021-02-20
- 0.3.0 — 2021-02-20
- 0.2.0 — 2021-02-19
- 0.1.3 — 2021-02-19
- 0.1.2 — 2021-02-19
- 0.1.1 — 2021-02-19
- … 3 more at https://npm.io/package/@compgen/core/versions

## README

# @compgen/core

This documentation will help you get started with writing your own generators. You can start writing your own generators in matter of minutes.

## Creating Prettier Generator

Personally I prefer practical examples. @compgen/core is small and thanks to typescript you can figure out pretty much everything yourself.

With that said I will create an actual documentation in the future.

### 1) Initialize builder

Every generator uses `builder` to generate `ISchema`. The schema holds information about what commands we want to run, what files we want to create and what dependencies we want to install.

```ts
import { builder } from '@compgen/core'

export const createPrettierSchema = () => {
  const schema = builder('prettier')
}
```

### 2) Add CLI Commands

If we want to create project directory, run create-react-app cli or run any other command we can use `addCommand` helper.

```ts
import { builder } from '@compgen/core'

export const createPrettierSchema = () => {
  const schema = builder('prettier')

  schema.addCommand({
    command: 'yarn init -y',
    successMessage: 'Initializing package.json',
    priority: 99,
    shouldRunInProject: true,
  })
}
```

### 3) Create files

If we want to add prettier to our project we need to create `.prettierrc.js`. We have to make directory `templates` and create config file:

```ts
// templates/.prettierrc.js
module.exports = require('@linters/prettier-config')
```

then we just have to call `addFolder` in our generator:

```ts
import path from 'path'
import { builder } from '@compgen/core'

export const createPrettierSchema = () => {
  const schema = builder('prettier')

  // ...

  schema.addFolder({
    name: 'prettier',
    source: path.join(__dirname, 'templates'),
  })
}
```

### 4) Add dependencies

Prettier will not work unless we install npm dependencies. That is where functions

- `addDependencies`
- `addDevDependencies`

are going to help us.

```ts
import { builder } from '@compgen/core'

export const createPrettierSchema = () => {
  const schema = builder('prettier')

  // ...

  schema.addDevDependencies(['prettier', '@linters/prettier-config'])
}
```

### 5) Add scripts to package.json

Our developers shouldn't have to figure out themselves that they can use prettier in our project. We can add `format` script to package.json.

```ts
import { builder } from '@compgen/core'

export const createPrettierSchema = () => {
  const schema = builder('prettier')

  // ...

  schema.addScript('format', "prettier --write '*/**/*.{ts,tsx,css,md,json}'")
}
```

### 7) Creating CLI application

That is pretty much it. All we have to do us return JSON schema:

```ts
import { builder } from '@compgen/core'

export const createPrettierConfig = () => {
  const schema = builder('prettier')

  // ...

  return schema.toJson()
}
```

now we can create another function that will use use `execute` function to acutally run the schema:

```ts
import { execute, builder } from '@compgen/core'

export const createPrettierConfig = () => {
  const schema = builder('prettier')

  // ...

  return schema.toJson()
}

const run = async () => {
  const prettierSchema = createPrettierConfig()

  await execute(prettierSchema, '.')
}

run()
```

Easy, isn't it? 🚀 Now you don't have to configure prettier yourselves ever again.

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