Skip to content

Commit

Permalink
Allow templates in the plop-dir outputDir config
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Sep 24, 2022
1 parent 4a531a3 commit c428e46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-pigs-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'plop-dir': minor
---

Allow templates in the outputDir config
2 changes: 1 addition & 1 deletion packages/plop-dir/plopfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function run(plop) {
// Path to my-template templates
templateDir: path.join(__dirname, './templates/my-template'),
// Path to output my-template files
outputDir: path.join(__dirname, './tmp'),
outputDir: path.join(__dirname, './{{kebabCase outputDir}}'),
// Override or extend the inferred prompts
prompts: [
{
Expand Down
25 changes: 17 additions & 8 deletions packages/plop-dir/src/plop-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export async function plopDir(
(await Promise.all(templateFiles.map(getTemplateFilePromptNames))).flat(),
)

const outputDirPromptNames = getPromptNames(outputDir)

plop.setActionType(plopDirActionType, handlePlopDirActionType)

const plopDirActionData: PlopDirActionData = {
Expand All @@ -53,12 +55,14 @@ export async function plopDir(

return {
description,
prompts: templateFilesPromptNames.map((promptName) => ({
type: 'input',
name: promptName,
message: `Enter ${promptName}`,
...prompts.find((prompt) => prompt.name === promptName),
})),
prompts: [...templateFilesPromptNames, ...outputDirPromptNames].map(
(promptName) => ({
type: 'input',
name: promptName,
message: `Enter ${promptName}`,
...prompts.find((prompt) => prompt.name === promptName),
}),
),
actions: [
{
type: plopDirActionType,
Expand All @@ -76,6 +80,8 @@ async function handlePlopDirActionType(...args: CustomActionFunctionArgs) {
const { outputDir, templateDir, templateFiles } =
config.data as PlopDirActionData

const renderedOutputDir = plop.renderString(outputDir, answers)

await Promise.all(
templateFiles.map(async (templateFile) => {
const templateFileContent = await fs.promises.readFile(
Expand All @@ -92,7 +98,10 @@ async function handlePlopDirActionType(...args: CustomActionFunctionArgs) {
.replace(`${templateDir}${path.sep}`, '')
.replace('.hbs', '')

const outputTemplateFilePath = path.resolve(outputDir, templateFilePath)
const outputTemplateFilePath = path.resolve(
renderedOutputDir,
templateFilePath,
)

const renderedTemplateFileOutputPath = plop.renderString(
outputTemplateFilePath,
Expand All @@ -110,7 +119,7 @@ async function handlePlopDirActionType(...args: CustomActionFunctionArgs) {
}),
)

return `successfully wrote files to ${outputDir}`
return `successfully wrote files to ${renderedOutputDir}`
}

const handlebarsTemplateRegExp = /{{(.+?)}}/g
Expand Down

0 comments on commit c428e46

Please sign in to comment.