対抗馬としてはplop や scaffdog というジェネレーターもあります
テンプレートが ejs 形式です。ejsが苦手な場合はおすすめ致しません。
npm install --save-dev hygen
npx hygen init self
hygen <好きな名前> new
と言うコマンドを作成することができます。
npx hygen generator new helloworld
_templates/helloworld/new/hello.ejs.t が生成されます。
npx hygen helloworld new
app/hello.js が自動生成されます。
npx hygen generator with-prompt helloworld
_templates/helloworld/with-prompt/hello.ejs.t
_templates/helloworld/with-prompt/prompt.js
が生成されます。
with-prompt を 短い名前に変更しておくと、実行時に入力が楽になります。
_templates/helloworld/prompt/hello.ejs.t
---
to: app/hello.js
---
const hello = "こんにちは <%= userName %>."
console.log(hello)
_templates/helloworld/prompt/prompt.js
// see types of prompts:
// https://github.com/enquirer/enquirer/tree/master/examples
//
module.exports = [
{
type: 'input',
name: 'userName',
message: "What's your name?"
}
]
npx hygen helloworld prompt
app/hello.js が以下の内容で出力されます
const hello = "こんにちは あいうえお."
console.log(hello)
<%
myParams = ['param001', 'param002'];
%>
<% ary.forEach(function (v, k) { %>
<p><%= k %>: <%= v %></p>
<% }); %>
コマンドの先頭に HYGEN_OVERWRITE=1 を追加します。
HYGEN_OVERWRITE=1 npx hygen generator new --name foobar
変数 name を変換します
<%= name.toLowerCase() %>
<%= h.inflection.camelize(name) %>
<%= h.inflection.camelize(name, true) %>
h.changeCase.snakeCase(name)
https://www.hygen.io/docs/templates/
to: が null の場合は出力されないのでこれを利用します。
---
to: "<%= redirect ? `/router/routes/${folder.toLowerCase().replace(/ +/g, '')}/routes.redirect.js` : null %>"
unless_exists: true
---
次のように 他のファイルより、先に読み込ませるためアンダースコア始まりのファイル名を つけたファイルに変数を指定ます。
また to: null としてファイルとしては出力しないようにしておくと、変数だけを格納するテンプレートファイルができます。
_const.ejs.t
---
to: null
---
<%
// 出力するディレクトリ名入れてください
outputDir = '05sendFileMessage'
%>