-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathset-version.js
More file actions
74 lines (64 loc) · 2.42 KB
/
set-version.js
File metadata and controls
74 lines (64 loc) · 2.42 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
const fs = require('node:fs');
const path = require('node:path');
const rootDir = path.join(__dirname, '..');
const packageJsonPath = path.join(rootDir, 'package.json');
const workspaceServerPackageJsonPath = path.join(rootDir, 'workspace-server', 'package.json');
const workspaceServerIndexPath = path.join(rootDir, 'workspace-server', 'src', 'index.ts');
const updateJsonFile = (filePath, version) => {
try {
const content = JSON.parse(fs.readFileSync(filePath, 'utf8'));
content.version = version;
fs.writeFileSync(filePath, JSON.stringify(content, null, 2) + '\n');
console.log(`Updated ${path.relative(rootDir, filePath)} to version ${version}`);
} catch (error) {
console.error(`Failed to update JSON file at ${path.relative(rootDir, filePath)}:`, error);
process.exit(1);
}
};
const updateTsFile = (filePath, version) => {
try {
const content = fs.readFileSync(filePath, 'utf8');
// Regex to find version: "x.y.z" and replace it
// Matches version: "..." or version: '...'
const regex = /(version:\s*["'])[^"']*(["'])/;
if (regex.test(content)) {
const newContent = content.replace(regex, `$1${version}$2`);
if (content !== newContent) {
fs.writeFileSync(filePath, newContent);
console.log(`Updated ${path.relative(rootDir, filePath)} to version ${version}`);
} else {
console.log(`Version in ${path.relative(rootDir, filePath)} is already ${version}`);
}
} else {
console.error(`Could not find version string in ${path.relative(rootDir, filePath)}`);
process.exit(1);
}
} catch (error) {
console.error(`Failed to update TS file at ${path.relative(rootDir, filePath)}:`, error);
process.exit(1);
}
};
const main = () => {
let version = process.argv[2];
if (version) {
// If version is provided as arg, update root package.json first
updateJsonFile(packageJsonPath, version);
} else {
// Otherwise read from root package.json
const packageJson = require(packageJsonPath);
version = packageJson.version;
console.log(`Using version from package.json: ${version}`);
}
if (!version) {
console.error('No version specified and no version found in package.json');
process.exit(1);
}
updateJsonFile(workspaceServerPackageJsonPath, version);
updateTsFile(workspaceServerIndexPath, version);
};
main();