From d1e12b6dde680194122d0bd6ab58a1c4e4e12d20 Mon Sep 17 00:00:00 2001 From: William Wong Date: Mon, 12 Aug 2019 11:51:06 -0700 Subject: [PATCH 1/3] Customize pre-release pattern --- CHANGELOG.md | 3 +++ README.md | 18 +++++++++++++++--- package.json | 2 ++ src/index.js | 20 +++++++++++++++++--- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddecda7..7cf756f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- Fix [#2](https://github.com/compulim/version-from-git/issues/2). Supports customize pre-release pattern, in [PR #3](https://github.com/compulim/version-from-git/pulls/3) + ## [1.0.0] - 2018-05-18 ### Added - Initial commit diff --git a/README.md b/README.md index ba4cdca..1e418b3 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ We want to use `npm version` to bump to a customized version that contains both Git branch and commit hash. -And use Travis CI to automatically publish the prerelease version to NPM, tagged using [`npm dist-tag`](https://docs.npmjs.com/cli/dist-tag). +And use Travis CI to automatically publish the pre-release version to NPM, tagged using [`npm dist-tag`](https://docs.npmjs.com/cli/dist-tag). -> Instead of using plus (+) to denote build information, we prefer period (.) for simpler escapes. +> Instead of using plus (+) to denote build information, we prefer period (.) for simpler escapes. If you prefer the plus sign, you can [customize the pre-release version pattern](#customizing-pre-release-version-pattern). # How to use -Run `npx version-from-git`, it will run `npm version 1.0.0-master+1a2b3c4`. +Run `npx version-from-git`, it will run `npm version 1.0.0-master.1a2b3c4`. ``` Usage: version-from-git [options] @@ -39,6 +39,18 @@ In Travis, when you push a tag (probably by `npm version 1.0.0` followed by `git Run `npx version-from-git -t`, it will detect whether `TRAVIS_TAG` environment variable is present and skip. +## Customizing pre-release version pattern + +You can customize the version pattern when tagging for pre-release versions. + +| Pattern name | Description | Sample | +|--------------|------------------------------------------------------------------|--------------------------------------------| +| `branch` | Branch name
In Travis, will use `process.env.TRAVIS_BRANCH` | `master` | +| `long` | Git commit in long form | `3807f9004867438c57a3e26f2073c33c458d4ef9` | +| `short` | Git commit in short form | `3807f90` | + +Default pattern is `branch.short`, which would produce `master.1a2b3c4`. + # Contributions Like us? [Star](https://github.com/compulim/version-from-git/stargazers) us. diff --git a/package.json b/package.json index 56ccbee..29e885e 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,10 @@ "keywords": [ "branch", "bump", + "ci", "commit", "git", + "travis", "version" ], "license": "MIT", diff --git a/src/index.js b/src/index.js index 5c8fdb4..5ab33e1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const { branch: gitBranch, short: gitShort } = require('git-rev-sync'); +const { branch: gitBranch, short: gitShort, long: gitLong } = require('git-rev-sync'); const { join, resolve } = require('path'); const { major, minor, patch } = require('semver'); const { green, magenta, red, yellow } = require('chalk'); @@ -11,10 +11,13 @@ const spawn = require('cross-spawn'); const { log } = console; const ourPackageJSON = require(join(__dirname, '../package.json')); +const DEFAULT_PATTERN = 'branch.short'; + program .version(ourPackageJSON.version) .description(ourPackageJSON.description) .option('-p, --path ', 'path to package.json, default to current directory', process.cwd()) + .option('--pattern ', 'pre-release version pattern', DEFAULT_PATTERN) .option('-t, --travis', 'run in Travis CI: skip when TRAVIS_TAG present') .option('-f, --force', 'run "npm version" with --force') .option('-m, --message ', 'run "npm version" with --message') @@ -27,7 +30,7 @@ program function main() { log(`Running ${ green(`${ ourPackageJSON.name }@${ ourPackageJSON.version }`) }`); - let branch, short; + let branch, long, short; if (program.travis) { log(`Travis mode ${ magenta('enabled') }`); @@ -47,6 +50,7 @@ function main() { try { branch = branch || gitBranch(cwd); + long = gitLong(cwd); short = gitShort(cwd); } catch (err) { log(red('Failed to read from .git directory, is this a Git branch with at least one commit?')); @@ -65,8 +69,18 @@ function main() { } const { version } = packageJSON; + const { pattern = DEFAULT_PATTERN } = program; + const preRelease = pattern.replace(/\w+/giu, name => { + switch (name) { + case 'branch': return branch; + case 'short': return short; + case 'long': return long; + + default: return name; + } + }); - const nextVersion = `${ major(version) }.${ minor(version) }.${ patch(version) }-${ branch }.${ short }`; + const nextVersion = `${ major(version) }.${ minor(version) }.${ patch(version) }-${ preRelease }`; log(`Bumping from ${ green(version) } to ${ green(nextVersion) }`); From e6ba25af7503fdf2e203ef363fc933661d6a7b50 Mon Sep 17 00:00:00 2001 From: William Wong Date: Mon, 12 Aug 2019 11:56:24 -0700 Subject: [PATCH 2/3] Rename pattern to template --- CHANGELOG.md | 2 +- src/index.js | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cf756f..b55d834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added -- Fix [#2](https://github.com/compulim/version-from-git/issues/2). Supports customize pre-release pattern, in [PR #3](https://github.com/compulim/version-from-git/pulls/3) +- Fix [#2](https://github.com/compulim/version-from-git/issues/2). Supports customizing pre-release, in [PR #3](https://github.com/compulim/version-from-git/pulls/3) ## [1.0.0] - 2018-05-18 ### Added diff --git a/src/index.js b/src/index.js index 5ab33e1..cb80c0b 100644 --- a/src/index.js +++ b/src/index.js @@ -11,13 +11,13 @@ const spawn = require('cross-spawn'); const { log } = console; const ourPackageJSON = require(join(__dirname, '../package.json')); -const DEFAULT_PATTERN = 'branch.short'; +const DEFAULT_TEMPLATE = 'branch.short'; program .version(ourPackageJSON.version) .description(ourPackageJSON.description) .option('-p, --path ', 'path to package.json, default to current directory', process.cwd()) - .option('--pattern ', 'pre-release version pattern', DEFAULT_PATTERN) + .option('--template