Mangane/app/soapbox/utils/code.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-03-30 19:43:45 +02:00
// @preval
const { execSync } = require('child_process');
2022-01-10 23:01:24 +01:00
const pkg = require('../../../package.json');
2021-03-30 19:43:45 +02:00
const shortRepoName = url => new URL(url).pathname.substring(1);
const trimHash = hash => hash.substring(0, 7);
2021-03-30 19:43:45 +02:00
const version = pkg => {
// Try to discern from GitLab CI first
const { CI_COMMIT_TAG, CI_COMMIT_REF_NAME, CI_COMMIT_SHA } = process.env;
if (CI_COMMIT_TAG === `v${pkg.version}` || CI_COMMIT_REF_NAME === 'stable') {
return pkg.version;
}
if (typeof CI_COMMIT_SHA === 'string') {
return `${pkg.version}-${trimHash(CI_COMMIT_SHA)}`;
}
// Fall back to git directly
try {
const head = String(execSync('git rev-parse HEAD'));
const tag = String(execSync(`git rev-parse v${pkg.version}`));
if (head !== tag) return `${pkg.version}-${trimHash(head)}`;
} catch (e) {
// Continue
}
// Fall back to version in package.json
return pkg.version;
};
2021-03-30 19:43:45 +02:00
module.exports = {
name: pkg.name,
2021-08-22 05:46:33 +02:00
displayName: pkg.displayName,
2021-03-30 19:43:45 +02:00
url: pkg.repository.url,
repository: shortRepoName(pkg.repository.url),
version: version(pkg),
homepage: pkg.homepage,
2021-03-30 19:43:45 +02:00
};