Skip to content

Commit

Permalink
feat(app): add files to git and update readme
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Files are now \`git add\`-ed, which may be a breaking change.
  • Loading branch information
uglow committed Feb 28, 2017
1 parent d292556 commit 7b1e06f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 18 deletions.
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# upver

<!--[]-->
> Updates the version string within a list of files with a new version string.
> Updates the version string within a list of files with a new version string, and `git add`s the changed files.
This module can be used with [corp-semantic-release](https://github.com/leonardoanalista/corp-semantic-release) to
This module can be used with [corp-semantic-release](https://github.com/leonardoanalista/corp-semantic-release) or plain NPM to
update the version number in a list of files when the package's version number is changed. See [Usage](#usage)


Expand All @@ -23,7 +23,7 @@ update the version number in a list of files when the package's version number i
## Install

1. `npm install upver`
2. Create a YAML configuration file with this format (Note the `<@VERSION@>` placeholder for the new version string):
2. Create a YAML configuration file with this format (and note the `<@VERSION@>` placeholder for the *new* version string):
```yaml
- file: path/to/file/relative/to/project/root.yml
search: Regex search for, e.g.: '"version": (".+")'
Expand All @@ -46,19 +46,19 @@ update the version number in a list of files when the package's version number i
This module gets the version number as an argument to the module, or from `package.json`.
There are two ways you would normally use `upver`:
There are three ways you could use `upver`:
<details>
<summary>Option 1 - Use with an 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 the version in `package.json` has been changed.
The files that are changed are `git add`ed in preparation for the changes being committed by `npm version` in subsequent steps.
In the following example, `upver` does *NOT* receive the version as an argument but queries `package.json` to get the bumped version.
```json
"scripts": {
"version": "upver"
}
```
</details>

Expand All @@ -68,17 +68,41 @@ In the following example, `upver` does *NOT* receive the version as an argument
`corp-semantic-release` provides a `--pre-commit <NPM script>` option. `upver` is passed the version
number as an argument to the script.

```json
The files that are changed are `git add`ed in preparation for the changes being committed `corp-semantic-release` in subsequent steps.

Both of the following examples are equivalent:
```json
"scripts": {
"corp-release": "corp-semantic-release --pre-commit updateFiles",
"updateFiles": "upver"
}

```
```json
"scripts": {
"corp-release": "corp-semantic-release",
"version": "upver"
}
```
</details>


<details>
<summary>Option 3 - Use with `semantic-release`</summary>
The [semantic-release](https://github.com/semantic-release/semantic-release) package provides hooks to allow `upver` to be called after
`package.json` has been updated.

**NOTE:** `semantic-release` does **not** commit file changes to git, but rather publishes the changes to NPM, then uploads a ZIP file to GitHub.
This means that the files versioned by `upver` will only contain the correct version when you install the module (not in git or on your
file-system). That's just how the `semantic-release` tool works.

Example:
```json
"scripts": {
"semantic-release": "semantic-release pre && upver && npm publish && semantic-release post"
}
```
</details>


<!--[RM_CONTRIBUTING]-->
## Contributing
Expand Down
4 changes: 2 additions & 2 deletions config/testUnit/thresholds.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"global": {
"statements": 80,
"branches":75,
"functions": 60,
"branches":80,
"functions": 80,
"lines": 80
},
"each": {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "upver",
"version": "0.0.0-development",
"description": "Updates the version number inside a list of files with a supplied version number.",
"description": "Updates the version string within a list of files with a new version string, and `git add`s the changed files.",
"license": "MIT",
"files": [
"src",
Expand Down Expand Up @@ -43,7 +43,6 @@
}
},
"dependencies": {
"app-root-path": "2.0.1",
"read-yaml": "1.0.0"
},
"devDependencies": {
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env node
'use strict';

const fs = require('fs');
const APP_ROOT = require('app-root-path').path;
// The packages that use 'let' instead of 'const' are stubbed in the unit tests using rewire()
let fs = require('fs');
const appRoot = process.cwd();
const path = require('path');
const readYaml = require('read-yaml');
let execSync = require('child_process').execSync;

const verRegEx = /^\d+\.\d+\.\d+$/;
const verRegEx = /^\d+\.\d+\.\d+.*$/;
const REPLACE_TOKEN = '<@VERSION@>';

module.exports = {
Expand All @@ -22,7 +24,7 @@ let pkgCache;

function getPackageData() {
if (!pkgCache) {
pkgCache = require(path.join(APP_ROOT, '/package.json')); // Find the root package
pkgCache = require(`${appRoot}/package.json`); // Find the root package
}
return pkgCache;
}
Expand Down Expand Up @@ -59,14 +61,15 @@ function updateFiles(configData, version, writeChanges) {

// Patch the files
configData.forEach((config) => {
let fileName = path.join(APP_ROOT, config.file);
let fileName = path.join(appRoot, config.file);
let text = fs.readFileSync(fileName, 'utf8');
let regEx = new RegExp(config.search, 'gm');

text = text.replace(regEx, config.replacement.replace(REPLACE_TOKEN, version));

if (writeChanges) {
fs.writeFileSync(fileName, text, 'utf8');
execSync(`git add ${config.file}`);
console.log(`[upver]: Updated version in ${config.file}`);
} else {
console.info(`[upver]: Updated version in ${config.file}:`);
Expand Down
42 changes: 41 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ describe('upver', () => {

assert.throws(() => module.getVersionNumber('foo'), /Error: A version number was not supplied and is not present in package\.json\./);
});

it('should throw an error if the version number in package.json and the argument is not valid', () => {
module.__set__({
getPackageData: () => ({version: '1.2.3-development'}),
});

let result = module.getVersionNumber();
assert.equal(result, '1.2.3-development');
});
});


Expand Down Expand Up @@ -102,7 +111,7 @@ describe('upver', () => {
});
});

it('should update the files correctly', () => {
it('should fake-update the files correctly (when writeChanges is false)', () => {
const MOCK_VERSION = '4.7.0';
const writeChanges = false;
const configData = module.getConfigFileData('test/fixtures/valid-config.yml');
Expand All @@ -120,5 +129,36 @@ describe('upver', () => {
assert.equal(output[5], '{\n "foo": "bar",\n "version": "4.7.0",\n "bar": "car"\n}\n\n');
assert.equal(output[6], '---------------\n');
});


it('should update the files correctly and add them to git (when writeChanges is true)', () => {
const MOCK_VERSION = '4.7.0';
const writeChanges = true;
const configData = module.getConfigFileData('test/fixtures/valid-config.yml');
const stdout = require('test-console').stdout;
let writeFileCallCount = 0;
let gitCommands = [];

module.__set__({
fs: {
readFileSync: require('fs').readFileSync,
writeFileSync: () => writeFileCallCount++,
},
execSync: (cmdStr) => gitCommands.push(cmdStr),
});

let output = stdout.inspectSync(() => {
module.updateFiles(configData, MOCK_VERSION, writeChanges);
});

assert.equal(configData.length, writeFileCallCount);

assert.equal(output[0], '[upver]: Latest version of test-upver: 4.7.0\n');
assert.equal(output[1], '[upver]: Updated version in test/fixtures/a.txt\n');
assert.equal(output[2], '[upver]: Updated version in test/fixtures/b.json\n');

assert.equal(gitCommands[0], 'git add test/fixtures/a.txt');
assert.equal(gitCommands[1], 'git add test/fixtures/b.json');
});
});
});

0 comments on commit 7b1e06f

Please sign in to comment.