Skip to content

Commit

Permalink
[DX-2988] faster and simpler watch mode and build in ci (#1957)
Browse files Browse the repository at this point in the history
  • Loading branch information
shineli1984 authored Jul 18, 2024
1 parent 3b3d193 commit d595f76
Show file tree
Hide file tree
Showing 32 changed files with 920 additions and 112 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build-lint-typecheck-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
branches:
- main

env:
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.TS_IMMUTABLE_SDK_NX_TOKEN }}

jobs:
build:
name: Build
Expand All @@ -34,7 +37,7 @@ jobs:
key: ${{ runner.os }}-build-cache-deps-${{ hashFiles('yarn.lock') }}

- name: Install dependencies
if: steps.restore-cache-node_modules.outputs.cache-hit != 'true'
if: steps.restore-cache-node_modules.outputs.cache-hit != 'true'
run: yarn install --immutable

- name: Check Single Package Version Policy
Expand Down Expand Up @@ -82,7 +85,7 @@ jobs:

- name: Build passport sample app
run: yarn workspace @imtbl/passport-sdk-sample-app build

typecheck:
name: Typecheck
runs-on: ubuntu-latest-4-cores
Expand Down
41 changes: 0 additions & 41 deletions .github/workflows/build-sdk.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ yalc.lock
package-lock.json

docs/



.nx/cache
.nx/workspace-data
nx-cloud.env
dep-graph.json
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,20 @@ Most packages in the workspace have their own README file that provides specific

NOTE: Some packages may have additional commands or require different setup steps, for example in a certain package the development mode may be started with `yarn start:dev` instead of `yarn start`, or `yarn dev`. For the steps below we will be assuming defaults.

### Development Mode
### Development watch mode

Development mode allows you to run a package in a local development environment. This is useful for testing changes and new features before deploying them to production. Any changes made will be reflected in real-time on save, so you can see the effects of your changes immediately. These are propogated in real time to the sample app, if it is running, for that SDK too.

Development mode for sdk packages will tend to use rollup and its watch mode to rebuild the package on file changes. This build output, stored in the dist folder of the sdk package, is what is exposed to any other packages that depend on it, including the sample app.

To run a package in development mode, you can use the following command in the context of that package:
Build a package and start watch mode for a package:

```bash
# Context: Specified package
yarn start
yarn dev YOUR_PACKAGE_NAME
```

Some packages also come with a sample app that you can run in development mode. To run the sample app, you first need to run the SDK it depends on in development mode, and then run the sample app development mode in a seperate terminal. For example, to run the `checkout-sdk-sample-app` located at `/packages/checkout/sdk-sample-app`, you can use the following commands:

e.g. for @imtbl/passport package
```bash
yarn workspace @imtbl/checkout-sdk start

# Or from the root of the monorepo
cd packages/checkout/sdk
yarn start
yarn dev @imtbl/passport
```
Changes in any packages that `@imtbl/passport` depends on will re-build passport package.

Then, in a separate terminal:

```bash
yarn workspace @imtbl/checkout-sdk-sample-app start

# Or from the root of the monorepo
cd packages/checkout/sdk-sample-app
yarn start
```

### Running Tests

Expand Down
38 changes: 38 additions & 0 deletions build-dependents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { execSync } = require('child_process');
const fs = require('fs');

if (process.argv.length < 4) {
console.error('Please provide a changed project name and the current project.');
process.exit(1);
}

const changedProject = process.argv[2];
const currentProject = process.argv[3];

try {
console.log('changedProject:', changedProject);
// Generate the focused dependency graph JSON
execSync(`nx graph --file=dep-graph.json --focus=${currentProject}`, { stdio: 'inherit' });

// Read and parse the dependency graph JSON
const depGraph = JSON.parse(fs.readFileSync('dep-graph.json', 'utf-8'));

// Check if the changed project is in the dependencies of the current project
const dependencies = depGraph.graph.dependencies[currentProject] || [];
const isDependent = dependencies.some((dep) => dep.target === changedProject);

if (isDependent || changedProject === currentProject) {
// Rebuild the current project
const command = `nx run-many --target=d --projects=${currentProject} --parallel=5`;

console.log(`Running command: ${command}`);
execSync(command, { stdio: 'inherit' });

console.log(`Rebuilt the ${currentProject} project successfully.`);
} else {
console.log(`No need to build anything as changes to ${changedProject} do not affect ${currentProject}.`);
}
} catch (error) {
console.error('Error rebuilding projects:', error);
process.exit(1);
}
27 changes: 27 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Check if an argument was provided
if [ -z "$1" ]; then
echo "No package specified."
echo ""
echo "Fetching package list..."
# This command might change based on your nx setup. It assumes you can list projects like this.
nx show projects | grep @ | sort | awk '{print NR-1 " " $1}' > /tmp/nx_projects
cat /tmp/nx_projects
echo ""
echo "Enter the number of the project you want to select:"
read project_number
project_name=$(awk -v num="$project_number" '$1 == num {print $2}' /tmp/nx_projects)
echo ""
echo "You selected: $project_name"
echo ""
rm /tmp/nx_projects
PACKAGE_NAME=$project_name
else
PACKAGE_NAME=$1
fi

# Run nx commands with the selected or provided package name
echo "Running commands for package: $PACKAGE_NAME"
nx run $PACKAGE_NAME:d --parallel=5
nx watch --all -- node ./build-dependents.js \$NX_PROJECT_NAME $(echo $PACKAGE_NAME)
5 changes: 5 additions & 0 deletions nx-cloud.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Create a file named nx-cloud.env in the root of the repo and obtain the contents from the Platform Engineering Shared 1Password vault
# By default the token set in the nx.json cloud access token field will only allow for read, while the token in the nx-cloud.env file will allow for read/write
# This is typically needed when you want to save the cache remotely from your local machine.

NX_CLOUD_ACCESS_TOKEN=<TOKEN>
32 changes: 32 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"targetDefaults": {
"d": {
"dependsOn": [
"^d"
],
"outputs": [
"{projectRoot}/dist"
],
"cache": true
},
"lint": {
"cache": true
},
"test": {
"cache": true
},
"build": {
"dependsOn": [
"^build"
],
"outputs": [
"{projectRoot}/dist"
],
"cache": true
}
},
"defaultBase": "main",
"parallel": 5,
"nxCloudAccessToken": "Mzg3ZGY1MWUtYmYyNy00ZmE4LTkyNDAtYjYxZmJmYmE4NWQ3fHJlYWQ="
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"@actions/core": "^1.10.1",
"@emotion/react": "^11.11.3",
"@release-it-plugins/workspaces": "^4.0.0",
"@swc/cli": "^0.4.0",
"@swc/core": "^1.3.36",
"@types/chai": "^4.3.16",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
Expand All @@ -23,6 +25,7 @@
"husky": "^8.0.3",
"lint-staged": "^13.2.0",
"node-gyp": "^9.3.1",
"nx": "19.3.2",
"release-it": "^16.1.3",
"syncpack": "^9.8.4",
"typedoc": "^0.24.8",
Expand All @@ -44,9 +47,8 @@
"responselike": "^2.0.0"
},
"scripts": {
"build": "NODE_OPTIONS=--max-old-space-size=14366 wsrun -y 4 -p @imtbl/sdk -p @imtbl/checkout-widgets -e -r --serial build && yarn syncpack:format && yarn wsrun -p @imtbl/sdk -a -m copyBrowserBundles",
"build:onlysdk": "NODE_OPTIONS=--max-old-space-size=14366 wsrun -y 4 -p @imtbl/sdk --stages build && yarn syncpack:format",
"dev": "wsrun --parallel --exclude-missing dev",
"build": "yarn workspace @imtbl/sdk updateDependencies && NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk,@imtbl/checkout-widgets && yarn syncpack:format && yarn wsrun -p @imtbl/sdk -a -m copyBrowserBundles",
"build:onlysdk": "NODE_OPTIONS=--max-old-space-size=14366 nx run-many --target=build --projects=@imtbl/sdk && yarn syncpack:format",
"docs:build": "typedoc",
"docs:serve": "http-server ./docs --cors -p 8080 -c-1",
"lint": "wsrun --exclude-missing -e lint --no-error-on-unmatched-pattern",
Expand All @@ -62,7 +64,8 @@
"test:checkout:sdk:coverage": "wsrun -p @imtbl/checkout-sdk --exclude-missing -e test:coverage",
"test:examples": "cd examples/ts-immutable-sample && yarn test && yarn test:e2e",
"test:vpn": "RUN_VPN_TESTS=1 wsrun --exclude-missing -e test",
"typecheck": "wsrun --exclude-missing typecheck"
"typecheck": "wsrun --exclude-missing typecheck",
"dev": "./dev.sh"
},
"workspaces": {
"packages": [
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain-data/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"generate-types": "typechain --target=ethers-v5 --out-dir=src/typechain/types 'abi/*.json'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"test": "jest",
Expand Down
2 changes: 1 addition & 1 deletion packages/checkout/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"scripts": {
"build": "rollup --config rollup.config.js",
"build:dev": "CHECKOUT_DEV_MODE=true yarn build",
"dev": "CHECKOUT_DEV_MODE=true rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"docs": "typedoc --plugin typedoc-plugin-markdown --skipErrorChecking --disableSources --out docs src/index.ts",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"lint:fix": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0 --fix",
Expand Down
2 changes: 1 addition & 1 deletion packages/checkout/widgets-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"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",
"dev": "NODE_ENV=development rollup --config rollup.config.js",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"lint:fix": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0 --fix",
"start": "yarn clean && NODE_ENV=development rollup --config rollup.config.js --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"test": "jest",
"test:watch": "jest --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/bridge/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts --max-warnings=0",
"lint:fix": "cd ../../../../ && yarn wsrun -p @imtbl/bridge-sdk -c lint --fix",
"test": "jest test -- --silent=false",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/cryptofiat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0 --fix",
"prepare": "wsrun -r build",
"test": "jest",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/dex/sdk-sample-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"private": true,
"scripts": {
"dev": "next dev",
"d": "next dev",
"lint": "eslint ./src --ext .ts --max-warnings=0",
"setup": "cd ../../../../ && yarn build:onlysdk",
"start": "next start"
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/dex/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"generate-types": "typechain --target ethers-v5 --out-dir ./src/contracts/types ./src/contracts/ABIs/*.json",
"lint": "eslint ./src --ext .ts --max-warnings=0",
"lint:fix": "cd ../../../.. && yarn wsrun -p @imtbl/dex-sdk -c lint --fix",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/factory/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts --max-warnings=0",
"lint:fix": "cd ../../../ && yarn wsrun -p @imtbl/factory-sdk -c lint --fix",
"test": "jest test",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/generated-clients/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"prepare": "wsrun -r build",
"test": "jest",
"typecheck": "tsc --noEmit --jsx preserve",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/guardian/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w"
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'"
},
"type": "module",
"types": "dist/index.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"test": "jest",
"typecheck": "tsc --noEmit --jsx preserve"
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"repository": "immutable/ts-immutable-sdk.git",
"scripts": {
"build": "NODE_ENV=production rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js -w",
"d": "swc src -d dist --strip-leading-paths --ignore '**/*.test.*'",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"prepare": "wsrun -r build",
"test": "jest",
Expand Down
Loading

0 comments on commit d595f76

Please sign in to comment.