コンテンツにスキップ

時刻ベースのセマンティックバージョンを生成する

インクリメンタルなセマンティックバージョニングではなく、ビルド・デプロイ時刻をリリースバージョンとするような運用をしたい時に使える関数を作りたかった

  • メジャー、マイナー、パッチのみをサポートし、プレリリースやビルドナンバーは考慮しない
  • Chrome拡張機能などではバージョンの最大値が65535なので、それぞれ最大値を超えないようにする
  • major: 現在の年(YYYY)
  • minor: 月日(MMDD)
  • patch: 時間 + 分 + 秒の下一桁(hhmms)
time-based-semantic-version.ts
type SemanticVersion = `${string}.${string}.${string}`;
function timeBasedSemanticVersion(): SemanticVersion {
const now = new Date();
const major = now.getFullYear();
// 上二桁を月、下二桁を日として扱う
const minor = `${(now.getMonth() + 1) * 100 + now.getDate()}`;
const hh = `${now.getHours()}`;
const mm = `${now.getMinutes()}`.padStart(2, "0");
const s = `${now.getSeconds()}`.slice(-1);
const patch = `${hh}${mm}${s}`;
return `${major}.${minor}.${patch}`;
}