Skip to content

Commit

Permalink
feat: export 'name' and 'description' fields from package.json file
Browse files Browse the repository at this point in the history
- update dist/index.js
- update tests. Manage descrption as optional
- update readme with new fields descrtiption
  • Loading branch information
dzoccarato authored and saitho committed Mar 30, 2020
1 parent fd11f1f commit 5f6d793
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 23 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,6 +133,7 @@ if (enableGit) {
}

src += `};
export default obj;
`;

console.log('[TsAppVersion] ' + colors.green('Writing version module to ') + colors.yellow(versionFile));
Expand Down
9 changes: 3 additions & 6 deletions example/src/app/app.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -97,7 +97,7 @@ svg.material-icons:not(:last-child) {
}

.card.card-small {
height: 16px;
height: 180px;
width: 168px;
}

Expand All @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion example/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@
</g>
</svg>

<span>This app is running with version {{ version }}</span>
<span>This app is running with version {{ version }} (from environment) / <strong>{{ tsAppVersion.version }}</strong> (from tsAppVersion direclty) and has following information:</span>
<ul>
<li>name: <strong>{{ tsAppVersion.name }}</strong> </li>
<li>versionDate: <strong>{{ tsAppVersion.versionDate }}</strong> </li>
<li>gitCommitHash: <strong>{{ tsAppVersion.gitCommitHash }}</strong> </li>
<li>versionLong: <strong>{{ tsAppVersion.versionLong }}</strong> </li>
<li>gitTag: <strong>{{ tsAppVersion.gitTag }}</strong> </li>
</ul>

<svg id="rocket-smoke" alt="Rocket Ship Smoke" xmlns="http://www.w3.org/2000/svg" width="516.119" height="1083.632" viewBox="0 0 516.119 1083.632">
<path id="Path_40" data-name="Path 40" d="M644.6,141S143.02,215.537,147.049,870.207s342.774,201.755,342.774,201.755S404.659,847.213,388.815,762.2c-27.116-145.51-11.551-384.124,271.9-609.1C671.15,139.365,644.6,141,644.6,141Z" transform="translate(-147.025 -140.939)" fill="#f5f5f5"/>
Expand All @@ -61,7 +68,9 @@
<ul>
<li><strong>no context:</strong> The displayed version should be the <i>latest Git tag</i> from @saithodev/ts-appversion project (e.g. v1.3.0)</li>
<li><strong>production:</strong> The displayed version should be <i>1.0.0</i> (from package.json)</li>
<li><strong>production:</strong> The displayed name should be <i>sample-app</i> (from package.json)</li>
<li><strong>development:</strong> The displayed version should be <i>1.0.0</i> (from package.json) with the latest <i>hash</i> of the@saithodev/ts-appversion project (e.g. 1.0.0-gda1e67f)</li>
<li><strong>development:</strong> The displayed name should be <i>sample-app</i> (from package.json)</li>
</ul>
</div>

Expand Down
22 changes: 19 additions & 3 deletions example/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
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',
templateUrl: './app.component.html',
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;
}
}
124 changes: 117 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -55,16 +55,34 @@ 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();
});
});

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.');
Expand All @@ -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.');
Expand All @@ -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();
});
});
Expand All @@ -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.');
Expand All @@ -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();
});
});
Expand Down

0 comments on commit 5f6d793

Please sign in to comment.