LoginSignup
1
3

More than 5 years have passed since last update.

複数のpackage.jsonをワンライナーでnpm installする

Posted at

タイトルの通り。

こういったディレクトリ構成だったとして、

.
├── README.md
├── _meta
│   ├── resources
│   │   └── s-resources-cf-hoge-apnortheast1.json
│   └── variables
│       ├── s-variables-common.json
│       ├── s-variables-hoge-apnortheast1.json
│       └── s-variables-hoge.json
├── functions
│   ├── hoge
│   │   ├── event.json
│   │   ├── handler.js
│   │   ├── package.json
│   │   └── s-function.json
│   ├── s-templates.json
│   └── piyo
│        ├── event.json
│        ├── handler.js
│        └── s-function.json
├── package.json
├── s-project.json
├── s-resources-cf.json
└── s-templates.json

rootディレクトリのpackage.json と
functions/hoge/package.json をnpm installしたい。

npm install && cd functions/hoge && npm install

とかでもいいけれど、カレントディレクトリが変わるし、package.json が増えた場合に面倒くさいのでワンライナーで行う。

find . -type d \( -name "node_modules" \) -prune -o -type f -name "package.json" | grep -v "node_modules" | xargs -I{} bash -c 'path={}; npm install -prefix ${path%package.json}'

bashの変数に格納した後、${変数名%パターン} の形式で記述してやることで、
後方一致でマッチ部分を最短マッチで削除できる。

1
3
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
1
3