在构建期间通过挂钩编辑值 [英] Editing values during build via hooks

查看:71
本文介绍了在构建期间通过挂钩编辑值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建应用程序时尝试编辑版本变量,但我根本无法使用cordova钩子运行任何脚本。

Im trying to edit a version variable when I build my applications, but I can't get any scripts to run using the cordova hooks at all.

我想要从package.json获取版本和git commit的最后5位数,所以我可以使用1.0.0.89gkt作为我的版本。

I want to get the version from the package.json and the last 5 digits of the git commit so i can have something like 1.0.0.89gkt as my versions.

过去对于离子1和使用grunt或gulp,我能够轻松地将脚本添加到构建过程中。

In the past for ionic 1 and using grunt or gulp I was able to add the scripts into the build process easily.

我尝试了使用hooks / hook_name /的旧方法脚本格式并使用config.xml中的钩子标记,但对我来说都不起作用。

I've tried both the old way using the hooks/hook_name/script format and using the hook tag in the config.xml and neither work for me.

通过package.json覆盖离子脚本允许我更改脚本自动运行,但如果可以,我想避免这种情况。虽然我可以轻松地将我的文本替换为其中一个复制脚本或其他东西(生病需要找出哪一个最好)

Overwriting the ionic scripts via the package.json allows me to change the scripts that are automatically run, but I want to avoid that if I can. Though I can easily add my text replace to one of the copy scripts or something (ill need to work out which one is best)

如果有人知道更好的方法或者为什么钩子不会开火,请告诉我。

If someone knows a better way or a reason as to why the hooks wont fire, please let me know.

推荐答案

这是我最终使用的解决方案。

This is the solution that I ended up using.

scripts / before_prepare_increment_build_number.js

scripts/before_prepare_increment_build_number.js

var fs = require('fs');
var git = require('git-rev-sync')

console.log('Incrementing Build Number');


var file = fs.readFileSync('www/build/main.js', 'utf8');

var str = git.short();

console.log('short', str)

var result = file.replace(/{{GITVERSIONSTRING}}/g, str);

fs.writeFileSync('www/build/main.js', result);

console.log('Incrementing Build Number Completed');

config.xml

config.xml

< hook src =scripts / before_prepare_increment_build_number.jstype =before_prepare/>

我需要确保一切都是同步发生的,否则内置脚本会在替换字符串之前开始复制。

I needed to ensure that everything was happening synchronously otherwise the built in scripts would start copying before the strings had been replaced.

目前它的目标是由默认离子生成的整个main.js-应用程序脚本,因此可以根据需要添加所有比较和替换。
此解决方案使用内置的cordova钩子 before_prepare

Currently its targeting the whole main.js that is generated by the default ionic-app-scripts so all comparisons and replacements can be added as required. This solution uses the built in cordova hook before_prepare

另一种可用于制作它的解决方案更高效的是根据需要定位单个文件,并在package.json中的build / serve脚本之前添加脚本,并使用npm控制并管理它。

Another solution that can be used to make it a bit more efficient is targeting the individual files as required and adding the script before the build/serve scripts in the package.json and have npm control and manage it.

这篇关于在构建期间通过挂钩编辑值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆