# @guanghechen/plop-helper

> A collection of utility functions for plop templates

Latest version **1.9.8** (published 2022-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @guanghechen/plop-helper
pnpm add @guanghechen/plop-helper
yarn add @guanghechen/plop-helper
bun add @guanghechen/plop-helper
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.9.8 |
| Published | 2022-07-27 |
| First published | 2021-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 14.15.0 |
| Dependencies | 10 |
| Unpacked size | 28.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | guanghechen |
| Maintainers | lemonclown |
| Keywords | plop |

## Links

- npm: https://www.npmjs.com/package/@guanghechen/plop-helper
- Repository: https://github.com/guanghechen/node-scaffolds
- Homepage: https://github.com/guanghechen/node-scaffolds/tree/main/packages/plop-helper#readme
- Issues: https://github.com/guanghechen/node-scaffolds/issues
- npm.io page: https://npm.io/package/@guanghechen/plop-helper

## Dependencies (10)

- [ora](https://npm.io/package/ora.md) ^5.4.1
- [plop](https://npm.io/package/plop.md) ^2.7.6
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [fs-extra](https://npm.io/package/fs-extra.md) ^10.1.0
- [minimist](https://npm.io/package/minimist.md) ^1.2.6
- [node-plop](https://npm.io/package/node-plop.md) ^0.26.3
- [mock-stdin](https://npm.io/package/mock-stdin.md) ^1.0.0
- [@guanghechen/invariant](https://npm.io/package/@guanghechen/invariant.md) ^1.9.8
- [@guanghechen/npm-helper](https://npm.io/package/@guanghechen/npm-helper.md) ^1.9.8
- [@guanghechen/option-helper](https://npm.io/package/@guanghechen/option-helper.md) ^1.9.8

## Recent versions

- 1.9.8 (latest) — 2022-07-27
- 1.9.7 — 2022-07-17
- 1.9.6 — 2022-07-10
- 1.9.5 — 2022-06-03
- 1.9.4 — 2022-04-27
- 1.9.3 — 2022-04-10
- 1.9.2 — 2022-03-12
- 1.9.1 — 2022-02-23
- 1.9.0 — 2022-02-17
- 1.9.0-alpha.0 — 2022-02-16
- 1.8.6 — 2022-01-15
- 1.8.5 — 2021-12-05
- 1.8.4 — 2021-10-28
- 1.8.3 — 2021-08-18
- 1.8.2 — 2021-08-14
- … 26 more at https://npm.io/package/@guanghechen/plop-helper/versions

## README

<header>
  <h1 align="center">
    <a href="https://github.com/guanghechen/node-scaffolds/tree/main/packages/plop-helper#readme">@guanghechen/plop-helper</a>
  </h1>
  <div align="center">
    <a href="https://www.npmjs.com/package/@guanghechen/plop-helper">
      <img
        alt="Npm Version"
        src="https://img.shields.io/npm/v/@guanghechen/plop-helper.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/plop-helper">
      <img
        alt="Npm Download"
        src="https://img.shields.io/npm/dm/@guanghechen/plop-helper.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/plop-helper">
      <img
        alt="Npm License"
        src="https://img.shields.io/npm/l/@guanghechen/plop-helper.svg"
      />
    </a>
    <a href="https://github.com/nodejs/node">
      <img
        alt="Node.js Version"
        src="https://img.shields.io/node/v/@guanghechen/plop-helper"
      />
    </a>
    <a href="https://github.com/prettier/prettier">
      <img
        alt="Code Style: prettier"
        src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
      />
    </a>
  </div>
</header>
<br/>


A collection of utility functions for plop templates.

## Install

* npm

  ```bash
  npm install --save-dev @guanghechen/plop-helper
  ```

* yarn

  ```bash
  yarn add --dev @guanghechen/plop-helper
  ```

## Usage

This package exposed some [inquirer][] question and answer resolver.

### Npm package

* `NpmPackagePreAnswers`:

  Name                  | Type      | Description
  :--------------------:|:---------:|:-----------------:
  `cwd`                 | `string`  | Current workspace directory
  `isMonorepo`          | `boolean` | Whether if this package under a monorepo

* `NpmPackagePromptsAnswers`:

  Name                  | Type      | Description
  :--------------------:|:---------:|:-----------------:
  `packageName`         | `string`  | Npm package name
  `packageAuthor`       | `string`  | Package author
  `packageVersion`      | `string`  | Package version
  `packageDescription`  | `string`  | Package description
  `packageLocation`     | `string`  | Package location (path relative to the current directory)

* `NpmPackageData`:

  Extended `NpmPackagePreAnswers` and `NpmPackagePromptsAnswers` With following
  Additional properties.

  Name                  | Type      | Description
  :--------------------:|:---------:|:-----------------:
  `packageUsage`        | `string`  | Package usage
  `repositoryName`      | `string`  | Git repository name
  `repositoryHomepage`  | `string`  | Git repository homepage

### Example

I recommend you use the following template directory structure:

```
├── boilerplate/
│   ├── package.json.hbs
│   ├── README.md.hbs
│   ├── rollup.config.js.hbs
│   ├── src
│   │   └── index.ts.hbs
│   ├── tsconfig.json.hbs
│   ├── tsconfig.settings.json.hbs
│   └── tsconfig.src.json.hbs
├── node_modules/
├── cli.js
├── index.js
├── package.json
└── README.md
```

Where the `index.js` exposed a default plop config, such as:

  ```javascript
  const {
    createNpmPackagePrompts,
    resolveNpmPackageAnswers,
    resolveNpmPackagePreAnswers,
  } = require('@guanghechen/plop-helper')
  const path = require('path')
  const manifest = require('./package.json')

  module.exports = function (plop) {
    const preAnswers = resolveNpmPackagePreAnswers()
    const defaultAnswers = { packageVersion: manifest.version }
    const { cwd, isMonorepo } = preAnswers

    plop.setGenerator('ts-package', {
      description: 'create template typescript project',
      prompts: [...createNpmPackagePrompts(preAnswers, defaultAnswers)],
      actions: function (_answers) {
        const answers = resolveNpmPackageAnswers(preAnswers, _answers)
        answers.toolPackageVersion = manifest.version

        const resolveSourcePath = p =>
          path.normalize(path.resolve(__dirname, 'boilerplate', p))
        const resolveTargetPath = p =>
          path.normalize(path.resolve(answers.packageLocation, p))
        const relativePath = path.relative(answers.packageLocation, cwd)

        // Assign resolved data into plop templates.
        Object.assign(_answers, answers)

        return [
          {
            type: 'add',
            path: resolveTargetPath('package.json'),
            templateFile: resolveSourcePath('package.json.hbs'),
          },
          {
            type: 'add',
            path: resolveTargetPath('README.md'),
            templateFile: resolveSourcePath('README.md.hbs'),
          },
          !isMonorepo && {
            type: 'add',
            path: resolveTargetPath('rollup.config.js'),
            templateFile: resolveSourcePath('rollup.config.js.hbs'),
          },
        ].filter(Boolean)
      },
    })
  }
  ```

And the `cli.js` exposed a Node.js CLI script, such as:

  ```javascript
  #! /usr/bin/env node

  const { launch } = require('@guanghechen/plop-helper')
  const path = require('path')

  launch(
    process.argv,
    args => ({
      configPath: args.plopfile || path.join(__dirname, 'index.js'),
    })
  )
  ```
## Related

* [@guanghechen/template-ts-package][]
* [@guanghechen/template-tsx-package][]
* [inquirer][]


[homepage]: https://github.com/guanghechen/node-scaffolds/tree/main/packages/plop-helper#readme
[inquirer]: https://github.com/SBoudrias/Inquirer.js/
[@guanghechen/template-ts-package]: https://github.com/guanghechen/node-scaffolds/tree/main/packages/template-ts-package#readme
[@guanghechen/template-tsx-package]: https://github.com/guanghechen/node-scaffolds/tree/main/packages/template-ts-package#readme

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