Skip to content

Commit

Permalink
Add plop-dir support to override or extend inferred prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Sep 18, 2022
1 parent 11a3fc8 commit 0ca985c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-boxes-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'plop-dir': patch
---

Add support to override or extends inferred prompts
2 changes: 1 addition & 1 deletion packages/plop-dir/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ module.exports = {
tsconfigRootDir: __dirname,
project: 'tsconfig.eslint.json',
},
ignorePatterns: ['node_modules', 'dist'],
ignorePatterns: ['node_modules', 'dist', 'templates'],
}
7 changes: 7 additions & 0 deletions packages/plop-dir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export default async function run(plop) {
templateDir: path.join(__dirname, './templates/my-generator'),
// Path to output my-generator files
outputDir: path.join(__dirname, './src'),
// Override or extend the inferred prompts
prompts: [
{
name: 'generatorName',
message: "What's the name of the generator?",
},
],
}),
)
}
Expand Down
7 changes: 7 additions & 0 deletions packages/plop-dir/plopfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export default async function run(plop) {
templateDir: path.join(__dirname, './templates/my-generator'),
// Path to output my-generator files
outputDir: path.join(__dirname, './tmp'),
// Override or extend the inferred prompts
prompts: [
{
name: 'generatorName',
message: "What's the name of the generator?",
},
],
}),
)
}
19 changes: 14 additions & 5 deletions packages/plop-dir/src/plop-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import * as fs from 'node:fs'
// @ts-expect-error
import type { NodePlopAPI, PlopGeneratorConfig } from 'plop'

interface PlopDirOptions {
type PlopPromptQuestion = Extract<PlopGeneratorConfig['prompts'], any[]>[number]

export const plopDirActionType = 'plop-dir'

export interface PlopDirOptions {
plop: NodePlopAPI
templateDir: string
outputDir: string
description?: string
// TODO: Add support for overriding inferred prompts
prompts?: Partial<PlopPromptQuestion>[]
}

export const plopDirActionType = 'plop-dir'

export async function plopDir(
options: PlopDirOptions,
): Promise<PlopGeneratorConfig> {
const { plop, templateDir, outputDir, description = '' } = options
const {
plop,
templateDir,
outputDir,
description = '',
prompts = [],
} = options

const templateFiles = await getTemplateFiles(templateDir)

Expand Down Expand Up @@ -69,6 +77,7 @@ export async function plopDir(
type: 'input',
name: promptName,
message: `Enter ${promptName}`,
...prompts.find((prompt) => prompt.name === promptName),
})),
actions: [{ type: plopDirActionType }],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function {{camelCase generatorName}}() {
return '{{pascalCase generatorName}}';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function {{camelCase generatorName}}() {
return '{{pascalCase generatorName}}';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function {{camelCase generatorName}}() {
return '{{pascalCase generatorName}}';
}

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion packages/plop-dir/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"outDir": "dist",
"declarationDir": "dist/types"
},
"include": ["src", "scripts"],
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

0 comments on commit 0ca985c

Please sign in to comment.