plop-dir
v0.2.0
Published
Directory based file generator
Downloads
988
Readme
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 plopfile
s 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 asynchronousplopfile
s
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