1.9.8 • Published 2 years ago

@guanghechen/plop-helper v1.9.8

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

A collection of utility functions for plop templates.

Install

  • npm

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

    yarn add --dev @guanghechen/plop-helper

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/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:

#! /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

1.9.8

2 years ago

1.9.7

2 years ago

1.9.6

2 years ago

1.9.5

2 years ago

1.9.0-alpha.0

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.6

2 years ago

1.9.4

2 years ago

1.9.3

2 years ago

1.9.2

2 years ago

1.8.5

2 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.7.0-alpha.3

3 years ago

1.7.0-alpha.1

3 years ago

1.7.0-alpha.2

3 years ago

1.7.0-alpha.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago