From 35824457552d4a40437bf0cbb88411992dbdc83f Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 19 Dec 2014 17:45:04 -0700 Subject: [PATCH] Add task for publishing to npm This task publishes an existing tag to the npm registry. To publish a new release, create a commit that updates the version number in package.json (e.g. to "3.1.0"). Then create a tag, push to GitHub, and run the publish task. Assuming "openlayers" is the remote for the canonical repo, this would look like the following: git tag -a v3.1.0 -m "3.1.0" git push --tags openlayers ./tasks/publish.sh 3.1.0 The task creates a build for each of the `PROFILES` in `publish.sh` (these correspond to `.json` files in the `config` directory). Builds are generated in the `dist` directory. Our `package.json` specifies `dist/ol.js` as the "main" build. So when people use a module loader to `require('openlayers')`, they get the full build. It is also possible to load a debug build (e.g. `require('openlayers/dist/ol-debug')`), and we can publish additional builds by adding `config` files and updating `PROFILES` in `publish.sh`. The `.npmignore` file determines what is *not* included in the package (note that `node_modules` are always ignored). So if additional items are added to `.gitignore` that should not be included in the npm package, they need to go in `.npmignore` as well (ideally, we don't need to generate anything else outside of the `build` directory that doesn't belong in the package). --- .gitignore | 3 +- .npmignore | 5 +++ package.json | 1 + tasks/publish.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 .npmignore create mode 100755 tasks/publish.sh diff --git a/.gitignore b/.gitignore index a39ab0b3e4..1aa8fc6a55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.pyc -/build +/build/ /examples/*.html.png /examples/example-list.js /examples/example-list.xml /node_modules/ +/dist/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000000..cdb35d22d6 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +*.pyc +/build/ +/examples/*.html.png +/examples/example-list.js +/examples/example-list.xml diff --git a/package.json b/package.json index 8a053a7333..ae546f8db8 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "start": "node tasks/serve.js", "test": "node tasks/test.js" }, + "main": "dist/ol.js", "repository": { "type": "git", "url": "git://github.com/openlayers/ol3.git" diff --git a/tasks/publish.sh b/tasks/publish.sh new file mode 100755 index 0000000000..d9b5d42c3a --- /dev/null +++ b/tasks/publish.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# +# Run this script to publish a new version of the library to npm. This requires +# that you have a clean working directory and have created a tag that matches +# the version number in package.json. +# +set -o errexit + +# +# All profiles to be built. Must correspond to .json files in the config +# directory. +# +PROFILES="ol ol-debug" + +# +# URL for canonical repo. +# +REMOTE=https://github.com/openlayers/ol3.git + +# +# Display usage and exit. +# +display_usage() { + cat <<-EOF + + Usage: ${1} + + To publish a new release, update the version number in package.json and + create a tag for the release. + + The tag name must match the version number prefixed by a "v" (for example, + version 3.2.1 would be tagged v3.2.1). + + The tag must be pushed to ${REMOTE} before the release can be published. + +EOF + exit 1 +} + +# +# Exit if the current working tree is not clean. +# +assert_clean() { + source `git --exec-path`/git-sh-setup && \ + require_clean_work_tree "publish" "Please commit or stash them." +} + +# +# Exit if the requested version doesn't match package.json. +# +assert_version_match() { + v=`grep -o '"version":.*' package.json | sed 's/"version": *"\(.*\)",/\1/'` + if test "${1}" != "${v}"; then + echo "Version mismatch: requested '${1}', but package.json specifies '${v}'" + exit 1 + fi +} + +# +# Build all of the distribution profiles. +# +build_profiles() { + for p in ${@}; do + echo building dist/${p}.js + node ./tasks/build.js config/${p}.json dist/${p}.js + done +} + +# +# Check out the provided tag. This ensures that the tag has been pushed to +# the canonical remote. +# +checkout_tag() { + git fetch ${REMOTE} refs/tags/v${1}:refs/tags/v${1} + git checkout refs/tags/v${1} +} + +# +# Build all profiles and publish. +main() { + root=$(cd -P -- "$(dirname -- "${0}")" && pwd -P)/.. + cd ${root} + assert_clean + checkout_tag ${1} + assert_version_match ${1} + rm -rf dist + build_profiles ${PROFILES} + npm publish +} + +if test ${#} -ne 1; then + display_usage ${0} +else + main ${1} +fi