Last active
January 13, 2023 21:45
-
-
Save AlansCodeLog/770d80bc7e51d264970dff75cb04ffc6 to your computer and use it in GitHub Desktop.
pnpm link local packages workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
import {spawn} from "child_process" | |
import fs from "fs" | |
import path from "path" | |
// mofified version of run from my utils lib https://github.com/AlansCodeLog/utils | |
async function run(command, cwd) { | |
const parts = command.split(" ") | |
const child = spawn(parts[0], [...parts.slice(1)], { cwd, shell: true }) | |
for await (const chunk of child.stdout) { | |
console.log(chunk.toString()) | |
} | |
for await (const chunk of child.stderr) { | |
console.log(chunk.toString()) | |
} | |
const code = await new Promise(resolve => { | |
child.on("close", resolve) | |
}) | |
if (code !== 0) { | |
process.exit(code) | |
} | |
} | |
/** | |
* | |
* Workaround for https://github.com/pnpm/pnpm/issues/3584 that does not require making changes to the project. | |
* | |
* This requires having two environment variables set: | |
* | |
* - LINKABLE_PACKAGES, a list of packages we want to link to if used: | |
* | |
* ```sh | |
* export LINKABLE_PACKAGES=" | |
* package1 | |
* package2" | |
* ``` | |
* | |
* - CODE_PROJECTS, the location of the packages | |
* | |
* This will then call `pnpm link $CODE_PROJECTS/{PACKAGENAME}` if the package uses the package as a dependency (dep, regular, or peer). | |
* | |
* Since regular pnpm link does not have issue 3583, this will work without modifying the package.json. | |
*/ | |
async function main() { | |
const packageJson = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), "package.json")).toString()) | |
const linkable = (process.env.LINKABLE_PACKAGES ?? "").split("\\n").filter(entry => entry.trim() !== "") | |
const toLink = [] | |
for (const type of ["dependencies", "devDependencies", "peerDependencies"]) { | |
if (!packageJson[type]) continue | |
const list = Object.keys(packageJson[type]) | |
for (const dep of list) { | |
if (linkable.includes(dep)) { | |
toLink.push(dep) | |
} | |
} | |
} | |
if (toLink.length > 0) { | |
console.log("Will link:") | |
console.log(toLink.join("\n")) | |
const command = toLink.map(dep => `pnpm link $CODE_PROJECTS/${dep}`).join(" && ") | |
await run(command) | |
} else { | |
console.log("Nothing to Link") | |
console.log(`LINKABLE: ${linkable}`) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment