Skip to content

Commit

Permalink
#28259 Adding sdk-version management locally through pre-commit. +3
Browse files Browse the repository at this point in the history
  • Loading branch information
dcolina committed Aug 14, 2024
1 parent 003b2bb commit cd8796b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
27 changes: 21 additions & 6 deletions core-web/.husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ NC='\033[0m' # No Color

check_sdk_client_affected() {
local YARN_EXEC="${root_dir}/installs/node/yarn/dist/bin/yarn"

local affected_projects=$(npx nx show projects --affected)
echo "Affected projects: $affected_projects"
if echo "$affected_projects" | grep -q "sdk-client"; then
echo "Building sdk-client"
if ! $YARN_EXEC nx run sdk-client:build:js; then
echo "Failed to build sdk-client"

# Check if sdk-client, sdk-angular, or sdk-react are affected
if echo "$affected_projects" | grep -qE "sdk-client|sdk-angular|sdk-react"; then
echo "Running update-versions.js script"
if ! node "${root_dir}/core-web/bump-sdk-versions.js"; then
echo "Failed to update versions"
has_errors=true
else
# Add all modified package.json files to staging
git add "${root_dir}/core-web/libs/sdk/"
git add "${root_dir}/examples/"
echo "Staged updated package.json files for SDKs and examples"
fi

# Build sdk-client if affected
if echo "$affected_projects" | grep -q "sdk-client"; then
echo "Building sdk-client"
if ! $YARN_EXEC nx run sdk-client:build:js; then
echo "Failed to build sdk-client"
has_errors=true
fi
fi
fi

Expand Down Expand Up @@ -274,4 +289,4 @@ if [ "$has_errors" = true ]; then
else
echo "Commit checks completed successfully."
exit 0 # No errors, exit normally
fi
fi
92 changes: 92 additions & 0 deletions core-web/bump-sdk-versions.js
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}`);

0 comments on commit cd8796b

Please sign in to comment.