git.delta.rocks / unique-network / refs/commits / ba5a87350f03

difftreelog

test fix shared command references

Yaroslav Bolyukin2023-11-16parent: #6d1c846.patch.diff
in: master

5 files changed

modifiedjs-packages/.yarnrc.ymldiffbeforeafterboth
--- 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
modifiedjs-packages/scripts/package.jsondiffbeforeafterboth
9 "type": "module",9 "type": "module",
10 "version": "1.0.0",10 "version": "1.0.0",
11 "main": "",11 "main": "",
12 "dependencies": {
13 "rxjs": "^7.8.1",
14 "tslib": "^2.6.2"
15 },
16 "scripts": {12 "scripts": {
17 "benchMintingFee": "ts-node benchmarks/mintFee/benchmark.ts",13 "benchMintingFee": "yarn ts-node benchmarks/mintFee/benchmark.ts",
18 "loadTransfer": "ts-node transfer.nload.ts",14 "loadTransfer": "yarn ts-node transfer.nload.ts",
19 "generateEnv": "ts-node --esm ./generateEnv.ts",15 "generateEnv": "yarn ts-node --esm ./generateEnv.ts",
20 "propose-upgrade": "ts-node --esm ./proposeupgrade.ts",16 "propose-upgrade": "yarn ts-node --esm ./proposeupgrade.ts",
21 "propose-fast-track": "ts-node --esm ./proposefasttrack.ts"17 "propose-fast-track": "yarn ts-node --esm ./proposefasttrack.ts"
22 }18 }
23}19}
2420
addedjs-packages/scripts/plugins/shared.cjsdiffbeforeafterboth
--- /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,
+      ],
+    };
+  },
+};
modifiedjs-packages/tests/package.jsondiffbeforeafterboth
--- 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'",
modifiedjs-packages/yarn.lockdiffbeforeafterboth
--- 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