From 78d21bfd1324b8b983c768c81d4c5e050918f689 Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Mon, 13 May 2019 12:35:59 +0200 Subject: [PATCH] feat: add check to prevent publishing without annotated tag I extended the scripts/publish/check.js script to make sure we don't push a release without having an annotated tag for it. Seeing as the script now does more than just check the dist files, I renamed the corresponding package.json script from "check:dist" to "check:version". Closes: https://github.com/liferay/alloy-editor/issues/1263 --- package.json | 4 ++-- scripts/publish/check.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1fe6b3b43c..d1333f19dd 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "scripts": { "build:assets": "gulp --gulpfile scripts/build/gulp/gulpfile.js build:assets", "build": "gulp --gulpfile scripts/build/gulp/gulpfile.js --release", - "check:dist": "node scripts/publish/check.js", + "check:version": "node scripts/publish/check.js", "ci": "yarn build && yarn test:saucelabs", "dev": "gulp --gulpfile scripts/build/gulp/gulpfile.js", "format": "prettier --write -- '*.{js,json}' '{scripts,src,test}/**/*.{js,jsx}'", @@ -67,7 +67,7 @@ "lint:changed": "git ls-files -mz 'src/*.js' 'src/*.jsx' | xargs -0 eslint", "lint:fix": "eslint --fix 'src/**/*.{js,jsx}'", "lint:quiet": "eslint --quiet 'src/**/*.{js,jsx}'", - "prepublishOnly": "yarn check:dist && yarn format:check && yarn test", + "prepublishOnly": "yarn check:version && yarn format:check && yarn test", "start": "webpack-dev-server --config webpack.dev.js", "test": "karma start karma.js", "test:debug": "karma start karma.js --debug", diff --git a/scripts/publish/check.js b/scripts/publish/check.js index 265fd8cdc6..f798109d6c 100644 --- a/scripts/publish/check.js +++ b/scripts/publish/check.js @@ -1,7 +1,11 @@ /** - * Verifies that the built "dist" file has the correct version number. + * Pre-publish check script that verifies that: + * + * - The built "dist" file has the correct version number. + * - An annotated (not lightweight) tag exists pointing at the current commit. */ +const child_process = require('child_process'); const fs = require('fs'); const path = require('path'); @@ -36,7 +40,7 @@ function log(...args) { console.log(...args); } -function check() { +function checkDistVersionNumber() { if (!fs.existsSync(FILE)) { throw new Error(`File ${FILE} does not exist`); } @@ -76,4 +80,30 @@ function check() { throw new Error(`Failed to find version string in ${FILE}`); } -check(); +function checkAnnotatedTag() { + const {version} = require('../../package.json'); + + const {error, signal, status, stdout} = child_process.spawnSync('git', [ + 'describe', + '--exact-match', + ]); + + if (error || signal || status) { + const command = `git tag -m v${version} v${version}`; + throw new Error( + `Expected current HEAD to have an annotated tag; try running \`${command}\`` + ); + } + + const tag = stdout.toString().trim(); + if (tag !== version) { + throw new Error( + `Expected current HEAD to be tagged as v${version} but it was ${tag}` + ); + } + + log(`✅ Found annotated version tag v${version}`); +} + +checkDistVersionNumber(); +checkAnnotatedTag();