Skip to content

Commit

Permalink
Adds option to change output path
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed Dec 30, 2017
1 parent bef08f0 commit e35c89e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ With that setup the file is updated when `npm start` and `npm run-script build`

## Receiving the versions

The script generates a TypeScript file at the location: `./node_modules/ng-appversion/_versions.ts`.
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`.

You'll be able to import the values just like any other package:
```
import { version } from 'ng-appversion/_versions';
import { version } from '../_versions';
```

The file will contain two version numbers:
Expand Down Expand Up @@ -55,7 +57,7 @@ export const environment = {

*environments/environment.staging.ts*
```
import { versionLong } from 'ng-appversion/_versions';
import { versionLong } from '../_versions';
export const environment = {
production: false,
version: versionLong,
Expand All @@ -64,7 +66,7 @@ export const environment = {

*environments/environment.prod.ts*
```
import { version } from 'ng-appversion/_versions';
import { version } from '../_versions';
export const environment = {
production: true,
version: version,
Expand Down
58 changes: 31 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,52 @@
const path = require('path');
const colors = require('colors/safe');
const fs = require('fs');
const argv = require('yargs').argv;
const packageFile = __dirname + '/../../package.json';
const gitFolder = __dirname + '/../../.git/';

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

const appVersion = require(packageFile).version;

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

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

let src = `export const version = '${appVersion}';\n`;
let src = 'export const version = \'' + appVersion + '\';\n';

if (fs.existsSync(gitFolder)) {
console.log('[NgAppVersion] Git repository detected. Getting current commit information.');
const git = require('git-describe');
try {
const info = git.gitDescribeSync({ longSemver: true });
let versionWithHash = appVersion;
if (info.hasOwnProperty('hash')) {
versionWithHash = versionWithHash + '-' + info.hash;
}
src += `export const versionLong = '${versionWithHash}';\n`;
} catch(e) {
if (new RegExp(/Not a git repository/).test(e.message)) {
console.log('[NgAppVersion] ' + colors.red('Not a Git repository.'));
return;
} else {
console.log('[NgAppVersion] ' + colors.red(e.message));
}
}
console.log('[NgAppVersion] Git repository detected. Getting current commit information.');
const git = require('git-describe');
try {
const info = git.gitDescribeSync({ longSemver: true });
let versionWithHash = appVersion;
if (info.hasOwnProperty('hash')) {
versionWithHash = versionWithHash + '-' + info.hash;
}
src += 'export const versionLong = \'' + versionWithHash + '\';\n';
} catch(e) {
if (new RegExp(/Not a git repository/).test(e.message)) {
console.log('[NgAppVersion] ' + colors.red('Not a Git repository.'));
return;
}
console.log('[NgAppVersion] ' + colors.red(e.message));
}
}

// 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) {
if (err) {
return console.log('[NgAppVersion] ' + colors.red(err));
}

console.log('[NgAppVersion] ' + colors.green(`Updating application version ${colors.yellow(appVersion)}`));
console.log('[NgAppVersion] ' + `${colors.green('Writing version module to ')}${colors.yellow(versionFilePath)}\n`);
if (err) {
return console.log('[NgAppVersion] ' + colors.red(err));
}
console.log('[NgAppVersion] ' + colors.green('File written.'));
});

0 comments on commit e35c89e

Please sign in to comment.