-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#28259 Adding sdk-version management locally through pre-commit. +3
- Loading branch information
Showing
2 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Function to read and parse JSON | ||
const readJSON = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8')); | ||
|
||
// Function to write JSON to file | ||
const writeJSON = (filePath, data) => | ||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2) + '\n'); | ||
|
||
// Function to bump the version | ||
const bumpVersion = (version) => { | ||
const match = version.match(/^(\d+\.\d+\.\d+-alpha\.)(\d+)$/); | ||
if (!match) { | ||
throw new Error(`Invalid version format: ${version}`); | ||
} | ||
|
||
const prefix = match[1]; | ||
const buildNumber = parseInt(match[2]); | ||
|
||
const newBuildNumber = buildNumber + 1; | ||
return `${prefix}${newBuildNumber}`; | ||
}; | ||
|
||
// Function to update dependencies in a package.json file | ||
const updatePackageDependencies = (packageJsonPath, newVersion) => { | ||
const packageJson = readJSON(packageJsonPath); | ||
let updated = false; | ||
|
||
// Update dependencies to the new version | ||
if (packageJson.dependencies) { | ||
['sdk-client', 'sdk-angular', 'sdk-react', 'sdk-experiments'].forEach((dep) => { | ||
if (packageJson.dependencies[dep]) { | ||
packageJson.dependencies[dep] = newVersion; | ||
updated = true; | ||
} | ||
}); | ||
} | ||
|
||
if (updated) { | ||
writeJSON(packageJsonPath, packageJson); | ||
console.log(`Updated dependencies in ${packageJsonPath} to version ${newVersion}`); | ||
} | ||
}; | ||
|
||
// Function to update the version of the SDK packages themselves | ||
const updateSdkPackageVersion = (packageJsonPath, newVersion) => { | ||
const packageJson = readJSON(packageJsonPath); | ||
packageJson.version = newVersion; | ||
|
||
writeJSON(packageJsonPath, packageJson); | ||
console.log(`Updated ${packageJsonPath} to version ${newVersion}`); | ||
}; | ||
|
||
// Function to find all package.json files in a directory recursively | ||
const findPackageJsonFilesRecursively = (dir) => { | ||
let results = []; | ||
const list = fs.readdirSync(dir); | ||
list.forEach((file) => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
if (stat && stat.isDirectory()) { | ||
results = results.concat(findPackageJsonFilesRecursively(filePath)); | ||
} else if (file === 'package.json') { | ||
results.push(filePath); | ||
} | ||
}); | ||
return results; | ||
}; | ||
|
||
// Define the root directories to search | ||
const sdkRoot = path.join(__dirname, 'libs/sdk'); | ||
const examplesRoot = path.join(__dirname, '../examples'); | ||
|
||
// Find all package.json files in the SDK and examples directories | ||
const sdkPackages = findPackageJsonFilesRecursively(sdkRoot); | ||
const examplePackages = findPackageJsonFilesRecursively(examplesRoot); | ||
|
||
// Get the current version from sdk-client | ||
const sdkClientPackageJson = readJSON(sdkPackages.find((pkg) => pkg.includes('client'))); | ||
const currentVersion = sdkClientPackageJson.version; | ||
|
||
// Bump the version | ||
const newVersion = bumpVersion(currentVersion); | ||
|
||
// Update all SDK package.json files (versions) | ||
sdkPackages.forEach((pkg) => updateSdkPackageVersion(pkg, newVersion)); | ||
|
||
// Update all example package.json files (dependencies only) | ||
examplePackages.forEach((pkg) => updatePackageDependencies(pkg, newVersion)); | ||
|
||
console.log(`All relevant package.json files updated to version ${newVersion}`); |