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

feat(env): add remove command to pnpm env #5263

Merged
merged 9 commits into from Aug 29, 2022
Merged

Conversation

mark-omarov
Copy link
Sponsor Contributor

Summary

This PR enhances the pnpm env functionality with the uninstall command, allowing to uninstall Node.JS globally, that was previously installed with pnpm env use <version>.

Partially closes #5155

Listing installed Node.JS will be done in a separate PR, I'm trying to keep this one short.

Note to reviewers

I think there's room for refactoring, but for now, I just tried to follow what's already in place. If refactoring is an option, I could work on it furthermore.

@welcome
Copy link

welcome bot commented Aug 25, 2022

💖 Thanks for opening this pull request! 💖
Please be patient and we will get back to you as soon as we can.

packages/plugin-commands-env/src/env.ts Outdated Show resolved Hide resolved
packages/plugin-commands-env/src/env.ts Outdated Show resolved Hide resolved
packages/plugin-commands-env/src/env.ts Outdated Show resolved Hide resolved
packages/plugin-commands-env/src/env.ts Outdated Show resolved Hide resolved
@zkochan
Copy link
Member

zkochan commented Aug 25, 2022

If refactoring is an option, I could work on it furthermore.

you can do refactoring if you want.

@mark-omarov
Copy link
Sponsor Contributor Author

mark-omarov commented Aug 26, 2022

@zkochan I'll do the refactoring in a separate PR, maybe together with the pnpm env --global list command, just to keep this one simple.

I've noticed that one of the CI for Node.js 14.6 was failing, I went to check and figured there's Node.js API I was using that didn't exist back then, I've added a workaround for that specific API, hope that was worth it.

@zkochan
Copy link
Member

zkochan commented Aug 26, 2022

I think remove with rm as a short alias would be a better name. But we can add the same aliases that the pnpm remove command has: remove, rm, uninstall, un

- Fix broken node path for the windows platform
- Notify user when removing default Node.JS
- Remove `await` on `fs.unlink` in `Promise.all`
- Add support for Node.JS v14.13 and earlier
- Add `rm, un, uninstall` aliases to the `remove` command
- Update CLI help message
- Ignore only `ENOENT` errors
@mark-omarov
Copy link
Sponsor Contributor Author

mark-omarov commented Aug 27, 2022

@zkochan, I've made the necessary changes and adjusted the tests to pass on the Windows platform. I don't have one available for testing locally, will have to see if it's working on the CI.
Also, I've made some changes to the CLI help message, since now pnpm env is not only handles the installation, but allows removing as well, I think it's more appropriate to state that pnpm env is managing Node.JS versions, and not only installs them.
Please take a look, lmk if any adjustments are required. Thanks!

Comment on lines 39 to 40
name: 'remove\nuninstall',
shortAlias: 'rm,\nun',
Copy link
Member

Choose a reason for hiding this comment

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

Just write remove and rm

Suggested change
name: 'remove\nuninstall',
shortAlias: 'rm,\nun',
name: 'remove',
shortAlias: 'rm',

"@pnpm/plugin-commands-env": minor
---

Enhance `pnpm env` with the `uninstall` command.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Enhance `pnpm env` with the `uninstall` command.
Enhance `pnpm env` with the `remove` command.

name: 'use',
},
{
description: 'Uninstalls the specified version of Node.JS.',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
description: 'Uninstalls the specified version of Node.JS.',
description: 'Removes the specified version of Node.JS.',

]).catch(err => {
const { code = '' } = err

if (code.toLowerCase() !== 'enoent') {
Copy link
Member

Choose a reason for hiding this comment

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

It will be also upper case. No need to lower it

Suggested change
if (code.toLowerCase() !== 'enoent') {
if (code !== 'ENOENT') {

Comment on lines 153 to 157
await Promise.all([
fs.unlink(nodePath),
fs.unlink(npmPath),
fs.unlink(npxPath),
]).catch(err => {
Copy link
Member

Choose a reason for hiding this comment

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

just use a try/catch syntax

Suggested change
await Promise.all([
fs.unlink(nodePath),
fs.unlink(npmPath),
fs.unlink(npxPath),
]).catch(err => {
try {
await Promise.all([
fs.unlink(nodePath),
fs.unlink(npmPath),
fs.unlink(npxPath),
])
} catch (err) {
if (err.code !== 'ENOENT') throw err

Comment on lines 168 to 177
/**
* Support for earlier Node.JS versions
* `fs.rmdir` is deprecated and `fs.rm` was added in v14.14.0
* @see https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsrmpath-options-callback
*/
if (Number(processMajorVersion) === 14 && Number(processMinorVersion) <= 13) {
await fs.rmdir(versionDir, { recursive: true })
} else {
await fs.rm(versionDir, { recursive: true })
}
Copy link
Member

Choose a reason for hiding this comment

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

Just use this rimraf lib:

"@zkochan/rimraf": "^2.1.2",

- Use `remove` with `rm` alias only
- Update CLI help message
- Update tests to use `remove` command
- Replace custom support for older Node.JS versions
  with `@zkochan/rimraf`
@mark-omarov mark-omarov changed the title feat(env): add uninstall command to pnpm env feat(env): add remove command to pnpm env Aug 28, 2022

const opts = {
env: {
[PATH]: `${process.cwd()}${path.delimiter}${process.env[PATH] as string}`,
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand how this test works. Even if you remove the node.js from home path, won't execa pick the system installed node.js?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I'm sorry, I didn't have the capacity to respond yesterday.

So, you're correct, it should have picked other (in my case nvm) installed versions. I was going to restrict path to only the current folder, but I see that you've done that already.

I think it's worth mentioning that execa, for some reason (I didn't get the chance to investigate further yet) tries to resolve the "node" in the temp folder first, I'm not sure why that's happening exactly.

Anyway, thanks for merging!

@zkochan zkochan merged commit ba270a9 into pnpm:main Aug 29, 2022
@welcome
Copy link

welcome bot commented Aug 29, 2022

Congrats on merging your first pull request! 🎉🎉🎉

@mark-omarov mark-omarov deleted the enhance-env branch August 30, 2022 02:53
@zkochan zkochan added this to the v7.10 milestone Sep 2, 2022
@ryuujo1573
Copy link

my pnpm is died 😭 help
#6122

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

Successfully merging this pull request may close these issues.

Should provide a command to list and remove the installed node.
3 participants