Sharkey/scripts/build-pre.js
dakkar 1b213dece8 better git version info
My previous patch to embed git version info in nodeinfo had some
issues, like it produced `2024.4.4-249-g04d45859e6` when building on
current `develop`, which is arguably correct but not helpful (it
should really say `2025.5.2-dev` somewhere!)

* if we're on a tag, trust `package.json`
* if we're not, *append* the current commit hash

so for example, building on `stable` right now gets you `2025.4.4`,
and building on the parent of this commit gets you
`2025.5.2-dev+g04d45859e6`

the `+` is correct according to https://semver.org/
2025-08-02 11:07:57 +01:00

53 lines
1.3 KiB
JavaScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
const fs = require('fs');
const packageJsonPath = __dirname + '/../package.json'
const { execFileSync } = require('node:child_process');
function callGit(args) {
return execFileSync('git', args, {
encoding: 'utf-8',
}).trim();
}
function getGitVersion(versionFromPackageJson) {
const thisTag = callGit(['tag', '--points-at', 'HEAD']);
if (thisTag) {
// we're building from a tag, we don't care about extra details
return null;
}
const commitId = callGit(['rev-parse', '--short', 'HEAD']);
return `${versionFromPackageJson}+g${commitId}`;
}
function build() {
try {
const json = fs.readFileSync(packageJsonPath, 'utf-8')
const meta = JSON.parse(json);
let gitVersion;
try {
gitVersion = getGitVersion(meta.version);
} catch (e) {
console.warn("couldn't get git commit details, ignoring",e);
}
fs.mkdirSync(__dirname + '/../built', { recursive: true });
fs.writeFileSync(__dirname + '/../built/meta.json', JSON.stringify({ version: meta.version, gitVersion }), 'utf-8');
} catch (e) {
console.error(e)
}
}
build();
if (process.argv.includes("--watch")) {
fs.watch(packageJsonPath, (event, filename) => {
console.log(`update ${filename} ...`)
build()
})
}