NodeJS 是否可以在PLOP中使用单个templateFile添加多个文件

bwitn5fc  于 2023-06-05  发布在  Node.js
关注(0)|答案(2)|浏览(182)

我使用plop依赖性来生成模块。我想生成的文件在一个去使用单一的模板n数量。

plop.setGenerator('module', {
  description: 'Create a module',
  // User input prompts provided as arguments to the template
  prompts: [
    {
      // Raw text input
      type: 'input',
      // Variable name for this input
      name: 'name',
      // Prompt to display on command line
      message: 'What is your module name?'
    }
  ],
  actions: [
    {
      // Add a new file
      type: 'addMany',
      // Path for the new file
      destination: 'src/Screens/{{pascalCase name}}',
      // Handlebars template used to generate content of new file
      templateFiles: 'plop-templates/**.js.hbs'
    }
  ]
})
lymnna71

lymnna711#

将templateFiles Package 在数组中。

templateFiles: ['plop-templates/**.js.hbs']
ijnw1ujt

ijnw1ujt2#

如果有人感兴趣的话,我可以告诉你我是怎么做到的

const page = {
  description: "⚛ react page",
  prompts: [
    {
      type: "input",
      name: "names",
      message: "page name",
    },
  ],
  actions: (data) => {
    const names = data.names.split(/\s+/).map((name) => name.trim());
    return names.flatMap((name) => {
      return [
        {
          type: "add",
          path: `../src/pages/{{pascalCase name}}/{{pascalCase name}}.tsx`,
          templateFile: "./templates/page.tsx.hbs",
          data: { name },
        },
        {
          type: "add",
          path: "../src/pages/{{pascalCase name}}/{{pascalCase name}}.test.tsx",
          templateFile: "./templates/test.tsx.hbs",
          data: { name },
        },
      ];
    });
  },
};

const generator = (plop) => {
  plop.setDefaultInclude({ generators: true });
  plop.setGenerator("page", page);
};

export default generator

语法:

$ plop page page1,page2,page3

✔  ++ src/pages/Page1/Page1.tsx
✔  ++ src/pages/Page1/Page1.test.tsx
✔  ++ src/pages/Page2/Page2.tsx
✔  ++ src/pages/Page2/Page2.test.tsx
✔  ++ src/pages/Page3/Page3.tsx
✔  ++ src/pages/Page3/Page3.test.ts

不幸的是,plop不能很好地与空格一起使用,所以你不能使用它。我选择了逗号

相关问题