Browse Source

Convert sync-version-numbers to Node.js script

Renée Kooi 6 years ago
parent
commit
27da43a8c9
3 changed files with 87 additions and 44 deletions
  1. 84 0
      bin/after-version-bump.js
  2. 0 43
      bin/sync-version-numbers
  3. 3 1
      package.json

+ 84 - 0
bin/after-version-bump.js

@@ -0,0 +1,84 @@
+#!/usr/bin/env node
+// Called by the `version` npm script.
+// This is run _after_ lerna updates the version numbers,
+// but _before_ it commits, so we have time to update the
+// version numbers throughout the repo and add it to the
+// release commit.
+// NOTE this _amends_ the previous commit, which should
+// already be a "Release" commit generated by bin/release.
+
+const lastCommitMessage = require('last-commit-message')
+const { spawn } = require('child_process')
+const { promisify } = require('util')
+const { once } = require('events')
+const globby = require('globby')
+const fs = require('fs')
+const readFile = promisify(fs.readFile)
+const writeFile = promisify(fs.writeFile)
+
+async function replaceInFile (filename, replacements) {
+  let content = await readFile(filename, 'utf8')
+  for (const [rx, replacement] of replacements) {
+    content = content.replace(rx, replacement)
+  }
+
+  await writeFile(filename, content, 'utf8')
+}
+
+async function updateVersions (files, packageName) {
+  const { version } = require(`../packages/${packageName}/package.json`)
+
+  // uppy → uppy
+  // @uppy/robodog → uppy/robodog
+  const urlPart = packageName === 'uppy' ? packageName : packageName.slice(1)
+
+  const replacements = new Map([
+    [RegExp(`${urlPart}/v\\d+\\.\\d+\\.\\d+\\/`, 'g'), `${urlPart}/v${version}/`]
+    // maybe more later
+  ])
+
+  console.log('replacing', replacements, 'in', files.length, 'files')
+
+  for (const f of files) {
+    await replaceInFile(f, replacements)
+  }
+}
+
+async function main () {
+  if (process.env.ENDTOEND === '1') {
+    console.log('Publishing for e2e tests, skipping version number sync.')
+    process.exit(0)
+  }
+
+  const message = await lastCommitMessage()
+  if (message.trim() === 'Release') {
+    console.error(`Last commit is not a release commit, but '${message}'`)
+    process.exit(1)
+  }
+
+  const files = await globby([
+    'README.md',
+    'examples/**/*.html',
+    'packages/*/README.md',
+    'packages/@uppy/*/README.md',
+    'website/src/docs/**',
+    'website/src/examples/**',
+    'website/themes/uppy/layout/**',
+    '!node_modules'
+  ])
+
+  await updateVersions(files, 'uppy')
+  await updateVersions(files, '@uppy/robodog')
+  await updateVersions(files, '@uppy/locales')
+
+  const gitAdd = spawn('git', ['add', ...files], { stdio: 'inherit' })
+  const exitCode = await once(gitAdd, 'exit')
+  if (exitCode !== 0) {
+    throw new Error(`git add failed with ${exitCode}`)
+  }
+}
+
+main().catch(function (err) {
+  console.error(err.stack)
+  process.exit(1)
+})

+ 0 - 43
bin/sync-version-numbers

@@ -1,43 +0,0 @@
-#!/usr/bin/env bash
-
-# Called by the `version` npm script.
-# This is run _after_ lerna updates the version numbers,
-# but _before_ it commits, so we have time to update the
-# version numbers throughout the repo and add it to the
-# release commit.
-# NOTE this _amends_ the previous commit, which should
-# already be a "Release" commit generated by bin/release.
-
-set -o pipefail
-set -o errexit
-set -o nounset
-
-# Set magic variables for current file & dir
-# __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-# __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
-# __base="$(basename ${__file} .sh)"
-# __root="$(cd "$(dirname "${__dir}")" && pwd)"
-
-# disable this script
-# until upload-to-cdn is refactored to publish multiple packages
-# remove when upload-to-cdn is back
-exit 0
-
-if [ "${ENDTOEND:=0}" = "1" ]; then
-  echo "Publishing for e2e tests, skipping version number sync."
-  exit 0
-fi
-
-commit_message="$(git log -1 --pretty=%B)"
-if [ "${commit_message}" != "Release" ]; then
-  echo "Last commit is not a release commit, but '${commit_message}'"
-  exit 1
-fi
-
-version_files="./examples/ README.md bin/upload-to-cdn.sh website/src/examples/ website/src/docs/ website/themes/uppy/layout/"
-main_package_version=$(node -p "require('./packages/uppy/package.json').version")
-# Legacy defeater for also renaming the old /dist/ locations, can be removed as soon as everything's on the new dist-less thing
-replace-x -r 'uppy/v\d+\.\d+\.\d+/dist/' "uppy/v$main_package_version/" ${version_files} --exclude=node_modules
-replace-x -r 'uppy/v\d+\.\d+\.\d+/' "uppy/v$main_package_version/" ${version_files} --exclude=node_modules
-# replace-x -r 'uppy@\d+\.\d+\.\d+' "uppy@$main_package_version" ${version_files} --exclude=node_modules
-git add ${version_files} # add changes to the Release commit

+ 3 - 1
package.json

@@ -48,10 +48,12 @@
     "flat": "^4.1.0",
     "flat": "^4.1.0",
     "github-contributors-list": "1.2.3",
     "github-contributors-list": "1.2.3",
     "glob": "^7.1.3",
     "glob": "^7.1.3",
+    "globby": "^9.2.0",
     "gzip-size": "^5.0.0",
     "gzip-size": "^5.0.0",
     "isomorphic-fetch": "2.2.1",
     "isomorphic-fetch": "2.2.1",
     "jest": "^24.7.1",
     "jest": "^24.7.1",
     "json3": "^3.3.2",
     "json3": "^3.3.2",
+    "last-commit-message": "^1.0.0",
     "lerna": "^3.14.1",
     "lerna": "^3.14.1",
     "lint-staged": "^8.1.7",
     "lint-staged": "^8.1.7",
     "minify-stream": "^1.2.0",
     "minify-stream": "^1.2.0",
@@ -119,7 +121,7 @@
     "test:watch": "jest --watch",
     "test:watch": "jest --watch",
     "test": "npm-run-all lint test:locale-packs test:unit test:type test:companion",
     "test": "npm-run-all lint test:locale-packs test:unit test:type test:companion",
     "uploadcdn": "bin/upload-to-cdn.sh",
     "uploadcdn": "bin/upload-to-cdn.sh",
-    "version": "./bin/sync-version-numbers",
+    "version": "node ./bin/after-version-bump.js",
     "watch:css": "onchange 'packages/**/*.scss' --initial --verbose -- npm run build:css",
     "watch:css": "onchange 'packages/**/*.scss' --initial --verbose -- npm run build:css",
     "watch:js:bundle": "onchange 'packages/{@uppy/,}*/src/**/*.js' --initial --verbose -- npm run build:bundle",
     "watch:js:bundle": "onchange 'packages/{@uppy/,}*/src/**/*.js' --initial --verbose -- npm run build:bundle",
     "watch:js:lib": "onchange 'packages/{@uppy/,}*/src/**/*.js' --initial --verbose -- npm run build:lib",
     "watch:js:lib": "onchange 'packages/{@uppy/,}*/src/**/*.js' --initial --verbose -- npm run build:lib",