--- a/js-packages/.yarnrc.yml +++ b/js-packages/.yarnrc.yml @@ -1,3 +1,6 @@ nodeLinker: node-modules yarnPath: .yarn/releases/yarn-3.6.1.cjs + +plugins: +- ./scripts/plugins/shared.cjs --- a/js-packages/scripts/package.json +++ b/js-packages/scripts/package.json @@ -9,15 +9,11 @@ "type": "module", "version": "1.0.0", "main": "", - "dependencies": { - "rxjs": "^7.8.1", - "tslib": "^2.6.2" - }, "scripts": { - "benchMintingFee": "ts-node benchmarks/mintFee/benchmark.ts", - "loadTransfer": "ts-node transfer.nload.ts", - "generateEnv": "ts-node --esm ./generateEnv.ts", - "propose-upgrade": "ts-node --esm ./proposeupgrade.ts", - "propose-fast-track": "ts-node --esm ./proposefasttrack.ts" + "benchMintingFee": "yarn ts-node benchmarks/mintFee/benchmark.ts", + "loadTransfer": "yarn ts-node transfer.nload.ts", + "generateEnv": "yarn ts-node --esm ./generateEnv.ts", + "propose-upgrade": "yarn ts-node --esm ./proposeupgrade.ts", + "propose-fast-track": "yarn ts-node --esm ./proposefasttrack.ts" } } --- /dev/null +++ b/js-packages/scripts/plugins/shared.cjs @@ -0,0 +1,63 @@ +const npmBinsToPackageName = { + 'ts-node': 'ts-node', + 'eslint': 'eslint', + 'tsc': 'typescript', + 'mocha': 'mocha', +}; + +const paths = []; +for(const k of Object.keys(npmBinsToPackageName)) { + paths.push([k], ['run', k]); +} + +module.exports = { + name: 'plugin-root-bin', + factory: require => { + const {BaseCommand} = require('@yarnpkg/cli'); + const {Option} = require('clipanion'); + + class ExecuteBinFromRootCommand extends BaseCommand { + static paths = paths; + + args = Option.Proxy(); + + async execute() { + const fs = require('fs'); + const path = require('path'); + + const ownPackageJson = JSON.parse(await fs.promises.readFile('package.json', 'utf-8')); + const cmd = this.path[this.path.length - 1]; + + if(ownPackageJson.scripts?.[cmd]) { + throw new Error(`'${cmd}' is defined in package.json but the 'plugin-root-bin' also defined it. To avoid unexpected results, please rename the script in package.json`); + } + + const dir = await this.getBinaryPackageDirectory(fs, path, cmd, ownPackageJson); + const pkg = JSON.parse(await fs.promises.readFile(path.join(dir, 'package.json'), 'utf-8')); + + const packageRelativeBinPath = typeof pkg.bin === 'object' + ? pkg.bin[cmd] + : pkg.bin; + + const binPath = path.join(dir, packageRelativeBinPath); + + process.exitCode = await this.cli.run(['node', binPath, ...this.args]); + + // seems something in yarn resets the exit code, so force exit + process.exit(); + } + + async getBinaryPackageDirectory(_fs, path, cmd, _ownPackageJson) { + const res = path.resolve(__dirname, '../../node_modules', npmBinsToPackageName[cmd]); + console.error(res); + return res; + } + } + + return { + commands: [ + ExecuteBinFromRootCommand, + ], + }; + }, +}; --- a/js-packages/tests/package.json +++ b/js-packages/tests/package.json @@ -9,22 +9,15 @@ "type": "module", "version": "1.0.0", "main": "", - "dependencies": { - "rxjs": "^7.8.1", - "tslib": "^2.6.2" - }, "devDependencies": { - "@types/node": "^20.8.10", - "mocha": "^10.1.0", - "ts-node": "^10.9.1", - "typescript": "^5.1.6" + "mocha": "^10.1.0" }, "scripts": { - "setup": "ts-node --esm ./util/globalSetup.ts", - "setIdentities": "ts-node --esm ./util/identitySetter.ts", - "checkRelayIdentities": "ts-node --esm ./util/relayIdentitiesChecker.ts", - "_test": "yarn setup && mocha --timeout 9999999 --loader=ts-node/esm.mjs", - "_testParallel": "yarn setup && mocha --timeout 9999999 --parallel --loader=ts-node/esm.mjs", + "setup": "yarn ts-node --esm ./util/globalSetup.ts", + "setIdentities": "yarn ts-node --esm ./util/identitySetter.ts", + "checkRelayIdentities": "yarn ts-node --esm ./util/relayIdentitiesChecker.ts", + "_test": "yarn setup && yarn mocha --timeout 9999999 --loader=ts-node/esm.mjs", + "_testParallel": "yarn setup && yarn mocha --timeout 9999999 --parallel --loader=ts-node/esm.mjs", "test": "yarn _test './**/*.*test.ts'", "testParallel": "yarn _testParallel './**/*.test.ts'", "testSequential": "yarn _test './**/*.seqtest.ts'", --- a/js-packages/yarn.lock +++ b/js-packages/yarn.lock @@ -1266,9 +1266,6 @@ "@unique/scripts@workspace:scripts": version: 0.0.0-use.local resolution: "@unique/scripts@workspace:scripts" - dependencies: - rxjs: ^7.8.1 - tslib: ^2.6.2 languageName: unknown linkType: soft