firasKoubaa
firasKoubaa

Reputation: 6867

Angular / node.js how to copy the app version from package.json to anothe non json file

Within my Angular app , and while building it , i ve to catch the app version , which is inside my package.json file and copy it to another file which would be not a json file.

My package.json file :

{
  "name": "my app",
  "author": "ME",
  "version": "0.1.3",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --public-host --port 4222 http://localhost:4222/",
    "build": "ng build",
    "sync-version": "sync-json -v --property version --source package.json projects/cockpit-tools/package.json && sync-json -v --property version --source package.json projects/cockpit-tools-demo/src/assets/configuration/properties.json",
    "sync-custom": "shx  node -p -e \"require('./package.json').version\"",
    "copy-version":"MY_COMMAND"
...
}

My target file is properties file whih looks like this , and which is located in same path of package.json (this file is used in another level of automation , that's why i can't change it)

app-infos.properties

project.version=0.0.0
project.author=ME

My purpose is to write a command line task script which i may run to synchronize / copy the version from package.json to the project.version property inside my target file.

I ve tried an npm library called sync-version , but that works only when the target is a json file

I ve tried also to do it with shell command line within the shx package. also that didn't work as expected.

i ve also tried some node.js tricks (require ...) but the problem persists

i need to find a way to do it with the simpliest way and not be obliged to install some linux tools or similar(jq or other) , since this would run in some CI CD contexts , so that would rather to be as native and as generic as possible.

Suggestions ?

Upvotes: 0

Views: 329

Answers (1)

Tal B
Tal B

Reputation: 1

A classic DevOps situation :)

CI/CD usually happens on linux envs. You will probably have plain shell/bash. Try extractinf the version to a variable:

myVersion=`grep 'version:' package.json | awk '{print $2}'`

then replacing it in the apps-info:

sed -i "s/0.0.0/$myVersion/" apps-info.properties

We can make ot more generic by always pulling the last version instead of 0.0.0, but i think you should have a place holder like 0.0.0

Upvotes: 0

Related Questions