diff --git a/CHANGELOG.md b/CHANGELOG.md
index ddecda7..00a2ffb 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 customizing pre-release thru `--template`, 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..cb80c0b 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_TEMPLATE = 'branch.short';
+
program
.version(ourPackageJSON.version)
.description(ourPackageJSON.description)
.option('-p, --path ', 'path to package.json, default to current directory', process.cwd())
+ .option('--template ', 'pre-release version template', DEFAULT_TEMPLATE)
.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,17 @@ function main() {
}
const { version } = packageJSON;
+ const preRelease = (program.template || DEFAULT_TEMPLATE).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) }`);
@@ -83,9 +96,9 @@ function main() {
log(`Running ${ magenta(`npm ${ args.join(' ') }`) }`);
- const result = spawn.sync('npm', args, { cwd, stdio: 'inherit' });
+ // const result = spawn.sync('npm', args, { cwd, stdio: 'inherit' });
- process.exit(result);
+ // process.exit(result);
}
main();