Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8829

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

$
0
0

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

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

https://qiita.com/sugyan/items/83e060e895fa8ef2038c

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

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

git rev-parse --abbrev-ref @ 

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

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

git-branch

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

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

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

/* eslint-disable */constbranch=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 環境でも使えると思われます。

以上でーす👋👋👋


Viewing all articles
Browse latest Browse all 8829

Trending Articles