LoginSignup
0
0

More than 5 years have passed since last update.

Yeomanのカスタムジェネレータで特定フォルダ配下を一括コピー

Posted at

はじめに

Yeomanのカスタムジェネレータでは、テンプレートファイルのコピーを絶対やりますよね。
http://yeoman.io/authoring/file-system.html
この公式ドキュメントにはcopyTpl()関数だけ載ってますけど、ファイルをそのまんまコピー、しかも特定フォルダ配下をそのまんま一括コピーしたいときは、もっと簡単な方法があります。

this.fs.copy()関数を使う方法

シンプルなコピー

generators/app/templates/staticってフォルダを作っておいて、そのまんまコピーしたいテンプレートファイルはその中に全部つっこんで以下のように書くと、static配下の全ファイル・フォルダを、ジェネレータで作成するプロジェクトのルート配下に綺麗にコピーしてくれます。

第二引数のthis.destinationRoot()がポイントですね。

generators/app/index.js
  writing() {
    // Copy all static files
    this.fs.copy(this.templatePath('static'), this.destinationRoot())
  }

ただこれだと、ドットで始まる隠しファイル・ディレクトリは無視されちゃうのです。

ドットファイルも含める

ドットファイルも含めてコピーしたいときは、copy()の第三引数にオプションをつければOK。

generators/app/index.js
  writing() {
    // Copy all static files
    this.fs.copy(this.templatePath('static'), this.destinationRoot(), {
      globOptions: { dot: true },
    })
  }

解説

Yeomanのthis.fs配下のメソッドはmem-fs-editorというライブラリが実体のようです。

copyメソッドの第三引数にoptionsオブジェクトを任意で渡せて、3種類のキーを指定可能。

  • process
    • よく分からんかったのでスルー
  • ignoreNoMatch
    • trueを指定すると、第一引数のfromファイルが見つからなくてもスルーして処理を続ける
  • globOptions
    • ファイルのマッチングパターンをオブジェクトで細かく指定する
    • fast-globライブラリのOptionsを指定
    • dot: falseがデフォルトなので、trueにするとドットファイル有効

参考ドキュメント

http://yeoman.io/authoring/file-system.html
https://github.com/sboudrias/mem-fs-editor
https://github.com/mrmlnc/fast-glob
https://github.com/yeoman/generator/issues/642

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0