diff --git a/README.md b/README.md index 856ec77..ea49d95 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,35 @@ With that setup the file is updated when `npm start` and `npm build` are run. ## 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: +You'll be able to import the values just like any other package, if you want use just versions information, like in environment.ts example file: ``` -import { versions, TsAppVersion } from '../_versions'; +import versions from '../_versions'; +``` +or you can import also TsAppVersion and use direclty in your template, like in app.component.ts example file ``` +import { TsAppVersion } from 'src/_versions.ts'; +import versions from 'src/_versions.ts'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'] +}) +export class AppComponent { + public readonly tsAppVersion: TsAppVersion; + constructor() { + this.tsAppVersion = versions; + } +} +``` + +> backward compatibility with previous version is guarantee The file will export an object with following variables: * **version** is the version from the packages.json (e.g. v1.0.0) +* **name** is the name from the packages.json (e.g. 'sample-app') +* **description** is the description from the packages.json * **versionDate** is the timestamp in ISO format when the compilation/package started. * **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 * **gitTag** is the latest Git tag diff --git a/dist/index.js b/dist/index.js index 8b80763..e2b3682 100644 --- a/dist/index.js +++ b/dist/index.js @@ -50,20 +50,33 @@ const outputFile = argv.hasOwnProperty('file') ? argv.file : path.join('src', '_ const versionFile = path.join(projectFolder, outputFile); // pull version from package.json -const appVersion = require(packageFile).version; +const pkg = require(packageFile); +const appVersion = pkg.version; +const appName = pkg.name; +const appDescription = pkg.description; console.log('[TsAppVersion] ' + colors.green('Application version (from package.json): ') + colors.yellow(appVersion)); +console.log('[TsAppVersion] ' + colors.green('Application description (from package.json): ') + colors.yellow(appName)); + +let src = `export interface TsAppVersion { version: string; + name: string; + description?: string; versionLong?: string; versionDate: string; gitCommitHash?: string; gitCommitDate?: string; gitTag?: string; -} -export const versions: TsAppVersion = { +}; +const obj: TsAppVersion = { version: '${appVersion}', - versionDate: '${new Date().toISOString()}' + name: '${appName}', + versionDate: '${new Date().toISOString()}', `; +if (appDescription !== undefined && appDescription !== '') { + console.log('[TsAppVersion] ' + colors.green('Application description (from package.json): ') + colors.yellow(appDescription)); + src += ` description: '${appDescription}',\n`; +} let enableGit = false; let gitFolder = projectFolder; @@ -120,6 +133,7 @@ if (enableGit) { } src += `}; +export default obj; `; console.log('[TsAppVersion] ' + colors.green('Writing version module to ') + colors.yellow(versionFile)); diff --git a/example/src/app/app.component.css b/example/src/app/app.component.css index 902d115..16b6640 100644 --- a/example/src/app/app.component.css +++ b/example/src/app/app.component.css @@ -84,7 +84,7 @@ svg.material-icons:not(:last-child) { width: 200px; margin: 0 8px 16px; padding: 16px; - display: flex; + display: block; flex-direction: row; justify-content: center; align-items: center; @@ -97,7 +97,7 @@ svg.material-icons:not(:last-child) { } .card.card-small { - height: 16px; + height: 180px; width: 168px; } @@ -122,10 +122,7 @@ svg.material-icons:not(:last-child) { width: auto; min-width: 30%; position: relative; -} - -.card.card.highlight-card span { - margin-left: 60px; + padding: 16px 16px 16px 75px; } svg#rocket { diff --git a/example/src/app/app.component.html b/example/src/app/app.component.html index 7eb1a6d..7141192 100644 --- a/example/src/app/app.component.html +++ b/example/src/app/app.component.html @@ -44,7 +44,14 @@ - This app is running with version {{ version }} + This app is running with version {{ version }} (from environment) / {{ tsAppVersion.version }} (from tsAppVersion direclty) and has following information: + @@ -61,7 +68,9 @@ diff --git a/example/src/app/app.component.ts b/example/src/app/app.component.ts index bd834a9..17df3f2 100644 --- a/example/src/app/app.component.ts +++ b/example/src/app/app.component.ts @@ -1,5 +1,8 @@ import { Component } from '@angular/core'; -import {environment} from "../environments/environment"; +import { environment } from "../environments/environment"; + +import { TsAppVersion } from 'src/_versions'; +import versions from 'src/_versions'; @Component({ selector: 'app-root', @@ -7,6 +10,19 @@ import {environment} from "../environments/environment"; styleUrls: ['./app.component.css'] }) export class AppComponent { - title = 'sample-app'; - version = environment.version; + public title: string = 'sample-app'; + + // form environment + public version: string = environment.version; + + // from _version.ts directly + public readonly tsAppVersion: TsAppVersion; + + /** + * AppComponent ctor + */ + constructor() { + // you can refer direcly to 'versions' information in your component + this.tsAppVersion = versions; + } } diff --git a/test/index.js b/test/index.js index cc59e62..001c8ad 100644 --- a/test/index.js +++ b/test/index.js @@ -38,7 +38,7 @@ describe('appversion', function() { it('should succeed with default settings and without a Git repository', function(done) { fs.mkdirSync(path.join(repoDir, 'src'), {recursive: true}); - fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0"}'); + fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0", "name": "test", "description": "test description"}'); exec('node dist/index.js --root=' + repoDir, (err, stdout, stderr) => { if (err) { done('Test failed: Could not execute command.'); @@ -55,8 +55,26 @@ describe('appversion', function() { return; } const fileContents = fs.readFileSync(outputFile, 'utf8'); + + // interface well format test + expect(fileContents).to.contains('export interface TsAppVersion {'); // interface start + expect(fileContents).to.contains('version: string;'); + expect(fileContents).to.contains('name: string;'); + expect(fileContents).to.contains('description?: string;'); + expect(fileContents).to.contains('versionLong?: string;'); + expect(fileContents).to.contains('versionDate: string;'); + expect(fileContents).to.contains('gitCommitHash?: string;'); + expect(fileContents).to.contains('gitCommitDate?: string;'); + expect(fileContents).to.contains('gitTag?: string;'); + expect(fileContents).to.contains('};\nconst obj: TsAppVersion = {'); // interface end + obj start + + // data test expect(fileContents).to.contains('version: \'1.0.0\','); + expect(fileContents).to.contains('name: \'test\','); + expect(fileContents).to.contains('description: \'test description\','); + expect(fileContents).to.contains('};\nexport default obj;\n'); // export default obj + file close expect(fileContents).not.to.contains('versionLong = \'1.0.0-'); + done(); }); }); @@ -64,7 +82,7 @@ describe('appversion', function() { it('should succeed with default settings', function(done) { repo.init(); fs.mkdirSync(path.join(repoDir, 'src')); - fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0"}'); + fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0", "name": "test", "description": "test description"}'); exec('node dist/index.js --root=' + repoDir, (err, stdout, stderr) => { if (err) { done('Test failed: Could not execute command.'); @@ -81,18 +99,77 @@ describe('appversion', function() { return; } const fileContents = fs.readFileSync(outputFile, 'utf8'); - expect(fileContents).to.contains('export const versions: TsAppVersion ='); + // interface well format test + expect(fileContents).to.contains('export interface TsAppVersion {'); // interface start + expect(fileContents).to.contains('version: string;'); + expect(fileContents).to.contains('name: string;'); + expect(fileContents).to.contains('description?: string;'); + expect(fileContents).to.contains('versionLong?: string;'); + expect(fileContents).to.contains('versionDate: string;'); + expect(fileContents).to.contains('gitCommitHash?: string;'); + expect(fileContents).to.contains('gitCommitDate?: string;'); + expect(fileContents).to.contains('gitTag?: string;'); + expect(fileContents).to.contains('};\nconst obj: TsAppVersion = {'); // interface end + obj start + + // data test expect(fileContents).to.contains('version: \'1.0.0\','); + expect(fileContents).to.contains('name: \'test\','); + expect(fileContents).to.contains('description: \'test description\','); expect(fileContents).to.contains('versionLong: \'1.0.0-'); + expect(fileContents).to.contains('};\nexport default obj;\n'); // export default obj + file close expect(fileContents).to.not.contains('versionLong: \'1.0.0-\''); + done(); }); }); + it('should succeed with default settings when no description is provided', function(done) { + repo.init(); + fs.mkdirSync(path.join(repoDir, 'src')); + fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0", "name": "test"}'); + exec('node dist/index.js --root=' + repoDir, (err, stdout, stderr) => { + if (err) { + done('Test failed: Could not execute command.'); + return; + } + if (stderr) { + done(stderr); + return; + } + expect(stdout).to.match(/Writing version module to/); + const outputFile = path.join(repoDir, 'src', '_versions.ts'); + if (!fs.existsSync(outputFile)) { + done('File ' + outputFile + ' not found.'); + return; + } + const fileContents = fs.readFileSync(outputFile, 'utf8'); + // interface well format test + expect(fileContents).to.contains('export interface TsAppVersion {'); // interface start + expect(fileContents).to.contains('version: string;'); + expect(fileContents).to.contains('name: string;'); + expect(fileContents).to.contains('description?: string;'); + expect(fileContents).to.contains('versionLong?: string;'); + expect(fileContents).to.contains('versionDate: string;'); + expect(fileContents).to.contains('gitCommitHash?: string;'); + expect(fileContents).to.contains('gitCommitDate?: string;'); + expect(fileContents).to.contains('gitTag?: string;'); + expect(fileContents).to.contains('};\nconst obj: TsAppVersion = {'); // interface end + obj start + + // data test + expect(fileContents).to.contains('version: \'1.0.0\','); + expect(fileContents).to.contains('name: \'test\','); + expect(fileContents).to.contains('versionLong: \'1.0.0-'); + expect(fileContents).to.not.contains('versionLong: \'1.0.0-\''); + expect(fileContents).to.contains('};\nexport default obj;\n'); // export default obj + file close + expect(fileContents).to.not.contains('description: \''); + + done(); + }); + }); it('should succeed with different file output', function(done) { repo.init(); - fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0"}'); + fs.writeFileSync(path.join(repoDir, 'package.json'), '{"version": "1.0.0", "name": "test", "description": "test description"}'); exec('node dist/index.js --root=' + repoDir + ' --file=version-test.ts', (err, stdout, stderr) => { if (err) { done('Test failed: Could not execute command.'); @@ -109,10 +186,26 @@ describe('appversion', function() { return; } const fileContents = fs.readFileSync(outputFile, 'utf8'); - expect(fileContents).to.contains('export const versions: TsAppVersion ='); + // interface well format test + expect(fileContents).to.contains('export interface TsAppVersion {'); // interface start + expect(fileContents).to.contains('version: string;'); + expect(fileContents).to.contains('name: string;'); + expect(fileContents).to.contains('description?: string;'); + expect(fileContents).to.contains('versionLong?: string;'); + expect(fileContents).to.contains('versionDate: string;'); + expect(fileContents).to.contains('gitCommitHash?: string;'); + expect(fileContents).to.contains('gitCommitDate?: string;'); + expect(fileContents).to.contains('gitTag?: string;'); + expect(fileContents).to.contains('};\nconst obj: TsAppVersion = {'); // interface end + obj start + + // data test expect(fileContents).to.contains('version: \'1.0.0\','); + expect(fileContents).to.contains('name: \'test\','); + expect(fileContents).to.contains('description: \'test description\','); expect(fileContents).to.contains('versionLong: \'1.0.0-'); + expect(fileContents).to.contains('};\nexport default obj;\n'); // export default obj + file close expect(fileContents).to.not.contains('versionLong: \'1.0.0-\''); + done(); }); }); @@ -122,7 +215,7 @@ describe('appversion', function() { const applicationDir = path.join(repoDir, 'application'); fs.mkdirSync(applicationDir); fs.mkdirSync(path.join(applicationDir, 'src')); - fs.writeFileSync(path.join(applicationDir, 'package.json'), '{"version": "1.0.0"}'); + fs.writeFileSync(path.join(applicationDir, 'package.json'), '{"version": "1.0.0", "name": "test", "description": "test description"}'); exec('node dist/index.js --root=' + applicationDir + ' --git=..', (err, stdout, stderr) => { if (err) { done('Test failed: Could not execute command.'); @@ -138,10 +231,27 @@ describe('appversion', function() { return; } const fileContents = fs.readFileSync(outputFile, 'utf8'); - expect(fileContents).to.contains('export const versions: TsAppVersion ='); + // interface well format test + expect(fileContents).to.contains('export interface TsAppVersion {'); // interface start + expect(fileContents).to.contains('version: string;'); + expect(fileContents).to.contains('name: string;'); + expect(fileContents).to.contains('description?: string;'); + expect(fileContents).to.contains('versionLong?: string;'); + expect(fileContents).to.contains('versionDate: string;'); + expect(fileContents).to.contains('gitCommitHash?: string;'); + expect(fileContents).to.contains('gitCommitDate?: string;'); + expect(fileContents).to.contains('gitTag?: string;'); + expect(fileContents).to.contains('};\nconst obj: TsAppVersion = {'); // interface end + obj start + + // data test expect(fileContents).to.contains('version: \'1.0.0\''); + expect(fileContents).to.contains('name: \'test\''); + expect(fileContents).to.contains('description: \'test description\''); expect(fileContents).to.contains('versionLong: \'1.0.0-'); + expect(fileContents).to.contains('};\nexport default obj;\n'); // export default obj + file close + expect(fileContents).to.not.contains('versionLong: \'1.0.0-\''); + done(); }); });