Skip to content

Commit

Permalink
Script for installing local version of sdkv2 (#2088)
Browse files Browse the repository at this point in the history
* script for installing local version of sdkv2

* only go off diffs if we have local installed

* dont add to overrides

* ensure SDK_PATH exists
  • Loading branch information
artursapek committed May 15, 2024
1 parent 9958b97 commit 222178b
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions wormhole-connect/scripts/sdkv2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const fs = require('fs');
const path = require('path');

const args = process.argv.slice(2);
const command = args[0];

const SDK_PATH = process.env['SDK_PATH'];

if (!SDK_PATH) throw new Error('Please set SDK_PATH in your environment');

const { execSync } = require('child_process');

// This script builds, packs, and installs sdkv2 from a local directory

const packageJsonPath = path.join(__dirname, '../package.json');
const sdkPackageJsonPath = path.join(SDK_PATH, './package.json');

// Get SDKv2 version
const { version, workspaces } = JSON.parse(
fs.readFileSync(sdkPackageJsonPath, 'utf8'),
);

let sdkPackages = [];
let sdkPackagesWithDiff = [];

for (const workspace of workspaces) {
if (workspace.includes('examples')) continue;
const workspacePackageJson = path.join(SDK_PATH, workspace, 'package.json');
const { name } = JSON.parse(fs.readFileSync(workspacePackageJson));
sdkPackages.push(name);

const diff = execSync(`git diff ${workspace}`, { cwd: path.join(SDK_PATH) });
if (diff.length > 0) {
sdkPackagesWithDiff.push(name);
}
}

if (command === 'install') {
console.log(
`Building and installing sdkv2 version ${version}\nPackages:\n ${sdkPackages.join(
'\n ',
)}`,
);

// Build SDKv2
console.log('Building SDK...');
execSync('npm run build', { cwd: SDK_PATH, encoding: 'utf-8' });

// Pack SDKv2
console.log('Packing SDK...');
execSync('npm pack --workspaces', { cwd: SDK_PATH, encoding: 'utf-8' });

// Get freshly made archives
const packageValues = {};
for (let pkg of sdkPackages) {
packageValues[pkg] = path.join(
SDK_PATH,
`wormhole-foundation-${pkg.split('/')[1]}-${version}.tgz`,
);
}

const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));

let alreadyHasLocalInstalled = packageJson.dependencies.hasOwnProperty(
'@wormhole-foundation/sdk-connect',
);

for (let pkg in packageValues) {
packageJson.dependencies[pkg] = packageValues[pkg];
}

fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, undefined, 2),
'utf-8',
);

let toInstall;

if (alreadyHasLocalInstalled && sdkPackagesWithDiff.length > 0) {
// Detected local changes; install only these
console.log('Installing packages with local diff');
toInstall = sdkPackagesWithDiff;
} else {
// Did not detect local changes; install all packages
console.log('Installing all packages');
toInstall = sdkPackages;
}

for (let pkg of toInstall) {
console.log(pkg);
execSync(`npm install ${pkg}`, { cwd: path.join(__dirname, '..') });
}
} else if (command === 'remove') {
// TODO?
}

0 comments on commit 222178b

Please sign in to comment.