Angular CLI 在构建之前修改 TS 文件?
Angular CLI to modify a TS file before building?
我正在使用 Angular 6 和 Angular CLI 通过 AOT 进行构建。 SPA 有一个关于对话框来告诉当前前端版本,基本上是 YYMMDD:mm。我将版本存储在 environments/environment.ts:
export const AppConfigConstants: AppConfigConstantsType = {
version: '180709.13',
};
目前在运行之前
ng build --configuration=production
我必须手动修改版本。如果能自动更新版本号就更好了
AngularCLI有没有修改ts文件的好功能?或者您有其他解决方案?
您可以向 npm build
添加一个脚本,在 运行 ng build
:
之前递增您的版本号
版本号可以存储在一个 JSON 文件中,您可以在您的应用程序中读取该文件:
src/version.json
{
"v": 0
}
增量-version.js
// read version.json from file system, increment the version by 1
const fs = require("fs");
fs.readFile("src/version.json", (err, content) => {
const versionObject = JSON.parse(content.toString());
versionObject.v ++;
fs.writeFile("src/version.json", JSON.stringify(versionObject), () => {});
});
package.json
"scripts": {
...
"build": "node increment-version.js && ng build"
}
angular.json
"assets": [
...
"src/version.json"
]
typings.d.ts
....
declare module "*.json" {
const value: any;
export default value;
}
关于-dialog.ts
import * as version from '../version.json';
const versionNumber = version.v;
我一般调用ng build的批处理文件。
批处理文件是这样的:
cd %~dp0NGSource
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%%MyDate:~4,2%%MyDate:~6,2%%time:~0,2%%time:~3,2%
@echo const BUILD_TIME={buildTime: %today%} > src\conf\buildTime.js
ng build --configuration=production
而buildTime.js包含在angular.json的脚本节点中,所以它被捆绑在scripts.xxxxx.js中。我的 TS 代码通过这样的声明访问这个文件:
declare const BUILD_TIME: {
buildTime?: string;
}
interface AppConfigConstantsType {
version: string;
buildTime?: string;
}
export const AppConfigConstants: AppConfigConstantsType = {
version: '2.3',
...(typeof BUILD_TIME === 'undefined' ? { buildTime: 'Unknown' } : BUILD_TIME),
};
最后,我决定将版本号和内部版本号分开。如果你想把它们合并成一个字符串,你应该不难调整上面的代码。
我正在使用 Angular 6 和 Angular CLI 通过 AOT 进行构建。 SPA 有一个关于对话框来告诉当前前端版本,基本上是 YYMMDD:mm。我将版本存储在 environments/environment.ts:
export const AppConfigConstants: AppConfigConstantsType = {
version: '180709.13',
};
目前在运行之前
ng build --configuration=production
我必须手动修改版本。如果能自动更新版本号就更好了
AngularCLI有没有修改ts文件的好功能?或者您有其他解决方案?
您可以向 npm build
添加一个脚本,在 运行 ng build
:
版本号可以存储在一个 JSON 文件中,您可以在您的应用程序中读取该文件:
src/version.json
{
"v": 0
}
增量-version.js
// read version.json from file system, increment the version by 1
const fs = require("fs");
fs.readFile("src/version.json", (err, content) => {
const versionObject = JSON.parse(content.toString());
versionObject.v ++;
fs.writeFile("src/version.json", JSON.stringify(versionObject), () => {});
});
package.json
"scripts": {
...
"build": "node increment-version.js && ng build"
}
angular.json
"assets": [
...
"src/version.json"
]
typings.d.ts
....
declare module "*.json" {
const value: any;
export default value;
}
关于-dialog.ts
import * as version from '../version.json';
const versionNumber = version.v;
我一般调用ng build的批处理文件。
批处理文件是这样的:
cd %~dp0NGSource
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%%MyDate:~4,2%%MyDate:~6,2%%time:~0,2%%time:~3,2%
@echo const BUILD_TIME={buildTime: %today%} > src\conf\buildTime.js
ng build --configuration=production
而buildTime.js包含在angular.json的脚本节点中,所以它被捆绑在scripts.xxxxx.js中。我的 TS 代码通过这样的声明访问这个文件:
declare const BUILD_TIME: {
buildTime?: string;
}
interface AppConfigConstantsType {
version: string;
buildTime?: string;
}
export const AppConfigConstants: AppConfigConstantsType = {
version: '2.3',
...(typeof BUILD_TIME === 'undefined' ? { buildTime: 'Unknown' } : BUILD_TIME),
};
最后,我决定将版本号和内部版本号分开。如果你想把它们合并成一个字符串,你应该不难调整上面的代码。