6.0.0-alpha.5 • Published 2 months ago

@guanghechen/helper-plop v6.0.0-alpha.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

A collection of utility functions for plop templates.

Install

  • npm

    npm install --save-dev @guanghechen/helper-plop
  • yarn

    yarn add --dev @guanghechen/helper-plop

Usage

This package exposed some inquirer question and answer resolver.

Npm package

  • NpmPackagePreAnswers:

    NameTypeDescription
    cwdstringCurrent workspace directory
    isMonorepobooleanWhether if this package under a monorepo
  • NpmPackagePromptsAnswers:

    NameTypeDescription
    packageNamestringNpm package name
    packageAuthorstringPackage author
    packageVersionstringPackage version
    packageDescriptionstringPackage description
    packageLocationstringPackage location (path relative to the current directory)
  • NpmPackageData:

    Extended NpmPackagePreAnswers and NpmPackagePromptsAnswers With following Additional properties.

    NameTypeDescription
    packageUsagestringPackage usage
    repositoryNamestringGit repository name
    repositoryHomepagestringGit 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:

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

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

  plop.setGenerator('ts-package', {
    description: 'create template typescript project',
    prompts: [...prompts],
    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:

#! /usr/bin/env node

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

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

Related

6.0.0-alpha.5

2 months ago

6.0.0-alpha.4

3 months ago

6.0.0-alpha.3

4 months ago

6.0.0-alpha.2

5 months ago

6.0.0-alpha.1

5 months ago

6.0.0-alpha.0

6 months ago

5.0.9

7 months ago

5.0.8

8 months ago

5.0.7

9 months ago

5.0.6

9 months ago

5.0.5

9 months ago

5.0.4

9 months ago

5.0.3

10 months ago

5.0.2

10 months ago

5.0.10

7 months ago

5.0.11

6 months ago

5.0.0-alpha.3

1 year ago

5.0.0-alpha.2

1 year ago

5.0.1

12 months ago

5.0.0

12 months ago

4.7.2

1 year ago

4.7.1

1 year ago

4.7.4

1 year ago

4.7.3

1 year ago

5.0.0-alpha.1

1 year ago

5.0.0-alpha.0

1 year ago

4.0.0-alpha.7

1 year ago

4.0.0-alpha.8

1 year ago

4.0.0-alpha.5

1 year ago

4.0.0-alpha.6

1 year ago

4.0.0-alpha.3

1 year ago

4.0.0-alpha.4

1 year ago

4.0.0-alpha.1

1 year ago

4.0.0-alpha.2

1 year ago

4.0.0-alpha.0

1 year ago

4.4.1

1 year ago

4.4.0

1 year ago

4.2.2

1 year ago

4.6.1

1 year ago

4.4.3

1 year ago

4.6.0

1 year ago

4.4.2

1 year ago

4.0.0

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.6.3

1 year ago

4.6.2

1 year ago

4.6.5

1 year ago

4.6.4

1 year ago

4.5.0

1 year ago

4.1.3

1 year ago

4.7.0

1 year ago

4.1.0

1 year ago

4.3.0

1 year ago

4.1.2

1 year ago

4.1.1

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

3.0.0-alpha.1

1 year ago

3.0.0-alpha.0

1 year ago

3.0.0-alpha.3

1 year ago

3.0.0-alpha.2

1 year ago

3.0.0-alpha.5

1 year ago

3.0.0-alpha.4

1 year ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.0.0-alpha.3

2 years ago

2.0.0-alpha.2

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

2.0.0-alpha.1

2 years ago

2.0.0-alpha.0

2 years ago