From 81ed6647a4b1d9d93be91fb0ba9b6aa4fcc1c773 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Thu, 23 May 2024 15:41:52 +0300 Subject: [PATCH] fix: improve duplicate jest-metadata detection --- package.json | 8 +++++--- scripts/postinstall.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100755 scripts/postinstall.js diff --git a/package.json b/package.json index ed83e4d..be79f06 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,12 @@ "files": [ ".idea/icon.svg", "README.md", + "scripts/postinstall.js", "src", "dist", - "*.js", - "*.d.ts", - "*.mjs", + "./*.js", + "./*.d.ts", + "./*.mjs", "!**/__utils__", "!**/__tests__", "!**/*.test.*" @@ -60,6 +61,7 @@ "scripts": { "prepare": "husky install || true", "prepack": "tsc", + "postinstall": "node scripts/postinstall.js", "build": "tsc", "build:e2e": "tsc && nyc instrument --in-place dist && npm pack && mv jest-metadata-*.tgz package.tar.gz", "docs": "typedoc", diff --git a/scripts/postinstall.js b/scripts/postinstall.js new file mode 100755 index 0000000..c855cda --- /dev/null +++ b/scripts/postinstall.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +const fs = require('node:fs'); +const path = require('node:path'); +const process = require('node:process'); + +const instances = []; + +for (let cwd = process.cwd(); cwd !== path.dirname(cwd); cwd = path.dirname(cwd)) { + const anotherInstance = path.join(cwd, 'node_modules', 'jest-metadata'); + if (fs.existsSync(anotherInstance)) { + instances.push(anotherInstance); + } +} + +if (instances.length > 1) { + process.exitCode = 1; + + console.error('\x1b[31m%s\x1b[0m', [ + '[jest-metadata] postinstall script failed!', + 'More than one instance of jest-metadata has been installed:', + ...instances.map((dir) => `- ${dir}`), + 'Please flatten your package-lock.json or yarn.lock file to resolve this issue.', + ].join('\n')); +}