要求:编写脚本实现更新package.json以及manifest.webapp中的版本号,要求不传参数时版本修订号(第三位)加1,传参时以传入的参数为当前版本号。修改版本号后自动commit并打上tag。
1
package.json 加入 "checkVersion": "sh scripts/version.sh",
2
version.sh:
if [ "$1" ];
then
npm --no-git-tag-version version $1
VERSION=$1
else
npm --no-git-tag-version version patch
VERSION=$(node -p "require('./package.json').version")
fi
node scripts/weappversion.js $VERSION --replace public/manifest.webapp
git add .
git commit -am "update version v$VERSION"
git tag v$VERSION
echo "\nVersion v$VERSION update succeeded!\n"
weappversion.js :
const fs = require('fs');
const program = require('commander');
program.version('1.0.0').option('-R, --replace <string>', 'folder to replace').parse(process.argv);
const options = program.opts();
const r = options.replace;
if (!r) {
// eslint-disable-next-line no-console
console.log('need replace file');
process.exit(1);
}
const arg = process.argv.splice(2);
const c = fs.readFileSync(r);
const json = JSON.parse(c);
if (arg[0] !== '--replace') {
json.version = arg[0];
fs.writeFileSync(r, JSON.stringify(json, null, 2));
} else {
const versionParts = json.version.split('.').map((str) => parseInt(str, 10));
versionParts[versionParts.length - 1]++;
json.version = versionParts.map((n) => `${n}`).join('.');
fs.writeFileSync(r, JSON.stringify(json, null, 2));
}
3. 使用:
yarn checkVersion
//不传参,版本号加1 1.0.0 ----》 1.0.1yarn checkVersion 2.2.2
//传参 ,版本号 1.0.0 -----》2.2.2
之前用js 写过一个,但是 tag 的时候不能正确打在当前commit上,很疑惑,不知道为啥,其他功能OK,贴上代码,大家有兴趣看看,我就是一个小白😄,还很多不懂,写的不好的地方,还请大家多多指教》》》
const inquirer = require('inquirer');
const chalk = require('chalk');
const { exec } = require('child_process');
const { name: projectName, version: versionCurrent } = require('../package.json');
const fs = require('fs');
const r1 = 'build/manifest.webapp';
const r2 = 'public/manifest.webapp';
const regVersion = /^[1-9]{1}\d*\.\d+\.\d+$/;
console.log('\n');
let versionNew;
inquirer
.prompt([
{
type: 'input',
name: 'version',
message: `请确认 ${projectName}/package.json/version 版本号(当前:v${versionCurrent}):\n`,
default: versionCurrent,
validate(version) {
if (!regVersion.test(version)) {
return false;
}
return true;
},
},
])
.then(({ version }) => {
const c = fs.readFileSync(r1);
const json = JSON.parse(c);
if (version !== versionCurrent) {
versionNew = version;
} else {
const versionParts = versionCurrent.split('.').map((str) => parseInt(str, 10));
versionParts[versionParts.length - 1]++;
versionNew = versionParts.map((n) => `${n}`).join('.');
}
command(`npm --no-git-tag-version version ${versionNew}`, {}, (error, stdout, stderr) => {
if (!error) {
json.version = versionNew;
fs.writeFileSync(r1, JSON.stringify(json, null, 2));
fs.writeFileSync(r2, JSON.stringify(json, null, 2));
console.log(
chalk.green(
`\n${projectName} 版本号(项目根目录下的 package.json/version)更新成功,version: v${versionNew} !`,
),
);
command(`git commit -am '更新项目版本号为: v${versionNew}' && git tag v${versionNew}`);
console.log(`\n`);
process.exit(0);
} else {
console.log(chalk.yellow(`\n更新版本号(v${versionNew})失败了~\n`));
process.exit(1);
}
});
});
function command(cmd, options, callback) {
console.log('\n');
console.log(chalk.cyan(cmd.toString()));
return exec(cmd, { ...options }, callback);
}
这个是参考segmentfault.com/a/119000002…
如果你只需要升修订号(patch) 那么 可以直接以下命令:
"postpublish": "npm --no-git-tag-version version patch && PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git commit -am $PACKAGE_VERSION && git tag $PACKAGE_VERSION"