Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: add script to switch from yarn to pnpm #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tyb-talks
Copy link

@tyb-talks tyb-talks commented Oct 1, 2024

This script will be used to convert all officially supported plugins & themes to use pnpm as a package manager.

See https://meta.discourse.org/t/324521 for more detail on this change.

How to run:

GITHUB_TOKEN=<your token> yarn mass-pr \
--message "DEV: Switch to use pnpm" \
--body "This PR switches usage of yarn to pnpm - more details in https://meta.discourse.org/t/324521."
--branch "switch-yarn-to-pnpm" \
--script scripts/switch-yarn-to-pnpm.sh \
$(cat plugin-list.txt)

--
Example PRs:
discourse/discourse-gifs#74 - CI runs should be passing now that discourse/.github#152 is merged.

@tyb-talks tyb-talks force-pushed the dev-add-script-to-switch-from-yarn-to-pnpm branch from dc2341c to b81b050 Compare October 1, 2024 15:56
@tyb-talks tyb-talks force-pushed the dev-add-script-to-switch-from-yarn-to-pnpm branch from b81b050 to a9c88e7 Compare October 1, 2024 16:00
@tyb-talks tyb-talks marked this pull request as ready for review October 2, 2024 00:11
fi

echo "Replacing 'yarn' with 'pnpm' in package.json scripts..."
jq 'if has("scripts") then .scripts |= with_entries(.value |= gsub("yarn "; "pnpm ")) else . end' package.json > temp.json && mv temp.json package.json
Copy link
Author

@tyb-talks tyb-talks Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a little clunky, but my experiment with using process substitution cp package.json <(jq 'some filter' package.json) errored out, so I think the temp file way is a more consistent across machines.

scripts/switch-yarn-to-pnpm.sh Outdated Show resolved Hide resolved
}' package.json > temp.json && mv temp.json package.json

echo "Installing dependencies with pnpm..."
pnpm install
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a cleanup script for existing setups, like we have in core?

https://github.com/discourse/discourse/blob/main/.pnpmfile.cjs#L6C1-L19C1

That is: will pnpm i work in dev envs that have yarn-created node_modules dirs?

Copy link
Author

@tyb-talks tyb-talks Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried without the cleanup script, pnpm tries to move the old modules into a .ignored folder, but I think for a cleaner slate, it's better to add this anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I added it to core was because pnpm's auto cleanup doesn't work in workspaces. Themes/plugins are much simpler, so I think it's probably fine to just let pnpm do its thing, and avoid duplicating the .pnpmfile hack in every single repo.

What do you think @CvX @tyb-talks ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnpm's auto cleanup doesn't work in workspaces

Ah I see.. in that case, I'm ok to let pnpm do its thing.. worst case, devs can rm their local node_modules and reinstall with pnpm.


if ! file_exists .npmrc; then
echo "Creating .npmrc..."
echo -e "engine-strict = true\nauto-install-peers = false" > .npmrc
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same npmrc settings we have for core - would be needed for ensuring running pnpm install locally on a theme/plugin repo behaves the same way.

command -v "$1" >/dev/null 2>&1
}

if ! command_exists jq; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore but... I wonder if we should just bite the bullet and write this script in Ruby? I made the mistake of writing the original ones in bash cos I thought they would be simple... but complexity has ballooned over time 😢

Copy link
Author

@tyb-talks tyb-talks Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for a future one? I'm still finding the current use cases ok for bash. (of course, without adding tools like jq in bash, it becomes quite hairy to handle stuff like json 😅 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in Ruby

some time ago I started redoing existing scripts in node js 😅 we could revive that effort, but it's low priority. though we could at least land the support for js scripts (w/o actually finishing the rewrite of all of the existing stuff)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

land the support for js scripts

It should 'just work', right? As long as you change the shebang at the top of the file to something like

#!/usr/bin/env node

or for ruby

#!/usr/bin/env ruby

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, I suppose that would work too. I was thinking about "native" support, without shelling out, but I guess that would make more sense when all scripts are already in js.

Copy link
Member

@davidtaylorhq davidtaylorhq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants