-
Notifications
You must be signed in to change notification settings - Fork 200
chore: add a script to simplify version update on release #650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Copyright 2025-present The maxGraph project Contributors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // IMPORTANT: this script is intended to run as part of a GitHub workflow which is not installing the dependencies of the project. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // So please, do not import code that is not provided by the node runtime. Otherwise, update the GitHub workflow definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { readFileSync, writeFileSync } from 'node:fs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // run from the root of the repository: node scripts/update-versions.mjs 0.12.0.alpha-1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newVersion = process.argv[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.info('Updating version in various files, version:', newVersion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateVersionInRootPackageLockJsonFile(newVersion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateVersionInCorePackageJsonFile(newVersion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateVersionInSourceFile(newVersion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.info('Files have been updated'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function updateVersionInRootPackageLockJsonFile(newVersion) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = 'package-lock.json'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.info('Updating', path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fileContent = readFileContent(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const packageJson = JSON.parse(fileContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| packageJson.packages['packages/core'].version = newVersion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeJsonContentToFile(path, packageJson); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for file operations and package structure validation. The function should handle file operation errors and validate the package-lock.json structure. Consider this implementation: function updateVersionInRootPackageLockJsonFile(newVersion) {
const path = 'package-lock.json';
console.info('Updating', path);
- const fileContent = readFileContent(path);
- const packageJson = JSON.parse(fileContent);
- packageJson.packages['packages/core'].version = newVersion;
- writeJsonContentToFile(path, packageJson);
+ try {
+ const fileContent = readFileContent(path);
+ const packageJson = JSON.parse(fileContent);
+ if (!packageJson.packages?.['packages/core']) {
+ throw new Error('packages/core not found in package-lock.json');
+ }
+ packageJson.packages['packages/core'].version = newVersion;
+ writeJsonContentToFile(path, packageJson);
+ } catch (error) {
+ console.error(`Failed to update ${path}:`, error.message);
+ process.exit(1);
+ }
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ESLint[error] 32-32: Unexpected console statement. (no-console) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function updateVersionInCorePackageJsonFile(newVersion) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = 'packages/core/package.json'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.info('Updating', path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const fileContent = readFileContent(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const packageJson = JSON.parse(fileContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| packageJson.version = newVersion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeJsonContentToFile(path, packageJson); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for core package.json operations. Similar to the previous function, add error handling and file validation. Consider this implementation: function updateVersionInCorePackageJsonFile(newVersion) {
const path = 'packages/core/package.json';
console.info('Updating', path);
- const fileContent = readFileContent(path);
- const packageJson = JSON.parse(fileContent);
- packageJson.version = newVersion;
- writeJsonContentToFile(path, packageJson);
+ try {
+ const fileContent = readFileContent(path);
+ const packageJson = JSON.parse(fileContent);
+ packageJson.version = newVersion;
+ writeJsonContentToFile(path, packageJson);
+ } catch (error) {
+ console.error(`Failed to update ${path}:`, error.message);
+ process.exit(1);
+ }
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ESLint[error] 41-41: Unexpected console statement. (no-console) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function updateVersionInSourceFile(newVersion) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const path = 'packages/core/src/Client.ts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.info('Updating', path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const content = readFileContent(path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // replace the 1st occurrence, this is OK as the constant appears only once in the file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const updatedContent = content.replace( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /static VERSION =.*/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `static VERSION = '${newVersion}';` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(path, updatedContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+48
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve version replacement reliability in source file. The current implementation could be more robust with better error handling and replacement verification. Consider this enhanced implementation: function updateVersionInSourceFile(newVersion) {
const path = 'packages/core/src/Client.ts';
console.info('Updating', path);
- const content = readFileContent(path);
- // replace the 1st occurrence, this is OK as the constant appears only once in the file
- const updatedContent = content.replace(
- /static VERSION =.*/,
- `static VERSION = '${newVersion}';`
- );
- writeFileSync(path, updatedContent);
+ try {
+ const content = readFileContent(path);
+ const versionRegex = /static\s+VERSION\s*=\s*['"]([^'"]+)['"]\s*;/;
+ if (!versionRegex.test(content)) {
+ throw new Error('VERSION constant not found in expected format');
+ }
+ const updatedContent = content.replace(
+ versionRegex,
+ `static VERSION = '${newVersion}';`
+ );
+ if (content === updatedContent) {
+ throw new Error('Failed to update version in file');
+ }
+ writeFileSync(path, updatedContent);
+ } catch (error) {
+ console.error(`Failed to update ${path}:`, error.message);
+ process.exit(1);
+ }
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ESLint[error] 50-50: Unexpected console statement. (no-console) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function readFileContent(path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return readFileSync(path, 'utf8').toString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function writeJsonContentToFile(path, obj) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync(path, JSON.stringify(obj, null, 2) + '\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add version format validation.
The script should validate the version format and handle missing arguments gracefully.
Consider adding this validation:
📝 Committable suggestion
🧰 Tools
🪛 ESLint
[error] 22-22: 'process' is not defined.
(no-undef)
[error] 24-24: Unexpected console statement.
(no-console)
[error] 28-28: Unexpected console statement.
(no-console)