Skip to content

Commit

Permalink
fix: [DX-3144] SDK build fails on Windows (#2108)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidarain1 authored Aug 25, 2024
1 parent bcdd87e commit c36c411
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 856 deletions.
3 changes: 2 additions & 1 deletion packages/checkout/widgets-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"local-cypress": "^1.2.6",
"react-app-rewired": "^2.2.1",
"react-scripts": "5.0.1",
"rimraf": "^6.0.1",
"rollup": "^4.19.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-svg": "^2.0.0",
Expand Down Expand Up @@ -99,7 +100,7 @@
"build": "yarn clean && NODE_ENV=production rollup --config rollup.config.js",
"build:analyse": "yarn build --plugin visualizer",
"build:local": "yarn clean && yarn build && mkdir -p ../widgets-sample-app/public/lib/js && cp dist/*.js ../widgets-sample-app/public/lib/js/",
"clean": "rm -rf ./dist",
"clean": "rimraf ./dist",
"d": "rollup --config rollup.config.js",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"lint:fix": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0 --fix",
Expand Down
6 changes: 3 additions & 3 deletions packages/internal/generated-clients/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ get-mr-openapi:

.PHONY: generate-imx-api-client
generate-imx-api-client:
rm -rf src/imx && \
rimraf src/imx && \
mkdir src/imx && \
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v6.2.1 generate \
-i ./app/src/imx-openapi.json \
Expand All @@ -33,7 +33,7 @@ generate-imx-api-client:

.PHONY: generate-mr-api-client
generate-mr-api-client:
rm -rf src/multi-rollup && \
rimraf src/multi-rollup && \
mkdir src/multi-rollup && \
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v7.0.1 generate \
--inline-schema-options REFACTOR_ALLOF_INLINE_SCHEMAS=true \
Expand All @@ -45,7 +45,7 @@ generate-mr-api-client:
# When running this command, ensure files not relevant to blockchain data are manually removed
.PHONY: generate-blockchain-data-types
generate-blockchain-data-types:
rm -rf src/blockchain-data && \
rimraf src/blockchain-data && \
mkdir src/blockchain-data && \
docker run --rm -v $(shell pwd):/app openapitools/openapi-generator-cli:v7.0.1 generate \
--inline-schema-options REFACTOR_ALLOF_INLINE_SCHEMAS=true \
Expand Down
1 change: 1 addition & 0 deletions packages/internal/generated-clients/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@rollup/plugin-typescript": "^11.1.6",
"@swc/core": "^1.3.36",
"jest": "^29.4.3",
"rimraf": "^6.0.1",
"rollup": "^4.19.1",
"typescript": "^5.5.4",
"unplugin-swc": "^1.5.1"
Expand Down
7 changes: 3 additions & 4 deletions sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@
"@rollup/plugin-replace": "^5.0.7",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@yarnpkg/cli": "3.5.0",
"@yarnpkg/core": "3.5.0",
"eslint": "^8.40.0",
"glob": "^10.2.3",
"rimraf": "^6.0.1",
"rollup": "^4.19.1",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-polyfill-node": "^0.13.0",
Expand Down Expand Up @@ -156,8 +155,8 @@
"@openzeppelin/contracts": "3.4.2-solc-0.7"
},
"scripts": {
"build": "yarn updateDependencies && yarn regenModules && rm -rf dist && NODE_ENV=production node --max-old-space-size=14366 ../node_modules/rollup/dist/bin/rollup --config rollup.config.js && rm -rf dist/types && yarn copyBrowserBundles",
"build:only": "rm -rf dist && NODE_ENV=production node --max-old-space-size=8192 ../node_modules/rollup/dist/bin/rollup --config rollup.config.js && rm -rf dist/types",
"build": "yarn updateDependencies && yarn regenModules && rimraf dist && NODE_ENV=production node --max-old-space-size=14366 ../node_modules/rollup/dist/bin/rollup --config rollup.config.js && rimraf dist/types && yarn copyBrowserBundles",
"build:only": "rimraf dist && NODE_ENV=production node --max-old-space-size=8192 ../node_modules/rollup/dist/bin/rollup --config rollup.config.js && rimraf dist/types",
"copyBrowserBundles": "node scripts/copyBrowserBundles.js",
"generateIndex": "node scripts/generateIndex.js",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
Expand Down
3 changes: 2 additions & 1 deletion sdk/scripts/copyBrowserBundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import fs from 'fs';
import * as glob from 'glob';
import path from 'path';
import pkg from '../package.json' assert { type: 'json' };
import { fileURLToPath } from 'url';

const dirname = path.dirname(new URL(import.meta.url).pathname);
const dirname = path.dirname(fileURLToPath(import.meta.url));

const SDK_VERSION = '__SDK_VERSION__';

Expand Down
3 changes: 2 additions & 1 deletion sdk/scripts/generateIndex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const dirname = path.dirname(new URL(import.meta.url).pathname);
const dirname = path.dirname(fileURLToPath(import.meta.url));

// Read the JSON file
const fileData = fs.readFileSync(
Expand Down
81 changes: 39 additions & 42 deletions sdk/scripts/updateDependencies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ts-check
import { getPluginConfiguration } from '@yarnpkg/cli';
import { Configuration, Project } from '@yarnpkg/core';
import semver from 'semver';
import fs from 'fs';
import path from 'path';
Expand All @@ -9,18 +7,21 @@ import { execSync } from 'child_process';
const __dirname = path.resolve();
const SDK_PACKAGE = '@imtbl/sdk';

const rootDir = path.resolve(__dirname, '..')

const workspacePackages = execSync('yarn workspaces list --json')
.toString()
.trim()
.split('\n')
.map((line) => JSON.parse(line))
.map((pkg) => pkg.name);

const workspaceNames = workspacePackages.map((pkg) => pkg.name);

// Update the map with the dependency if it doesn't exist, or if the
// version is greater than the existing version
const updateVersion = (map, dependency, version) => {
// Don't add any workspace packages as a dependency
if (workspacePackages.includes(dependency)) return;
if (workspaceNames.includes(dependency)) return;

const existingVersion = map.get(dependency);

Expand All @@ -41,50 +42,56 @@ const collectDependenciesRecusively = async (sdkWorkspace) => {

// Recursively go through a workspace and update the dependencies
const processWorkspace = (workspace) => {
const manifest = workspace.manifest;
const { dependencies, peerDependencies, devDependencies } = manifest;
const workspacePackageJSON = path.resolve(
rootDir, workspace, 'package.json'
);

const manifest = JSON.parse(fs.readFileSync(workspacePackageJSON, {encoding: 'utf8'}))
const { dependencies, peerDependencies, devDependencies, optionalDependencies } = manifest;

// Dev dependencies, only check if they're workspace packages
// And then process them
devDependencies.forEach((dep) => {
const depWorkspace = workspace.project.tryWorkspaceByIdent(dep);
Object.keys(devDependencies).forEach((dep) => {
const depWorkspace = workspacePackages.find((pkg) => pkg.name === dep);
if (depWorkspace) {
processWorkspace(depWorkspace);
processWorkspace(depWorkspace.location);
}
});

// If sdkpackage, exit early
if (manifest.raw.name === SDK_PACKAGE) return;
if (manifest.name === SDK_PACKAGE) return;

// UpdateVersion for dependencies
dependencies.forEach((dep) => {
// check for optional dependencies metadata
if (manifest.dependenciesMeta?.get(dep.name)?.get(null)?.optional) {
updateVersion(
optionalDependenciesMap,
packageName(dep.scope, dep.name),
dep.range
);
} else {
if (dependencies) Object.keys(dependencies).forEach((dep) => {
updateVersion(
dependenciesMap,
packageName(dep.scope, dep.name),
dep.range
dep,
dependencies[dep]
);
}

const depWorkspace = workspace.project.tryWorkspaceByIdent(dep);
if (depWorkspace) {
processWorkspace(depWorkspace);
}
const depWorkspace = workspacePackages.find((pkg) => pkg.name === dep);
if (depWorkspace) {
processWorkspace(depWorkspace.location);
}
});



// refactor the above optionalDependencies part
if (optionalDependencies) Object.keys(optionalDependencies).forEach((dep) =>
updateVersion(
optionalDependenciesMap,
dep,
optionalDependencies[dep]
)
);

// Same for peerDependencies, but don't recurse
peerDependencies.forEach((dep) =>
if (peerDependencies) Object.keys(peerDependencies).forEach((dep) =>
updateVersion(
peerDependenciesMap,
packageName(dep.scope, dep.name),
dep.range
dep,
peerDependencies[dep]
)
);
};
Expand All @@ -99,26 +106,16 @@ const collectDependenciesRecusively = async (sdkWorkspace) => {
};
};

// Takes a scope and a package name and returns a scoped package name
const packageName = (scope, name) => {
return scope ? `@${scope}/${name}` : name;
};

// Remove ranges to parse just version
const parseVersion = (version) => {
return version.replace(/^[^\d]*/, '');
};

// Update package.json with the dependencies and peerDependencies
const main = async () => {
const cwd = process.cwd();
const pluginConfiguration = getPluginConfiguration();
const configuration = await Configuration.find(cwd, pluginConfiguration);
const { project } = await Project.find(configuration, cwd);

const targetWorkspace = project.workspaces.find(
(workspace) => workspace.manifest.raw.name === SDK_PACKAGE
);
const targetWorkspace = workspacePackages.find(
(pkg) => pkg.name === SDK_PACKAGE
).location;

if (!targetWorkspace) {
throw Error(`${SDK_PACKAGE} package not found`);
Expand Down
3 changes: 2 additions & 1 deletion sdk/scripts/updateExports.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(new URL(import.meta.url).pathname);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Read the module-release.json file
const moduleReleaseData = fs.readFileSync(
Expand Down
Loading

0 comments on commit c36c411

Please sign in to comment.