-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-helpers.cjs
More file actions
57 lines (48 loc) · 1.44 KB
/
build-helpers.cjs
File metadata and controls
57 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Cross-platform build helper script
const fs = require('node:fs');
const path = require('node:path');
const { execSync } = require('node:child_process');
// Create directory recursively (cross-platform equivalent of mkdir -p)
function mkdirp(dirPath) {
const absolutePath = path.resolve(dirPath);
if (!fs.existsSync(absolutePath)) {
fs.mkdirSync(absolutePath, { recursive: true });
}
}
// Run a git command, returning a fallback string if git is unavailable
function tryGit(args, fallback = 'unknown') {
try {
return execSync(`git ${args}`, { stdio: ['pipe', 'pipe', 'pipe'] })
.toString()
.trim();
} catch {
return fallback;
}
}
// Generate version info
function generateVersion() {
mkdirp('dist');
const sha = tryGit('rev-parse HEAD');
const tag = tryGit('describe --tags --always');
const branch = tryGit('rev-parse --abbrev-ref HEAD');
const version = process.env.npm_package_version;
const versionInfo = {
sha,
tag,
branch,
version
};
fs.writeFileSync('dist/esm/version.json', JSON.stringify(versionInfo, null, 2));
fs.writeFileSync('dist/commonjs/version.json', JSON.stringify(versionInfo, null, 2));
console.log('Generated version.json:', versionInfo);
}
// Process command line arguments
const command = process.argv[2];
switch (command) {
case 'generate-version':
generateVersion();
break;
default:
console.error('Unknown command:', command);
process.exit(1);
}