Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<br />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.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"keywords": [
"branch",
"bump",
"ci",
"commit",
"git",
"travis",
"version"
],
"license": "MIT",
Expand Down
23 changes: 18 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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>', 'path to package.json, default to current directory', process.cwd())
.option('--template <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 <message>', 'run "npm version" with --message')
Expand All @@ -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') }`);
Expand All @@ -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?'));
Expand All @@ -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) }`);

Expand All @@ -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();