Skip to content

Commit

Permalink
Deprecates --force-git in favour of --git; improves argument list in …
Browse files Browse the repository at this point in the history
…README
  • Loading branch information
saitho committed Feb 9, 2019
1 parent c983296 commit fe46a7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,28 @@ You might want to use *prestart* and *prebuild* inside your package.json for tha
With that setup the file is updated when `npm start` and `npm run-script build` are run.
*Note:* You won't be able to run `ng build` anymore as the script will not be executed. Use `npm run-script build` instead.

## Receiving the versions
## Command arguments

| Argument | Meaning | Default |
|---|---|---|
| --root | root directory where your package.json is located | . |
| --file | relative location of the output file (based on the root directory) | ./src/_version.ts |
| --force-git | force Git detection (deprecated, use --git instead to point to your .git directory) | false |
| --git | relative location of the .git directory to use (based on the root directory) | . |

The script generates a TypeScript file at the location: `./src/_versions.ts`.
You can change that by passing the option *--file*, e.g. `node ./node_modules/ng-appversion/index.js --file=src/config/version.ts`.
If your .git folder is not in the same folder as your node_modules folder, automatic detection won't work. In that case you can add *--force-git* to force detection. If your .git folder has another location you can use *--git* to specify the path.
## Receiving the versions

The script generates a TypeScript file at the location `./src/_versions.ts` if you haven't provided a different location.
You'll be able to import the values just like any other package:
```
import { version } from '../_versions';
```

The file will contain two version numbers:

* **version** is the version from the packages.json (e.g. v1.0.0)
* **versionLong** is the version from the packages.json PLUS the Hash of the current Git-Commit (e.g. v1.0.0-g63962e3)
* **versionLong** is the version from the packages.json PLUS the Hash of the current Git-Commit (e.g. v1.0.0-g63962e3) - will only be generated if your repository is a Git Repository

## Environment-related versions

Expand Down
26 changes: 18 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ const path = require('path');
const colors = require('colors/safe');
const fs = require('fs');
const argv = require('yargs').argv;
const packageFile = __dirname + '/../../package.json';
let gitFolder = __dirname + '/../../.git/';
let rootPath = path.join('..', '..');
if (argv.hasOwnProperty('root')) {
rootPath = argv.root;
}
const packageFile = path.join(__dirname, rootPath, 'package.json');
let gitFolder = path.join(__dirname, rootPath, '.git');

// TODO: remove with v2
if (argv.hasOwnProperty('force-git')) {
console.log('Note: The use of --force-git is ' + colors.red('deprecated') + ' and will be removed with version 2. Use --git instead to point to the .git directory.');
}

if (!fs.existsSync(packageFile)) {
console.log('[NgAppVersion] ' + colors.yellow('Cannot find packages.json in root path. Skipping...'));
console.log('[NgAppVersion] ' + colors.yellow('Cannot find package.json in root path. Skipping...'));
return;
}

const appVersion = require(packageFile).version;

let versionFile = __dirname + '/../../src/_versions.ts';
let versionFile = path.join(__dirname, rootPath, 'src', '_versions.ts');
if (argv.hasOwnProperty('file')) {
versionFile = __dirname + '/../../' + argv.file;
versionFile = path.join(__dirname, rootPath, argv.file);
}

console.log(colors.cyan('\nWriting version strings to ' + versionFile));
const versionFilePath = path.join(versionFile);

let src = 'export const version = \'' + appVersion + '\';\n';

Expand All @@ -30,6 +38,8 @@ if (argv.hasOwnProperty('git')) {
gitFolder = path.resolve(__dirname, argv.git);
}
if (argv.hasOwnProperty('force-git')) {
// TODO: remove with v2
// this option is required e.g. when the repository in question is a sub repository
enableGit = true;
console.log('[NgAppVersion] Git repository forced. Getting current commit information.');
} else if (fs.existsSync(gitFolder)) {
Expand Down Expand Up @@ -57,8 +67,8 @@ if (enableGit) {

// ensure version module pulls value from package.json
console.log('[NgAppVersion] ' + colors.green('Updating application version ') + colors.yellow(appVersion));
console.log('[NgAppVersion] ' + colors.green('Writing version module to ') + colors.yellow(versionFilePath));
fs.writeFile(versionFilePath, src, { flat: 'w' }, function (err) {
console.log('[NgAppVersion] ' + colors.green('Writing version module to ') + colors.yellow(versionFile));
fs.writeFile(versionFile, src, { flat: 'w' }, function (err) {
if (err) {
return console.log('[NgAppVersion] ' + colors.red(err));
}
Expand Down

0 comments on commit fe46a7f

Please sign in to comment.