简体   繁体   English

Angular / node.js 如何将应用程序版本从 package.json 复制到另一个非 json 文件

[英]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.在我的 Angular 应用程序中,在构建它时,我必须捕获应用程序版本,它位于我的package.json文件中,并将其复制到另一个不是 json 文件的文件。

My package.json file:我的package.json文件:

{
  "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)我的目标文件是属性文件,看起来像这样,它位于 package.json 的相同路径中(此文件用于另一个自动化级别,这就是我无法更改它的原因)

app-infos.properties应用信息.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.我的目的是编写一个命令行任务脚本,我可以运行它来同步/复制版本从package.json到我的目标文件中的project.version属性。

I ve tried an npm library called sync-version , but that works only when the target is a json file我已经尝试了一个名为sync-version的 npm 库,但只有当目标是 json 文件时才有效

I ve tried also to do it with shell command line within the shx package. also that didn't work as expected.我也尝试过在shx package 中使用 shell 命令行来执行此操作。但也没有按预期工作。

i ve also tried some node.js tricks (require...) but the problem persists我也尝试了一些 node.js 技巧(需要...),但问题仍然存在

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.我需要找到一种以最简单的方式来完成它的方法,而不是必须安装一些 linux 工具或类似工具(jq 或其他),因为这会在某些 CI CD 上下文中运行,所以宁愿像本机一样尽可能通用。

Suggestions?建议?

A classic DevOps situation:)一个经典的 DevOps 场景:)

CI/CD usually happens on linux envs. CI/CD 通常发生在 linux 环境中。 You will probably have plain shell/bash.您可能会有普通的 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我们可以通过始终提取最新版本而不是 0.0.0 来使 ot 更通用,但我认为你应该有一个像 0.0.0 这样的占位符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM