0.2.0 ā€¢ Published 4 months ago

plop-dir v0.2.0

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

plop-dir

Directory based file generator built on plop. Simply point to a directory and plop-dir will:

  • Parse each file path and content for handlebars templates
  • Extract prompt names from each template and generate a plop prompt config
  • Upon answering the prompts, leverage the plop API to render each template

Project goals:

Spend less time configuring plopfiles and more time writing templates

Installation

npm i -D plop plop-dir

Usage

Example templates directory:

plopfile.mjs
src/
templates/
|__ my-template/
   |-- {{kebabCase templateName}}.js
   |__ tests/
      |__ {{kebabCase templateName}}.test.js

plopfile.mjs

plop-dir fits into your existing plop workflow

import * as url from 'node:url'
import * as path from 'node:path'

import { plopDir } from 'plop-dir'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

/** @param {import('plop').NodePlopAPI} plop */
export default async function run(plop) {
  plop.setGenerator(
    'my-template',
    await plopDir({
      plop,
      // Path to my-template templates
      templateDir: path.join(__dirname, './templates/my-template'),
      // Path to output my-template files
      outputDir: path.join(__dirname, './src'),
      // Override or extend the inferred prompts
      prompts: [
        {
          name: 'templateName',
          message: "What's the name of your template?",
        },
      ],
    }),
  )
}

Note: Currently, plopDir is only supported in asynchronous plopfiles

Example template file: my-template/tests/{{kebabCase templateName}}.js

export default function {{camelCase testName}}() {
  return '{{pascalCase templateName}}'
}

testName is extracted from the file content (e.g. {{camelCase testName}}) and included as a plop prompt

See the plop docs for all available case modifiers

Run the plop CLI:

$ npm run plop my-template
Whats the name of your template? <answer>
Enter testName <answer>
āœ”  plop-dir successfully wrote files to <outputDir>

That's it!

Aside from configuring the templateDir and outputDir, you choose whether or not to add custom prompt messages or opt for a "zero-config" workflow where plop-dir generates all prompts from the template file paths and content

0.2.0

4 months ago

0.1.0

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago