Para começar, você deve instalar as bibliotecas:
npm i --save-dev --save-exact auto-changelog @jsdevtools/version-bump-prompt @jsdevtools/ez-spawn capacitor-set-version cross-env
Dentro do package.json
, você precisa criar o script chamado release
, da seguinte forma:
"release": "cross-env HUSKY=0 node scripts/release.js",
Depois, crie o arquivo .versionrc
na raiz com o conteúdo:
{
"files": [
"package.json",
"package-lock.json",
"src/environments/environment.ts",
"src/environments/environment.hmr.ts",
"src/environments/environment.prod.ts"
],
"versionCode": 1
}
Dentro de files
, você coloca cada arquivo que você quer que ele atualize a versão.
A propriedade chamada versionCode
é utilizada quase que exclusivamente para o Capacitor,
para que nós possamos trackear a versão atual em inteiro para os apps.
Depois, precisamos criar uma pasta chamada scripts
e criar o arquivo chamado release.js
:
const BumpVersion =
require('capacitor-set-version/dist/commands/set/index').default;
const { readFileSync, writeFileSync, existsSync } = require('fs');
const { resolve } = require('path');
const { run } = require('auto-changelog/src/run');
const { versionBump } = require('@jsdevtools/version-bump-prompt');
const ezSpawn = require('@jsdevtools/ez-spawn');
const basePath = resolve(__dirname, '..');
const packageJson = resolve(basePath, 'package.json');
const versionFilePath = resolve(basePath, '.versionrc');
const versionFile = JSON.parse(
readFileSync(versionFilePath).toString('utf-8'),
);
let newVersion;
async function bumpVersion() {
const result = await versionBump({
preid: 'build',
noVerify: true,
commit: 'chore(release): bump version to %s',
tag: true,
push: false,
files: versionFile.files,
});
newVersion = result.newVersion;
}
async function bumpCapacitorVersion() {
try {
if (!existsSync(resolve(basePath, 'android')) && !existsSync(resolve(basePath, 'ios')))
return;
const versionName = JSON.parse(
readFileSync(packageJson).toString('utf-8'),
).version;
versionFile.versionCode = Number(versionFile.versionCode || 0) + 1;
writeFileSync(versionFilePath, JSON.stringify(versionFile, null, 2), {
encoding: 'utf-8',
});
await BumpVersion.run([
basePath,
'-v',
versionName,
'-b',
versionFile.versionCode.toString(),
]);
} catch (e) {
console.error(e);
}
}
async function generateChangelog() {
await run(['-l', '1000', '-t', 'keepachangelog']);
}
bumpVersion().then(bumpCapacitorVersion).then(generateChangelog).then(async () => {
const androidFiles = existsSync(resolve(basePath, 'android')) ? ['android'] : [];
const iosFiles = existsSync(resolve(basePath, 'ios')) ? ['ios'] : [];
const filesToCommit = ['.versionrc', 'CHANGELOG.md', ...androidFiles, ...iosFiles];
for (const file of filesToCommit)
await ezSpawn.async('git', ['add', file]);
await ezSpawn.async('git', ['commit', '--amend', '--no-verify', '--no-edit']);
await ezSpawn.async('git', ['tag', '-d', `v${newVersion}`]);
await ezSpawn.async('git', ['tag', `v${newVersion}`]);
});
E para finalizar, precisamos criar o arquivo chamado conventional.hbs
que irá gerar o CHANGELOG.md:
Portanto, a estrutura final será:
.versionrc
- scripts
release.js
conventional.hbs