Skip to content

Commit abdc80b

Browse files
authored
Merge pull request #510 from dotnet/nbgvNpmBinScripts
Add node.js bin scripts to nb-gv NPM package
2 parents 0abc798 + f545ed0 commit abdc80b

File tree

6 files changed

+84
-22
lines changed

6 files changed

+84
-22
lines changed

src/nerdbank-gitversioning.npm/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Nerdbank.GitVersioning
2-
======================
1+
# nerdbank-gitversioning
32

43
With this package, and a version.json file to express your version number
54
checked into the root of your git repo:
@@ -13,6 +12,52 @@ checked into the root of your git repo:
1312
Your NPM packages and other builds can be automatically stamped with a
1413
version that precisely describes the git commit that built it.
1514

15+
## CLI use
16+
17+
Stamp your package.json file with the git-based version:
18+
19+
```sh
20+
nbgv-setversion
21+
```
22+
23+
Reset your package.json file with a version placeholder (suitable for checking in):
24+
25+
```sh
26+
nbgv-setversion --reset
27+
```
28+
29+
Or invoke the `nbgv` tool directly for many options:
30+
31+
```sh
32+
nbgv -?
33+
```
34+
35+
### Pack script
36+
37+
A possible script to pack your NPM package:
38+
39+
```sh
40+
yarn nbgv-setversion
41+
yarn pack
42+
yarn nbgv-setversion --reset
43+
```
44+
45+
## Programmatic consumption
46+
47+
```ts
48+
import * as nbgv from 'nerdbank-gitversioning'
49+
50+
// Retrieve all sorts of version information. Print just one bit.
51+
const versionInfo = await nbgv.getVersion();
52+
console.log(versionInfo.npmPackageVersion);
53+
54+
// Stamp the package.json file in the current directory with the computed version.
55+
await nbgv.setPackageVersion();
56+
57+
// After packing, reset your package.json file to using a placeholder version number.
58+
await nbgv.resetPackageVersionPlaceholder();
59+
```
60+
1661
See our [project README][GitHubREADME] for more information.
1762

1863
[GitHubREADME]: https://github.com/dotnet/Nerdbank.GitVersioning/blob/master/README.md

src/nerdbank-gitversioning.npm/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"email": "[email protected]",
88
"url": "http://blog.nerdbank.net/"
99
},
10+
"bin": {
11+
"nbgv": "main.js",
12+
"nbgv-setversion": "npmVersion.js"
13+
},
1014
"repository": {
1115
"type": "git",
1216
"url": "https://github.com/dotnet/Nerdbank.GitVersioning.git"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
const nbgvPath = 'nbgv.cli';
5+
6+
export function getNbgvCommand(dotnetCommand?: string): string {
7+
var command = dotnetCommand || 'dotnet';
8+
const nbgvDll = path.join(__dirname, nbgvPath, "tools", "netcoreapp2.1", "any", "nbgv.dll");
9+
return `${command} "${nbgvDll}"`;
10+
}

src/nerdbank-gitversioning.npm/ts/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
22

3-
import * as fs from 'fs';
4-
import * as path from 'path';
53
import * as camelCase from 'camel-case';
6-
import {execAsync} from './asyncprocess';
7-
8-
const nbgvPath = 'nbgv.cli';
4+
import { execAsync } from './asyncprocess';
5+
import { getNbgvCommand } from './core';
96

107
/**
118
* The various aspects of a version that can be calculated.
@@ -43,9 +40,7 @@ export interface IGitVersion {
4340
*/
4441
export async function getVersion(projectDirectory?: string, dotnetCommand?: string): Promise<IGitVersion> {
4542
projectDirectory = projectDirectory || '.';
46-
var command = dotnetCommand || 'dotnet';
47-
var getVersionScriptPath = path.join(__dirname, nbgvPath, "tools", "netcoreapp2.1", "any", "nbgv.dll");
48-
var versionText = await execAsync(`${command} "${getVersionScriptPath}" get-version --format json`, { cwd: projectDirectory })
43+
var versionText = await execAsync(`${getNbgvCommand(dotnetCommand)} get-version --format json`, { cwd: projectDirectory })
4944
if (versionText.stderr) {
5045
throw versionText.stderr;
5146
}
@@ -83,7 +78,7 @@ export async function setPackageVersion(packageDirectory?: string, srcDirectory?
8378
*/
8479
export async function resetPackageVersionPlaceholder(srcDirectory?: string) {
8580
srcDirectory = srcDirectory || '.';
86-
var result = await execAsync(`npm version 0.0.0-placeholder`, { cwd: srcDirectory });
81+
var result = await execAsync(`npm version 0.0.0-placeholder --no-git-tag-version --allow-same-version`, { cwd: srcDirectory });
8782
if (result.stderr) {
8883
console.log(result.stderr);
8984
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
'use string';
1+
#!/usr/bin/env node
22

3-
import * as lib from './index'
3+
'use strict';
44

5-
async function printGitVersion() {
6-
try {
7-
console.log(await lib.getVersion());
8-
} catch (err) {
9-
console.log('Failed to get version:');
10-
console.log(err);
11-
}
12-
}
5+
import { getNbgvCommand } from "./core";
136

14-
printGitVersion();
7+
const { spawn } = require('child_process');
8+
const { argv, exit } = require('process');
9+
10+
const cp = spawn(`${getNbgvCommand()} ${argv.slice(2).join(' ')}`, { shell: true, stdio: "inherit" })
11+
cp.once('exit', code => exit(code))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
3+
import * as lib from './index'
4+
5+
(async () => {
6+
if (process.argv[2] === '--reset') {
7+
await lib.resetPackageVersionPlaceholder();
8+
} else {
9+
await lib.setPackageVersion();
10+
}
11+
})();

0 commit comments

Comments
 (0)