You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plop.js AddMany Action生成不必要文件夹的问题求助

解决Plop.js addMany生成多余模板文件夹的问题

当使用addMany动作时,模板文件所在的子目录结构会被默认完整复制到目标路径中,导致生成不必要的文件夹。你可以通过以下几种方式解决:

方法1:指定模板根目录(保留需要的子结构)

通过root参数指定模板文件的根目录,Plop会只复制该根目录下的内容(包含子目录),不会把根目录本身带到目标路径里。

修改后的配置:

plop.setGenerator(generators.serverless, {
    prompts: {
      type: 'input',
      name: 'name',
      message: 'Choose test file name',
    },
    actions: (data) => [
      {
        type: 'addMany',
        destination: 'src/functions/{{name}}',
        templateFiles: `serverless/templates/${serverlessTempates.endpoint}/**/*`,
        root: `serverless/templates/${serverlessTempates.endpoint}` // 指定模板根目录
      }
    ]
  });

方法2:扁平化所有文件(不保留子结构)

如果不需要保留模板里的子目录结构,只想把所有文件直接放到目标根目录,添加flatten: true参数即可:

plop.setGenerator(generators.serverless, {
    prompts: {
      type: 'input',
      name: 'name',
      message: 'Choose test file name',
    },
    actions: (data) => [
      {
        type: 'addMany',
        destination: 'src/functions/{{name}}',
        templateFiles: `serverless/templates/${serverlessTempates.endpoint}/**/*`,
        flatten: true // 扁平化所有文件到目标根目录
      }
    ]
  });

方法3:调整glob匹配模式(精确控制文件范围)

如果模板目录下只有一级文件、没有子目录,可以把**/*改成*,避免匹配到不必要的目录:

templateFiles: `serverless/templates/${serverlessTempates.endpoint}/*`,

根据你的实际需求选择对应方法,就能避免生成多余的模板文件夹结构。

内容的提问来源于stack exchange,提问作者TheSmartMonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 02:37:12