びぼーろくっ!

誰かに見せるわけでもないけど、備忘録として。。

Node.jsでコマンド一発で全プロジェクトを最新にするツールをつくった。

ちょっと担当しているプロジェクトが増えすぎたので作成しました。
いちいちgitコマンドだったりgit Extensionでpullやcloneするのが面倒だったので・・・

やっぱツール作りは楽しいなー。

使い方。
・ProjectList.js -- 定義ファイル
・run.js -- 実行ファイル
・./shell 実行させるshellを格納

とりあえず現時点ではcloneとpullができればいいかなと思ってたので拡張はしてません。
でも拡張しやすいように作りました。

//gitCmd.sh

cd $2 && git $1;


// ProjectList.js

const projectList = [
  {
    dir: 'Hoge', // 格納したいディレクトリ
    gitRepository: 'RepositoryName', // git上のリポジトリ名
    category: 'parentDirectory', // gitRepositoryの親階層
  },
  {
    ...
  },
]
module.exports = projectList;


//run.js

const { exec } = require('child_process');

const gitUrl = 'https://exsample.com/gitbucket/git';
const projectListObjects = require('./projectList');
const shellDir = './shell';

const execute = (cmd) => {
  exec(cmd, {encoding: 'utf8'}, (err, stdout, stderr) => {
    console.log(stdout);
    if (stderr) console.error(stderr);
  });
}

const gitRun = (dir, cmd) => {
  execute(`sh ${shellDir}/gitCmd.sh "${cmd}" ${dir}`);
}

const getDir = pj => `~/Code/${pj.dir}`;

const createCloneCmd = (pj) => {
  const baseUrl = pj.category != null ? `${gitUrl}/${pj.category}` : `${gitUrl}`;
  const url = `${baseUrl}/${pj.gitRepository}.git`;

  const branch = pj.branch || 'develop';
  return `clone -b ${branch} ${url}`;
}

const clone = (pj) => {
  const dir = getDir(pj);
  const cloneCmd = createCloneCmd(pj);

  gitRun(dir, cloneCmd);
  gitRun(dir, 'pull');
}

const run = () => {
  projectListObjects.forEach((project) => {
    clone(project);
  });
}
run();