Skip to content

Commit

Permalink
feat(process): pass version to pre-commit script
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #26
  • Loading branch information
Brett Uglow authored and Brett Uglow committed Feb 24, 2017
1 parent a75680a commit 7febd66
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,52 @@ Of course you can change `corp-release` to any name you like.

## Options
* `-d` or `--dryrun`: it runs in non-destructive mode. No alteration should be done in your workspace.
* `--pre-commit [pre-commit]`: Pre-commit hook [pre-commit]. Pass a string with the name of the npm script to run. it will run like this: `npm run [pre-commit]`. If you need more hooks to be implemented please open an issue.
* `--pre-commit [npm-script]`: Pre-commit hook [pre-commit]. Pass a string with the name of the npm script to run. it will run like this: `npm run [pre-commit]`. If you need more hooks to be implemented please open an issue.
* `-b [branch]` or `--branch [branch]`: Branch name allowed to run release. Default is `master`. If you want to release from another branch, you need to specify.
* `-v` or `--verbose`: it prints extra info such as commit list from last tag and command details.
* `--changelogpreset [preset]`: The conventional-changelog preset to use. Default is `angular`. `angular-bitbucket` is available for [BitBucket repositories](https://github.com/uglow/conventional-changelog-angular-bitbucket). Other presets can be installed, e.g: `npm i conventional-changelog-jquery` then pass this flag to the command: `--changelogpreset jquery`.

**NOTE**: If you run via `npm`, you have to add `--` before the options so npm passes all arguments to node. Eg.: `npm run corp-release -- -v -d`


## Updating other files
A pretty common requirement when updating the version number is to update other files with
the same version number. There are two ways you can run your own scripts to update additional files:

<details>
<summary>Option 1 - NPM hook</summary>
You can use NPM's built-in `(pre|post)version` [script-hook](https://docs.npmjs.com/cli/version) to run code before/just-after/after `package.json` is modified by `corp-semantic-release`.

In the following example, `updateOtherFiles.js` does *NOT* receive the version as an argument but must query `package.json` to get the bumped version.
```json

"scripts": {
"corp-release": "corp-semantic-release",
"version": "node updateOtherFiles.js"
}

```
</details>


<details>
<summary>Option 2 - `--pre-commit [npm-script]`</summary>
`corp-semantic-release` also provides a `--pre-commit <NPM script>` option. The NPM script is passed the version
number as an argument to the script.

```json

"scripts": {
"corp-release": "corp-semantic-release --pre-commit updateFiles",
"updateFiles": "node updateOtherFiles.js"
}

```
</details>

Remember to stage the files using `git add <file-name>` after modifying the files, so that when `corp-semantic-release` commits the changes, all the changed files are commited.


## Contribute

Please refer to the [Contributor Guidelines](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md) and [Conduct of Code](https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md) from [AngularJs](https://github.com/angular/angular.js) project.
Expand Down
15 changes: 13 additions & 2 deletions spec/system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,22 @@ describe('corp-semantic-release', function() {
});


it('should run pre-commit script if required', function() {
it('should run pre-commit script and pass the version number to the npm script', function() {
commitFeat();
const out = semanticRelease(`-v --pre-commit set-version`);

expect(out).to.include('this is my pre-commit script');
expect(out).to.include('this is my pre-commit script v1.0.0');
});

it('should run pre-commit script and pass the version number to a node script referenced by the npm script', function() {
commitFeat();
shell.exec('rm package.json');
shell.cp(__dirname + '/testData/package_precommit.json', tempDir + '/package.json');
shell.cp(__dirname + '/testData/precommit.js', tempDir);

const out = semanticRelease(`-v --pre-commit set-version`);

expect(out).to.include('Inside precommit.js, version is v1.0.0');
});


Expand Down
12 changes: 12 additions & 0 deletions spec/testData/package_precommit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.0.1",
"name": "corp-semantic-release-test",
"author": "leonardoanalista",
"scripts": {
"set-version": "node precommit.js"
},
"repository": {
"type": "git",
"url": "https://any.git.host/owner-name/repo-name.git"
}
}
1 change: 1 addition & 0 deletions spec/testData/precommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Inside precommit.js, version is', process.argv[2]);
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function(err, results) {
}

// ### STEP 8 - Run if any pre commit script has been specified (DESTRUCTIVE OPERATION)
lib.runPreCommitScript(program.preCommit);
lib.runPreCommitScript(program.preCommit, version);
// ### STEP 9 - Tag and push (DESTRUCTIVE OPERATION)
if (!program.dryrun) lib.addFilesAndCreateTag(version);
});
4 changes: 2 additions & 2 deletions src/lib/bumpUpVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const log = require('./log');

module.exports = function bumpUpVersion(bumpType, latestTag) {
log.info('>>> update version on package.json...');
try {
let version = 'v1.0.0'; // First Release
let version = 'v1.0.0'; // First Release

try {
if (helpers.isFirstRelease(latestTag)) {
shell.exec('npm version --no-git-tag-version ' + version).output.split('\n')[0];
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/runPreCommitScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
const log = require('./log');
const shell = require('shelljs');

module.exports = function runPreCommitScript(script) {
module.exports = function runPreCommitScript(script, version) {
if (script) {
log.info(`>>> about to run your "pre-commit" script called "${script}". Command is: npm run ${script}`);
shell.exec(`npm run ${script}`).output;
shell.exec(`npm run ${script} -- ${version}`).output;
}
};

1 comment on commit 7febd66

@leonardoanalista
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one! 👍
Now I remember what this pre-commit was about:
The initial intention of this pre-commit was to run a Grunt script replace a placeholder in the app/index.html with the new version number. Eg.:

<html>
<head>
  <javascript>
    window.global.appVersion = @@versionPlaceholder@@
  </javascript>
<head>
...
</html>

Previously the script would have to look at the staged files in order to work out the new version. Now the new version is passed as an arg to the script. Is that right?

Please sign in to comment.