From ed8459004db9fa6534e3822daf688e5af5621da6 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Thu, 28 Jul 2022 00:17:06 +0100 Subject: [PATCH] fix(node/prettier): dirty hack to allow prettier to find plugins installed with volta Volta installs all global packages into their own separate sandbox, with no other packages installed in the same node_modules directory. This hack abuses the fact that prettier can be configured with a *.js file, and effectively scans the volta installation directory for sandboxes for prettier plugins, and sets anything it finds as the "pluginSearchDirs" list. --- install.sh | 1 + prettierrc.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 prettierrc.js diff --git a/install.sh b/install.sh index 8f3f4a0..9a7777f 100755 --- a/install.sh +++ b/install.sh @@ -28,6 +28,7 @@ SYMLINKS=( irbrc peco powconfig + prettierrc.js pryrc reek.yml rspec diff --git a/prettierrc.js b/prettierrc.js new file mode 100644 index 0000000..b6d00e3 --- /dev/null +++ b/prettierrc.js @@ -0,0 +1,50 @@ +const homedir = require('os').homedir(); +const fs = require("fs"); + +function voltaPrettierSearchDirs(voltaDir) { + const packagesDir = `${voltaDir}/tools/image/packages`; + + let paths = [] + let parents = []; + + fs.readdirSync(packagesDir).forEach((item) => { + if (/^prettier-plugin-[^/]+$/.test(item)) { + paths.push(`${packagesDir}/${item}/lib`); + } + + if (item == '@prettier') { + paths = paths.concat( + findDirs(`${packagesDir}/@prettier`, /^plugin-[^/]+$/, "lib") + ); + } else if (/^@[^/]+$/.test(item)) { + parents.push(`${packagesDir}/${item}`); + } + + return []; + }) + + parents.forEach((parent) => { + paths = paths.concat(findDirs(parent, /^prettier-plugin-[^/]+$/, "lib")); + }) + + return paths; +} + +function findDirs(parent, pattern, suffix) { + return fs.readdirSync(parent).flatMap((item) => { + const fp = `${parent}/${item}`; + if (pattern.test(item) && fs.statSync(fp).isDirectory()) { + if (suffix) { + return `${fp}/${suffix}`; + } + return fp; + } + + return []; + }); + +} + +module.exports = { + pluginSearchDirs: voltaPrettierSearchDirs(`${homedir}/.volta`), +};