用yeoman复制多个dotfiles的推荐方法是什么?

Rai*_*ere 23 yeoman yeoman-generator

我正在为一个相当典型的节点应用程序构建一个yeoman生成器:

/
|--package.json
|--.gitignore
|--.travis.yml
|--README.md
|--app/
    |--index.js
    |--models
    |--views
    |--controllers
Run Code Online (Sandbox Code Playgroud)

在我的yeoman生成器的templates文件夹中,我必须重命名dotfiles(和package.json)以防止它们作为生成器的一部分进行处理:

templates/
 |--_package.json
 |--_gitignore
 |--_travis.yml
 |--README.md
 |--app/
     |--index.js
     |--models
     |--views
     |--controllers
Run Code Online (Sandbox Code Playgroud)

我看到很多生成器手动复制dotfiles:

this.copy('_package.json', 'package.json')
this.copy('_gitignore', '.gitignore')
this.copy('_gitattributes', '.gitattributes')
Run Code Online (Sandbox Code Playgroud)

我认为在添加新模板文件时手动更改生成器代码很麻烦.我想自动复制/ templates文件夹中的所有文件,并重命名前缀为_的文件.

最好的方法是什么?

如果我在想象的正则表达式中描述我的意图,那就是它的样子:

this.copy(/^_(.*)/, '.$1')
ths.copy(/^[^_]/)
Run Code Online (Sandbox Code Playgroud)

编辑 这是我能管理的最好的:

this.expandFiles('**', { cwd: this.sourceRoot() }).map(function() {
    this.copy file, file.replace(/^_/, '.')
}, this);
Run Code Online (Sandbox Code Playgroud)

cal*_*rae 31

我通过谷歌找到了这个问题,因为我正在寻找解决方案,然后我自己想出来了.

使用新fsAPI,您可以使用globs!

  // Copy all non-dotfiles
  this.fs.copy(
    this.templatePath('static/**/*'),
    this.destinationRoot()
  );

  // Copy all dotfiles
  this.fs.copy(
    this.templatePath('static/.*'),
    this.destinationRoot()
  );
Run Code Online (Sandbox Code Playgroud)


sth*_*hzg 22

添加到@callumacrae的回答是:你还可以定义dot: trueglobOptionscopy().这样一个/**glob将包括 dotfiles.例:

this.fs.copy(
  this.templatePath('files/**'),
  this.destinationPath('client'),
  { globOptions: { dot: true } }
);
Run Code Online (Sandbox Code Playgroud)

可以在README中找到可用的Glob选项列表node-glob.

  • Yeoman文档非常糟糕.你在哪里找到了"globOptions"的信息?我知道选项列表,但是没有关于基础对象名称"globOptions"的提示. (3认同)
  • @NicolasForney 对于 `copyTpl`,似乎 `{globOptions}` 需要在 **5th** 参数位置([例如。这里](/sf/answers/3225864421/)),作为[语法](https://github.com/SBoudrias/mem-fs-editor#copytplfrom-to-context-templateoptions--copyoptions) 说`#copyTpl(from, to, context[, templateOptions [, `**` copyOptions`** `]])`。 (2认同)

Dan*_*ick 9

刚刚为我工作:globOptions需要在第四个参数:

this.fs.copyTpl(
  this.templatePath('sometemplate/**/*'),
  this.destinationPath(this.destinationRoot()),
  null,
  null,
  { globOptions: { dot: true } }
);
Run Code Online (Sandbox Code Playgroud)