touch
コマンドは基本的には、新しいファイルの作成に使われている。
しかし、touch
は空のファイルを作成するのであまり便利ではない。
例えば、license
ファイルをプロジェクトに含め忘れたので後から追加したいとする。
そのとき叩くのは、touch license
もしくは、既存のプロジェクトからlicense
を探してcp
をする。
ここで思い至るのは、licenseファイルの内容はほぼ変わることがない。だいたい以下のような感じである。
The MIT License (MIT)
Copyright (c) akameco <akameco.t@gmail.com> (akameco.github.io)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
(略)
となると、これをテンプレートとしてtouchの代わりにこれを新しいファイルとして作成した方が良さそうだ。
そこで、touch
の代わりにtouch-alt
を使う。
$ npm i -g touch-alt
akameco/touch-alt: Create from a template instead of a new file
使い方はtouchと同じだが、touchの本来の機能であるタイムスタンプ更新の機能はない。
自分はほぼ使わないのでalias touch=touch-alt
を.zshrc
に追記した。
使い方はtouch
と同じだ。
だが、--add
もしくは-a
フラグで新しいテンプレートを追加できる。
追加されたファイルは~/.touch-alt
以下に保存される。
$ touch-alt --add license
後は、touch
の代わりにtouch-alt
でlicense
を作成するだけだ。
$ touch license
$ cat license
$ rm license
$ ls -la ~/.touch-alt
. .. license
$ touch-alt license
$ cat license
The MIT License (MIT)
Copyright (c) akameco <akameco.t@gmail.com> (akameco.github.io)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
(略)
もちろん、テンプレートがないファイルは空ファイルを作成する。
まとめ
自分はわりとyeomenでテンプレートを作成することが多いが、既存のプロジェクトにわりといつも使う設定を持ってきたいときに便利であると思う。
例えば、.babelr
cや.eslinrc
、.editorconfig
などがそれだ。
これらをtouch -a .eslintrc
としておくことで、touch .eslintrc
をするだけでプロジェクトに追加できる。
akameco/touch-alt: Create from a template instead of a new file
実際のところ、すでにテンプレートが保存してあればそれをコピーしてくるだけのスクリプトだが、この手のスクリプトはどうやってもtestの方が行数が増えるのが面倒なところだった。
$ touch-alt --help
Create from a template instead of a new file
Usage
$ touch-alt <source>
$ touch-alt --add <source>
Options
-a, --add Create new template file
-o, --overwrite Overwrite by template
Example
$ touch-alt .editorconfig