From 600801f1943cf4741fbc7ffa41d6500ee6b8c616 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 12 Sep 2024 13:45:25 +0100 Subject: [PATCH] DEV: Automatically add `--ignore-workspace` when pnpm used in plugins (#28878) --- .pnpmfile.cjs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/.pnpmfile.cjs b/.pnpmfile.cjs index 450a3dac307..3a21a8206d0 100644 --- a/.pnpmfile.cjs +++ b/.pnpmfile.cjs @@ -1,7 +1,9 @@ const fs = require("fs"); -const { execSync } = require("child_process"); +const { execSync, execFileSync } = require("child_process"); -if (fs.existsSync("node_modules/.yarn-integrity")) { +const discourseRoot = __dirname; + +if (fs.existsSync(`${discourseRoot}/node_modules/.yarn-integrity`)) { console.log( "Detected yarn-managed node_modules. Performing one-time cleanup..." ); @@ -9,8 +11,40 @@ if (fs.existsSync("node_modules/.yarn-integrity")) { // Delete entire contents of all node_modules directories // But keep the directories themselves, in case they are volume mounts (e.g. in devcontainer) execSync( - "find ./node_modules ./app/assets/javascripts/*/node_modules -mindepth 1 -maxdepth 1 -exec rm -rf {} +" + `find ${discourseRoot}/node_modules ${discourseRoot}/app/assets/javascripts/*/node_modules -mindepth 1 -maxdepth 1 -exec rm -rf {} +` ); console.log("cleanup done"); } + +const pluginBase = `${discourseRoot}/plugins/`; +const cwd = process.cwd(); +const pluginName = + cwd.startsWith(pluginBase) && cwd.replace(pluginBase, "").split("/", 2)[0]; + +if ( + pluginName && + fs.existsSync(`${discourseRoot}/plugins/${pluginName}/package.json`) && + !process.argv.includes("--ignore-workspace") +) { + console.log( + "> pnpm was run inside a plugin directory. Re-executing with --ignore-workspace..." + ); + + try { + execFileSync( + process.argv[0], + [...process.argv.slice(1), "--ignore-workspace"], + { + stdio: "inherit", + } + ); + } catch (e) { + if (e.status) { + process.exit(e.status); + } + throw e; + } + + process.exit(0); +}