From 75fff82a59904d75565c119a9462d147b48a426a Mon Sep 17 00:00:00 2001 From: Anson Date: Thu, 24 Aug 2023 00:47:46 +0100 Subject: [PATCH] [V3-Revamp/Feat] @getlit/sdk publish pipeline (#201) * feat: add "group" metadata * feat: add support on group * feat: update "publish" script * chore: update all package versions * feat: add "whatPackages" * chore: update scripts * feat: "yarn bump" for @getlit/sdk * chore: update log * chore: update script * chore: update ui * chore: improve DX --- lit.group.json | 6 +- package.json | 21 +- .../access-control-conditions/package.json | 3 +- packages/auth-browser/package.json | 3 +- packages/auth-helpers/package.json | 3 +- packages/bls-sdk/package.json | 3 +- packages/constants/package.json | 3 +- packages/contracts-sdk/package.json | 3 +- packages/core/package.json | 3 +- packages/crypto/package.json | 3 +- packages/ecdsa-sdk/package.json | 3 +- packages/encryption/package.json | 3 +- packages/getlit-sdk/package.json | 3 +- packages/getlit-sdk/src/lib/utils.ts | 2 +- packages/lit-auth-client/package.json | 3 +- packages/lit-node-client-nodejs/package.json | 3 +- packages/lit-node-client/package.json | 3 +- packages/lit-storage/package.json | 3 +- packages/lit-third-party-libs/package.json | 3 +- packages/misc-browser/package.json | 3 +- packages/misc/package.json | 3 +- packages/nacl/package.json | 3 +- packages/pkp-base/package.json | 3 +- packages/pkp-client/package.json | 3 +- packages/pkp-cosmos/package.json | 3 +- packages/pkp-ethers/package.json | 3 +- packages/pkp-sui/package.json | 3 +- packages/pkp-walletconnect/package.json | 3 +- packages/types/package.json | 3 +- packages/uint8arrays/package.json | 3 +- tools/scripts/pub.mjs | 94 ++++++--- tools/scripts/tools.mjs | 195 +++++++++++------- tools/scripts/utils.mjs | 51 ++++- tools/scripts/what-packages.mjs | 65 ++++++ 34 files changed, 373 insertions(+), 142 deletions(-) create mode 100644 tools/scripts/what-packages.mjs diff --git a/lit.group.json b/lit.group.json index e9a9521576..2e72c823cb 100644 --- a/lit.group.json +++ b/lit.group.json @@ -1,12 +1,14 @@ [ { + "namespace": "@lit-protocol", "group": "core", - "version": "2.2.49", + "version": "2.2.51", "npm": "https://registry.npmjs.org/@lit-protocol/lit-node-client" }, { + "namespace": "@getlit", "group": "revamp", - "version": "0.0.4", + "version": "0.0.10", "npm": "https://registry.npmjs.org/@getlit/sdk" } ] \ No newline at end of file diff --git a/package.json b/package.json index 02fb03ade2..524f684937 100644 --- a/package.json +++ b/package.json @@ -22,10 +22,11 @@ "test:e2e": "yarn tools --test --e2e run-react-and-test", "test:unit": "yarn test:packages", "test:custom": "yarn tools --test --custom", - "publish:packages": "yarn node ./tools/scripts/pub.mjs --prod", - "publish:dev": "yarn node ./tools/scripts/pub.mjs --tag dev", - "publish:test": "yarn node ./tools/scripts/pub.mjs --tag test", - "publish:revamp": "yarn node ./tools/scripts/pub.mjs --tag revamp", + "publish:packages": "yarn node ./tools/scripts/pub.mjs --prod --group=core", + "publish:dev": "yarn node ./tools/scripts/pub.mjs --tag dev --group=core", + "publish:test": "yarn node ./tools/scripts/pub.mjs --tag test --group=core", + "publish:revamp": "yarn node ./tools/scripts/pub.mjs --prod --group=revamp", + "publish:revamp:dev": "yarn node ./tools/scripts/pub.mjs --tag dev --group=revamp", "publish:app:html": "yarn node ./tools/scripts/build-html.mjs && cd dist/apps/html && vercel --prod", "buildAndPublish": "yarn build:packages && yarn publish:packages", "bbp": "yarn bump && yarn build && yarn publish:dev", @@ -49,14 +50,18 @@ "apps": "yarn tools --dev --apps", "watch": "yarn tools --watch", "v": "yarn tools v", - "bump": "yarn tools v --patch", - "bump:minor": "yarn tools v --minor", - "bump:major": "yarn tools v --major", + "bump": "yarn tools --version --patch --group=core", + "bump:minor": "yarn tools --version --minor --group=core", + "bump:major": "yarn tools --version --major --group=core", + "bump:revamp": "yarn tools --version --patch --group=revamp", + "bump:revamp:minor": "yarn tools --version --minor --group=revamp", + "bump:revamp:major": "yarn tools --version --major --group=revamp", "b": "git branch --show", "txServer": "node tx-handler-server.mjs", "prettier": "nx format:write --all", "delete:merged-branches": "git branch --merged | egrep -v '(^\\*|master|main|develop)' | xargs git branch -d", - "delete:no-merged-branches": "git branch --no-merged | egrep -v '(^\\*|master|main|develop)' | xargs git branch -d" + "delete:no-merged-branches": "git branch --no-merged | egrep -v '(^\\*|master|main|develop)' | xargs git branch -d", + "what:packages": "node ./tools/scripts/what-packages.mjs" }, "private": true, "dependencies": { diff --git a/packages/access-control-conditions/package.json b/packages/access-control-conditions/package.json index 4b46c7b590..c1c37b2d55 100644 --- a/packages/access-control-conditions/package.json +++ b/packages/access-control-conditions/package.json @@ -21,7 +21,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/auth-browser/package.json b/packages/auth-browser/package.json index b2656291b9..68659c40b0 100644 --- a/packages/auth-browser/package.json +++ b/packages/auth-browser/package.json @@ -30,7 +30,8 @@ "tags": [ "browser" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/auth-helpers/package.json b/packages/auth-helpers/package.json index 8a2cbd9cd9..e6bedb519e 100644 --- a/packages/auth-helpers/package.json +++ b/packages/auth-helpers/package.json @@ -28,7 +28,8 @@ "crypto": false, "stream": false }, - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/bls-sdk/package.json b/packages/bls-sdk/package.json index 150a076d66..048220403b 100644 --- a/packages/bls-sdk/package.json +++ b/packages/bls-sdk/package.json @@ -24,7 +24,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/constants/package.json b/packages/constants/package.json index 24989598d9..f46ee59fbb 100644 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -20,7 +20,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/contracts-sdk/package.json b/packages/contracts-sdk/package.json index 8d099a2b62..60c06b3f2b 100644 --- a/packages/contracts-sdk/package.json +++ b/packages/contracts-sdk/package.json @@ -27,7 +27,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index f4162479da..4f7a3c6194 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/core", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/crypto/package.json b/packages/crypto/package.json index d23c43a2d0..50e1007ce0 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -21,7 +21,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/ecdsa-sdk/package.json b/packages/ecdsa-sdk/package.json index 16c81cc39f..c7a05801f2 100644 --- a/packages/ecdsa-sdk/package.json +++ b/packages/ecdsa-sdk/package.json @@ -21,7 +21,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/encryption/package.json b/packages/encryption/package.json index 7de044a62c..980e39de1b 100644 --- a/packages/encryption/package.json +++ b/packages/encryption/package.json @@ -25,7 +25,8 @@ "crypto": false, "stream": false }, - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/getlit-sdk/package.json b/packages/getlit-sdk/package.json index 5252c96219..ecc518284a 100644 --- a/packages/getlit-sdk/package.json +++ b/packages/getlit-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@getlit/sdk", - "version": "2.2.49", + "version": "0.0.10", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -32,6 +32,7 @@ ".": "./src/index.js", "./autoLoad": "./src/autoLoad.js" }, + "group": "revamp", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/getlit-sdk/src/lib/utils.ts b/packages/getlit-sdk/src/lib/utils.ts index 8e35e99504..9ae29b533f 100644 --- a/packages/getlit-sdk/src/lib/utils.ts +++ b/packages/getlit-sdk/src/lib/utils.ts @@ -24,7 +24,7 @@ import { BaseAuthenticateOptions, } from '@lit-protocol/types'; -const version = '0.0.714'; +const version = '0.0.715'; const PREFIX = 'GetLit SDK'; const logBuffer: Array = []; diff --git a/packages/lit-auth-client/package.json b/packages/lit-auth-client/package.json index 909916c550..d9910d78b0 100644 --- a/packages/lit-auth-client/package.json +++ b/packages/lit-auth-client/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/lit-auth-client", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -30,6 +30,7 @@ "@simplewebauthn/typescript-types": "^7.0.0", "nanoid": "3.3.4" }, + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/lit-node-client-nodejs/package.json b/packages/lit-node-client-nodejs/package.json index e008ef8677..d20d2ea4ad 100644 --- a/packages/lit-node-client-nodejs/package.json +++ b/packages/lit-node-client-nodejs/package.json @@ -24,7 +24,8 @@ "tags": [ "nodejs" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/lit-node-client/package.json b/packages/lit-node-client/package.json index 07f66a8d3b..3db3dd733b 100644 --- a/packages/lit-node-client/package.json +++ b/packages/lit-node-client/package.json @@ -25,7 +25,8 @@ "crypto": false, "stream": false }, - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/lit-storage/package.json b/packages/lit-storage/package.json index 77d48d2d43..a493a3473e 100644 --- a/packages/lit-storage/package.json +++ b/packages/lit-storage/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/lit-storage", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/lit-third-party-libs/package.json b/packages/lit-third-party-libs/package.json index 4808b0ad2f..d9e6c3636e 100644 --- a/packages/lit-third-party-libs/package.json +++ b/packages/lit-third-party-libs/package.json @@ -21,7 +21,8 @@ "universal" ], "gitHead": "0d7334c2c55f448e91fe32f29edc5db8f5e09e4b", - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/misc-browser/package.json b/packages/misc-browser/package.json index ae0d7b3291..1abdedb21c 100644 --- a/packages/misc-browser/package.json +++ b/packages/misc-browser/package.json @@ -21,7 +21,8 @@ "tags": [ "browser" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/misc/package.json b/packages/misc/package.json index 7e1880bede..5559a1d98e 100644 --- a/packages/misc/package.json +++ b/packages/misc/package.json @@ -24,7 +24,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/nacl/package.json b/packages/nacl/package.json index eac51c8f4a..8a11fd48d5 100644 --- a/packages/nacl/package.json +++ b/packages/nacl/package.json @@ -21,7 +21,8 @@ "access": "public", "directory": "../../dist/packages/nacl" }, - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-base/package.json b/packages/pkp-base/package.json index 261001aa84..f10cc14002 100644 --- a/packages/pkp-base/package.json +++ b/packages/pkp-base/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/pkp-base", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-client/package.json b/packages/pkp-client/package.json index 619bde0570..c294701268 100644 --- a/packages/pkp-client/package.json +++ b/packages/pkp-client/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/pkp-client", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-cosmos/package.json b/packages/pkp-cosmos/package.json index a26c3ae8dc..ec49d04174 100644 --- a/packages/pkp-cosmos/package.json +++ b/packages/pkp-cosmos/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/pkp-cosmos", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-ethers/package.json b/packages/pkp-ethers/package.json index e44d2e4dd7..38150254df 100644 --- a/packages/pkp-ethers/package.json +++ b/packages/pkp-ethers/package.json @@ -20,7 +20,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-sui/package.json b/packages/pkp-sui/package.json index 51085bf791..e162f0b9e4 100644 --- a/packages/pkp-sui/package.json +++ b/packages/pkp-sui/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/pkp-sui", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -25,6 +25,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/pkp-walletconnect/package.json b/packages/pkp-walletconnect/package.json index 143d2c4452..96c85b0402 100644 --- a/packages/pkp-walletconnect/package.json +++ b/packages/pkp-walletconnect/package.json @@ -1,6 +1,6 @@ { "name": "@lit-protocol/pkp-walletconnect", - "version": "2.2.49", + "version": "2.2.51", "type": "commonjs", "license": "MIT", "homepage": "https://github.com/Lit-Protocol/js-sdk", @@ -31,6 +31,7 @@ "tags": [ "universal" ], + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/types/package.json b/packages/types/package.json index 2e98d4bc07..23e13cfe45 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -20,7 +20,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/packages/uint8arrays/package.json b/packages/uint8arrays/package.json index 337daac214..21e3873802 100644 --- a/packages/uint8arrays/package.json +++ b/packages/uint8arrays/package.json @@ -21,7 +21,8 @@ "tags": [ "universal" ], - "version": "2.2.49", + "version": "2.2.51", + "group": "core", "main": "./dist/src/index.js", "typings": "./dist/src/index.d.ts" } \ No newline at end of file diff --git a/tools/scripts/pub.mjs b/tools/scripts/pub.mjs index 557a75e08b..216af80854 100644 --- a/tools/scripts/pub.mjs +++ b/tools/scripts/pub.mjs @@ -22,10 +22,11 @@ const args = getArgs(); const OPTION = args[0]; const VALUE = args[1]; +console.log('⏰ ========== Getting Ready ========== ⏰'); console.log(args); const groupFlag = getFlag('--group'); -console.log('groupFlag:', groupFlag); +greenLog(`â›ŗī¸ groupFlag: ${groupFlag}`); if (!OPTION || OPTION === '' || OPTION === '--help') { greenLog( @@ -56,13 +57,14 @@ if (OPTION) { let version; const groupConfig = getGroupConfig(); + if (groupFlag) { version = groupConfig.config.find((item) => item.group === groupFlag).version; } else { version = groupConfig.config.find((item) => item.group === 'core').version; } -console.log('version:', version); +greenLog(`đŸ”Ĩ version: ${version}`); let dirs = await listDirsRecursive('dist/packages', false); let newDirs = []; @@ -77,24 +79,46 @@ if (groupFlag) { } }); }); +} else { + redLog( + `🚨 No group flag is provided! Please provide a group flag eg. "yarn node ./tools/scripts/pub.mjs --tag revamp --group=revamp"` + ); + process.exit(1); } +const groupNPM = groupConfig.config.find( + (item) => item.group === groupFlag +).npm; + +const npmVersionRes = await fetch(groupNPM); +let npmVersion = await npmVersionRes.json(); + +npmVersion = Object.keys(npmVersion.time).pop(); + if (newDirs.length > 0) { dirs = newDirs; } -console.log('Ready to publish the following packages:'); +console.log( + '\n\n========== Ready to publish the following packages ========== \n' +); + +let namespace = await readJsonFile(`${dirs[0]}/package.json`); + +const maxNameLength = Math.max(...dirs.map((dir) => dir.length)); await asyncForEach(dirs, async (dir) => { - const pkg = await readJsonFile(`${dir}/package.json`); + const distPkg = await readJsonFile(`${dir}/package.json`); + + const paddedName = distPkg.name.padEnd(maxNameLength, ' '); - greenLog(`${pkg.name} => ${pkg.version}`); + greenLog(`${paddedName} ${npmVersion} => ${distPkg.version}`); // remove peer dependencies - delete pkg.peerDependencies; + delete distPkg.peerDependencies; // write the package.json file - await writeJsonFile(`${dir}/package.json`, pkg); + await writeJsonFile(`${dir}/package.json`, distPkg); }); // prompt user to confirm publish @@ -102,12 +126,12 @@ const type = OPTION === '--tag' ? `TAG => ${VALUE} - You will need to install like this: yarn add @lit-protocol/lit-node-client@${VALUE}` - : 'PRODUCTION'; + You will need to install like this: yarn add ${namespace.name}@${VALUE}` + : '🚨 PRODUCTION 🚨'; greenLog( ` - Publishing: ${type} + 🚨 Publishing: ${type} `, true ); @@ -115,27 +139,35 @@ greenLog( // get latest version let publishVersion; -if (!groupFlag) { - try { - let res = await fetch( - 'https://registry.npmjs.org/@lit-protocol/lit-node-client' - ); - - res = await res.json(); - - // get the last one - const modified = Object.keys(res.time).pop(); - - // increase x from 0.0.x to 0.0.x+1 - const version = modified.split('.'); - version[2] = parseInt(version[2]) + 1; - publishVersion = version.join('.'); - console.log('publishVersion', publishVersion); - } catch (e) { - yellowLog( - "Couldn't get latest version from npm, will use the config version" - ); - } +let TAG; + +try { + TAG = getFlag('--tag', false); +} catch (e) { + TAG = 'latest'; +} + +try { + const groupNpm = getGroupConfig().config.find( + (item) => item.group === groupFlag + ).npm; + // greenLog(`...getting latest version from npm: ${groupNpm}`); + let res = await fetch(groupNpm); + + res = await res.json(); + + // get the last one + const foundVersion = res['dist-tags'][TAG ?? 'latest']; + + // increase x from 0.0.x to 0.0.x+1 + const version = foundVersion.split('.'); + version[2] = parseInt(version[2]) + 1; + publishVersion = version.join('.'); + // greenLog(`ℹī¸ Version "${publishVersion}" found on NPM\n`); +} catch (e) { + yellowLog( + "Couldn't get latest version from npm, will use the config version" + ); } await question('Are you sure you want to publish to? (y/n)', { diff --git a/tools/scripts/tools.mjs b/tools/scripts/tools.mjs index e9469c15f8..3fb4a93bdf 100644 --- a/tools/scripts/tools.mjs +++ b/tools/scripts/tools.mjs @@ -28,6 +28,9 @@ import { validateGroupIsInConfig, writeGroupConfig, getDefaultGroupVersion, + groupNames, + compareVersions, + greyLog, } from './utils.mjs'; import fs from 'fs'; import path from 'path'; @@ -53,7 +56,6 @@ const optionMaps = new Map([ ['--setup-local-dev', () => setupLocalDevFunc()], ['--match-versions', () => matchVersionsFunc()], ['default', () => helpFunc()], - ['v', () => versionFunc()], ['--version', () => versionFunc()], ['--verify', () => validateDependencyVersions()], ['--postBuild', () => buildTestApps()], @@ -520,6 +522,7 @@ async function buildFunc() { if (groupFlag) { await childRunCommand(`yarn tools --match-versions --group=${groupFlag}`); } else { + yellowLog('No groupFlag is provided, not a problem. Continuing...'); await childRunCommand(`yarn tools --match-versions`); } @@ -527,7 +530,7 @@ async function buildFunc() { .map((item) => item.replace('apps/', '')) .join(','); - console.log('groupFlag:', groupFlag); + console.log("🚀 Versions checked! Let's build some packages!"); let groupPackageNames; let additionalIgnoreList; @@ -1140,10 +1143,27 @@ async function removeLocalDevFunc() { item.replace('packages/', '') ); - const group = args[1]; + // -- use group flag by default + let group = args[1]; console.log('group', group); - const groupList = await getGroupPackageNames(group, removeList); + let groupList = []; + + // -- use all packages if no group flag is provided + if (!group) { + yellowLog( + `group flag not provided, not a problem, will use all packages...` + ); + + // for each group, get the package names + for (const name of groupNames) { + groupList.push(...(await getGroupPackageNames(name))); + } + } else { + groupList = await getGroupPackageNames(group, removeList); + } + + greenLog('groupList', groupList); if (groupList.length > 0) { removeList = groupList; @@ -1248,44 +1268,58 @@ async function setupLocalDevFunc() { } async function matchVersionsFunc() { - const group = getFlag('--group'); + const allPackages = (await listDirsRecursive('./packages', false)).map( + (item) => item.replace('packages/', '') + ); - let packageList; - let version; + const maxLength = Math.max(...allPackages.map((pkg) => pkg.length)); - if (group) { - validateGroupIsInConfig(group); + await asyncForEach(allPackages, async (pkg) => { + const packageJson = await readJsonFile(`packages/${pkg}/package.json`); - // match group version - greenLog(`Group flag detected: ${group}`); + // -- get the version to be updated to + const group = packageJson.group; - packageList = (await getGroupPackageNames(group)).map((item) => { - return `packages/${item}`; - }); + const groupConfig = getGroupConfig(); + + const groupName = groupConfig.config.find( + (item) => item.group === group + ).group; - // get group version - version = getGroupConfig().config.find( + const groupVersion = groupConfig.config.find( (item) => item.group === group ).version; - } else { - // async foreach packages - packageList = await listDirsRecursive('./packages', false); - // get lerna version - version = getDefaultGroupVersion(); - } + // -- update the version in package.json + let direction = compareVersions(packageJson.version, groupVersion); - await asyncForEach(packageList, async (pkg) => { - const packageJson = await readJsonFile(`${pkg}/package.json`); + if (direction === '<') { + direction = 'âŦ†'; + } else if (direction === '>') { + direction = 'âŦ‡'; + } else { + direction = 'âŦŒ'; + } - packageJson.version = version; + // Pad the package name to align version numbers + const paddedPkg = pkg.padEnd(maxLength, ' '); - greenLog( - `Updating ${pkg}/package.json version ${packageJson.version} => ${version}...` - ); - await writeJsonFile(`${pkg}/package.json`, packageJson); + if (direction === 'âŦ†') { + greenLog( + `${direction} Updating ${paddedPkg}/package.json version ${packageJson.version} => ${groupVersion}` + ); + } else { + greyLog( + `${direction} Updating ${paddedPkg}/package.json version ${packageJson.version} => ${groupVersion}` + ); + } + + // write + packageJson.version = groupVersion; + await writeJsonFile(`packages/${pkg}/package.json`, packageJson); }); - exit(); + + exit(0); } async function versionFunc() { @@ -1297,14 +1331,11 @@ async function versionFunc() { const groupConfig = getGroupConfig(); - console.log("group:", group) - - if (group) { - validateGroupIsInConfig(group); - } else { - group = 'core'; - // url = 'https://registry.npmjs.org/@lit-protocol/lit-node-client'; + if (!group) { + redLog(`"group" is undefined when being called in "versionFunc"`); + process.exit(1); } + url = groupConfig.config.find((item) => item.group === group).npm; res = await fetch(url); @@ -1419,46 +1450,72 @@ async function versionFunc() { } async function validateDependencyVersions() { - const PREFIX = '@lit-protocol'; - const ignoreList = ['']; + const PREFIXES = { + core: '@lit-protocol', + revamp: '@getlit', + }; - const packageList = (await listDirsRecursive('./packages', false)).map( - (item) => { - return `dist/${item}/package.json`; - } - ); + await asyncForEach(groupNames, async (groupName) => { + const PREFIX = PREFIXES[groupName]; - await asyncForEach(packageList, async (pkg, i) => { - const packageJson = await readJsonFile(pkg); - const pkgVersion = packageJson.version; + const allPackages = (await listDirsRecursive('./packages', false)).map( + (item) => { + return `dist/${item}/package.json`; + } + ); - const dependencies = packageJson.dependencies; + let packageList = []; - let total = 0; - let passes = 0; - let fails = 0; + await asyncForEach(allPackages, async (pkg, i) => { + const pkgJson = await readJsonFile(pkg); - // search for dependencies that start with @lit-protocol - for (const [key, value] of Object.entries(dependencies)) { - if (key.includes(PREFIX) && !ignoreList.includes(key)) { - total++; - if (value !== pkgVersion) { - fails++; - } else { - passes++; + if (pkgJson.name.includes(PREFIX)) { + packageList.push(pkg); + } + }); + const maxLength = Math.max(...packageList.map((pkg) => pkg.length)); + + const maxIndexLength = packageList.length < 10 ? 1 : 2; + const maxLineLength = maxIndexLength + 2 + maxLength + 45; + const separatorLength = maxLineLength - groupName.length - 2; // subtract 2 for the spaces on either side of the groupName + const separator = '='.repeat(separatorLength / 2); + + console.log(`\n${separator} ${groupName} packages ${separator}`); + + await asyncForEach(packageList, async (pkg, i) => { + const packageJson = await readJsonFile(pkg); + const pkgVersion = packageJson.version; + const dependencies = packageJson.dependencies; + + let total = 0; + let passes = 0; + let fails = 0; + + // search for dependencies that start with @lit-protocol + for (const [key, value] of Object.entries(dependencies)) { + if (key.includes(PREFIX)) { + total++; + if (value !== pkgVersion) { + fails++; + } else { + passes++; + } } } - } - if (fails > 0) { - redLog( - `❌ ${pkg} has ${fails} dependencies with versions that do not match.` - ); - } else { - greenLog( - `✅ ${i + 1} ${pkg} contains all dependencies with matching versions.` - ); - } + const paddedPkg = pkg.padEnd(maxLength, ' '); + const paddedIndex = (i + 1).toString().padStart(maxIndexLength, ' '); + + if (fails > 0) { + redLog( + `❌ ${paddedIndex} ${paddedPkg} has ${fails} dependencies with versions that do not match.` + ); + } else { + greenLog( + `✅ ${paddedIndex} ${paddedPkg} contains all dependencies with matching versions.` + ); + } + }); }); process.exit(); diff --git a/tools/scripts/utils.mjs b/tools/scripts/utils.mjs index da4d1c6466..8d56c06d4a 100644 --- a/tools/scripts/utils.mjs +++ b/tools/scripts/utils.mjs @@ -249,6 +249,14 @@ export const yellowLog = (msg, noDash = false) => { } }; +export const greyLog = (msg, noDash = false) => { + if (noDash) { + console.log('\x1b[90m%s\x1b[0m', msg); + } else { + console.log('\x1b[90m%s\x1b[0m', `- ${msg}`); + } +}; + export const question = (str, { yes, no }) => { return new Promise((resolve) => { return rl.question(`- ${str} [yes]/no:`, async (answer) => { @@ -481,6 +489,21 @@ export const versionChecker = (pkg, lernaVersion) => { return { status: 200, message: `${pkg.name}@${version} => @${lernaVersion}` }; }; +export function compareVersions(version1, version2) { + const v1 = version1.split('.').map(Number); + const v2 = version2.split('.').map(Number); + + for (let i = 0; i < 3; i++) { + if (v1[i] > v2[i]) { + return '>'; + } else if (v1[i] < v2[i]) { + return '<'; + } + } + + return '=='; +} + // mini custom test framework // Describe function @@ -607,7 +630,7 @@ export function findArg(args, flag) { } } -export const getFlag = (flag = '--foo') => { +export const getFlag = (flag = '--foo', exit = true) => { try { const args = process.argv.slice(2); @@ -616,8 +639,11 @@ export const getFlag = (flag = '--foo') => { const value = args[index].split('=')[1]; if (!value) { - redLog(`❌ "${flag}" value cannot be empty. eg. ${flag}=bar`); - process.exit(); + yellowLog(`👀 "${flag}" value is empty. eg. ${flag}=bar`); + + if(exit){ + process.exit(); + } } return value; @@ -734,7 +760,15 @@ export function validateGroupIsInConfig(group) { } export async function getGroupPackageNames(groupName, allPackages) { + if (!groupName) { + redLog(`❌ No group name provided.`); + process.exit(1); + } + + greenLog(`...trying to find "${groupName}"`); + if (!allPackages) { + yellowLog(`🚨 "allPackages" is not provided, getting all packages...`); allPackages = (await listDirsRecursive('./packages', false)).map((item) => item.replace('packages/', '') ); @@ -742,7 +776,7 @@ export async function getGroupPackageNames(groupName, allPackages) { let groupList = []; - greenLog(`🔎 Looking for packages in "${groupName}" group...`, true); + greenLog(`🔎 Looking for packages in "${groupName}" group...`); for (let i = 0; i < allPackages.length; i++) { const pkgName = allPackages[i]; @@ -757,6 +791,13 @@ export async function getGroupPackageNames(groupName, allPackages) { } } + if (groupList.length === 0) { + redLog(`❌ No packages found in "${groupName}" group.`); + process.exit(); + } + + greenLog(`🔎 Found ${groupList.length} packages in "${groupName}" group.`); + return groupList; } @@ -767,3 +808,5 @@ export const getDefaultGroupVersion = () => { return version; }; + +export const groupNames = getGroupConfig().config.map((item) => item.group); diff --git a/tools/scripts/what-packages.mjs b/tools/scripts/what-packages.mjs new file mode 100644 index 0000000000..fb47f910e3 --- /dev/null +++ b/tools/scripts/what-packages.mjs @@ -0,0 +1,65 @@ +/** + * EMS Function to Recursively Search for Imports by a Given String + * + * Usage Example: + * ```javascript + * import { findImports } from './path-to-this-file.mjs'; + * + * const searchString = '@lit-protocol'; + * const pathToSearch = './src'; + * + * const results = findImports(searchString, pathToSearch); + * console.log(results); // ['@lit-protocol/lit-storage', '@lit-protocol/lit-auth-client'] + * ``` + */ + +// what-packages.mjs + +import { promises as fs } from 'fs'; +import path from 'path'; + +async function findImports(targetString, searchPath) { + const allFiles = await listFilesRecursive(searchPath); + const targetFiles = allFiles.filter((file) => file.endsWith('.ts')); + + const matches = new Set(); + + for (const file of targetFiles) { + const content = await fs.readFile(file, 'utf-8'); + const regex = new RegExp(`from ['"](${targetString}[^'"]+)['"]`, 'g'); + let match; + while ((match = regex.exec(content)) !== null) { + matches.add(match[1]); + } + } + + return [...matches]; +} + +async function listFilesRecursive(dir) { + const dirents = await fs.readdir(dir, { withFileTypes: true }); + const files = await Promise.all( + dirents.map((dirent) => { + const res = path.resolve(dir, dirent.name); + return dirent.isDirectory() ? listFilesRecursive(res) : res; + }) + ); + return Array.prototype.concat(...files); +} + +const args = process.argv.slice(2); +const searchString = args[0] || '@lit-protocol'; +const searchPath = args[1] || './src'; + +const results = await findImports(searchString, searchPath); + +console.log('\n========== Results ==========\n', results); + +console.log("\n========== Copy/Paste into package.json's peerDependencies ==========\n"); +console.log(`"peerDependencies": {`); +results.forEach((dep, index) => { + console.log( + ` "${dep}": "*"${index === results.length - 1 ? '' : ','}` + ); +}); +console.log(`}`);