Coder Social home page Coder Social logo

sharmanikhil04 / ts-appversion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from saitho/ts-appversion

0.0 1.0 0.0 825 KB

Display the version of your app in your frontend. For Angular and any other TypeScript framework.

Home Page: https://www.npmjs.com/package/@saithodev/ts-appversion

JavaScript 48.75% TypeScript 19.45% CSS 15.74% HTML 16.07%

ts-appversion's Introduction

TS-AppVersion

@sharmanikhil04/ts-appversion

Why this repo?

This repo is a fork of https://github.com/saitho/ts-appversion.

It was forked to update dependencies and keep this upto date, especially prevent security issues due to dependencies of this repository.

Build Status Quality Gate Status npm version npm license Known Vulnerabilities Dependency Status semantic-release Commitizen friendly FOSSA Status

This package extracts version information from your package.json and Git (if configured) and saves it into a TypeScript file. You can then access that TypeScript file from your application and display the version in your app.

The examples below illustrate the usage of this package for the Angular framework. However it should work similarly for any other JavaScript framework that is using TypeScript.

Getting started

The package comes with a script that has to be run before your application is built. You might want to use prestart and prebuild inside your package.json for that:

{
  scripts: [
    "prestart": "ts-appversion",
    "start": "ng serve",
    "prebuild": "ts-appversion",
    "build": "ng build",
  ]
}

With that setup the file is updated when npm start and npm build are run. Note: You won't be able to run ng build anymore as the script will not be executed. Use npm build instead.

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/_versions.ts
--git relative location of the folder containing the .git folder (based on the root directory) .
--pnpm PNPM has a different folder structure, resulting in a different root level. Add this if you use PNPM to install your dependencies. If package.json is not found at the expected PNPM path, it will fall back to the default one. This setting is ignored if --root is an absolute path. false
--set-version Set this to override the value of the version string fetched from package.json (set in version property)

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, if you want use just versions information, like in environment.ts example file:

import versions from '../_versions';

or you can import also TsAppVersion and use directly in your template, like in app.component.ts example file

import { TsAppVersion, 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;
  }
}

The file will export an object with following variables:

  • version is the version from packages.json (or value of set-version option if set)
  • 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
  • gitCommitHash is the short hash of the last commit
  • gitCommitDate is the timestamp in ISO format of the last commit

Note: The variables starting with "git" and the variable "versionLong" will only be available for Git repositories.

Environment-related versions

In some cases it might be better to not display the version number or only the short notation. You can use the environments to display different version informations.

In the following example:

  • the dev environment will display the version timestamp
  • the staging environemnt will diplay the long version (with the Commit hash)
  • the production environment will display the simple notation

environments/environment.ts

import versions from '../_versions';
export const environment = {
  production: false,
  version: versions.versionDate,
};

environments/environment.staging.ts

import versions from '../_versions';
export const environment = {
  production: false,
  version: versions.versionLong,
};

environments/environment.prod.ts

import versions from '../_versions';
export const environment = {
  production: true,
  version: versions.version,
};

From there you can access the version inside the Component which should display the version, e.g.:

import { Component } from '@angular/core';
import { environment } from '../environments/environment';
@Component({
  selector: 'app-root',
  template: '{{title}} {{version}}'
})
export class AppComponent {
  title = 'app';
  version = environment.version ? 'v' + environment.version : '';
}

Check out the example/ directory for a working example Angular application.

License

FOSSA Status

ts-appversion's People

Contributors

saitho avatar dzoccarato avatar sharmanikhil04 avatar cdelgadob avatar snyk-bot avatar dellermann avatar dependabot[bot] avatar fossabot avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.