LoginSignup
3
0

More than 3 years have passed since last update.

もしも npm script の引数にブランチ名をいれてみたくなったら

Last updated at Posted at 2019-11-06

git のローカルブランチをとるのは意外にめんどくさい。

考えに考えられている git なら簡単にとれそうなものなんですが、素の git から取り出すのは結構めんどくさいようです。

正直もっと簡単なコマンド一発で取れるかなと思っていたのですが。
もっと簡潔な方法あったら教えてください。

追記:教えてもらいました。

git rev-parse --abbrev-ref @ 

きっと誰かが作ってくれているはず

npm 検索すると出てきました。世界は支え合ってます。

git-branch

npm の引数に組み込むには?

npm script に色々書いても、Windows では実行できなかったりするので、node.js を作りました。

引数に callback に入ってくるブランチ名を渡しています。

/* eslint-disable */
const branch = require('git-branch');
const { exec } = require('child_process');

branch('./')
  .then((br) => {
    exec(`npx vue-cli-service storybook:build -c config/storybook -o test-sb-${br}`,function (error, stdout, stderr) {
      if(stdout){
          console.log('stdout: ' + stdout);
      }
      if(stderr){
          console.error('stderr: ' + stderr);
      }
      if (error !== null) {
        console.log('Exec error: ' + error);
      }
    })
  })
  .catch(console.error);

ここでは、storybook の出力にブランチ名を入れてみました。

npm scripts として組み込む

  "scripts": {
    "sb:td": "node ./sb-now-deploy.js"
  },

こんな感じで書いてあげると npm スクリプトのように実行できます。
shell も使ってないので、Windows 環境でも使えると思われます。

以上でーす👋👋👋

3
0
2

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