如何增加Ionic 3项目的版本号?

时间:2019-12-16 11:03:38

标签: ionic-framework

是否有一种快速简便的方法来增加Ionic 3项目的版本号,而不必手动编辑出现版本号的每个文件?

我正在考虑config.xmlpackage.jsonsrc/index.html以及项目中出现版本号的其他所有地方。

说我要对代码进行一些更新后,将版本号从2.3.4增加到2.3.5。我想拥有类似的东西

$ ionic run version-increment [major][middle][minor]

可以一次编辑所有文件。

是否有插件或魔术可以帮我呢?

2 个答案:

答案 0 :(得分:1)

理想情况下,您应该将版本更新与构建过程联系在一起。这样,当您启动构建过程时,您会自动增加应用程序的版本。

您可以在Ionic 4中以这种方式完成操作

步骤1:更新ionic.config.json,使其具有预构建挂钩:

{
  "name": "morphistic-app",
  "type": "angular",
  "id": "b8d9b232",
  "integrations": {
    "cordova": {}
  },
  "hooks": {
    "build:before": "./config/build-before.js"
  }
}

步骤2:创建build-before.js并添加以下内容:

const fs = require('fs');

module.exports = (ctx) => {

    if (ctx.build && ctx.build.configuration && ctx.build.configuration === "production") {

        console.log("production build: performing version bump...");

        // update package.json:
        let packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf-8').toString());
        let versionArray = packageJSON.version.split(".");
        versionArray[2] = (parseInt(versionArray[2])+1).toString();
        packageJSON.version = versionArray.join(".");
        fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, "\t"), 'utf-8');
        console.log("package.json app version updated");

        let prodEnvData = fs.readFileSync(`src/environments/environment.prod.ts`, 'utf-8');
        prodEnvData = prodEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
        fs.writeFileSync('src/environments/environment.prod.ts', prodEnvData, 'utf-8');
        console.log("environments.prod.ts app version updated");

        let devEnvData = fs.readFileSync(`src/environments/environment.dev.ts`, 'utf-8');
        devEnvData = devEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
        fs.writeFileSync('src/environments/environment.dev.ts', devEnvData, 'utf-8');
        console.log("environments.dev.ts app version updated");

        let localEnvData = fs.readFileSync(`src/environments/environment.local.ts`, 'utf-8');
        localEnvData = localEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
        fs.writeFileSync('src/environments/environment.local.ts', localEnvData, 'utf-8');
        console.log("environments.local.ts app version updated");

        let defaultEnvData = fs.readFileSync(`src/environments/environment.ts`, 'utf-8');
        defaultEnvData = defaultEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
        fs.writeFileSync('src/environments/environment.ts', defaultEnvData, 'utf-8');
        console.log("environments.ts app version updated");

        let configXmlData = fs.readFileSync('config.xml', 'utf-8');
        configXmlData = configXmlData.replace(/id="com.yourapp.ionic" version=".*"/, `id="com.yourapp.ionic" version="${packageJSON.version}"`);
        fs.writeFileSync('config.xml', configXmlData,'utf-8');
        console.log("config.xml app version updated");

    };

};

上面的代码还处理环境文件更改,如果不是您的目标,则可以跳过。

如果您需要更新任何html文件-请参见上面的config.xml示例

上面的代码还假定您的版本是与semver兼容的架构,也称为“ n.n.n”

或者在离子3中,您可以执行以下操作:

步骤1:更新package.json中的'scripts'属性,使其指向您已构建的步骤脚本:

"scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve --non-interactive",
        "ionic:serve:before": "cross-env MY_ENV='dev' node ./config/env.config.js",
        "ionic:build:before": "cross-env MY_ENV='prod' node ./config/env.config.js",
    }

请注意,我正在使用cross-env library来确保环境变量在以下脚本的上下文中可用:

步骤2:创建env.config.js文件:

var fs = require('fs');
function readWriteSync() {
    let env = process.env.MY_ENV || 'dev';
    console.log("updating configuration for: ", env);
    // read env data:
    let envData = fs.readFileSync(`config/environment.${env}.ts`, 'utf-8');
    // read data from package.json:
    let packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf-8').toString());
    let version;
    if (env === "prod") {
        let versionArray = packageJSON.version.split(".");
        versionArray[2] = (parseInt(versionArray[2])+1).toString();
        packageJSON.version = versionArray.join(".");
        // write to package.json:
        fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, "\t"), 'utf-8');
    };
    version = packageJSON.version;
    // replace env data:
    envData = envData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${version}"`);
    // write env data
    fs.writeFileSync('src/environments/environment.ts', envData, 'utf-8');
    // update config.xml:
    let configXmlData = fs.readFileSync('config.xml', 'utf-8');
    configXmlData = configXmlData.replace(/id="com.yourapp.ionic" version=".*"/, `id="com.yourapp.ionic" version="${version}"`);
    fs.writeFileSync('config.xml', configXmlData,'utf-8');

};
readWriteSync();

答案 1 :(得分:0)

我有一个大型的Ionic / Cordova项目,而您真正需要版本号的唯一地方是config.xml。您应该让您的App仅在该号码上使用该号码,然后手动更改并不重要。