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

CLI: refactor to simplify works with multiple package managers #11074

Merged
merged 23 commits into from
Jun 10, 2020

Conversation

gaetanmaisse
Copy link
Member

@gaetanmaisse gaetanmaisse commented Jun 7, 2020

What I did

I introduced a JsPackageManager class and specialized subclasses for NPM, Yarn 1, and Yarn 2. The goal of this is to use the Factory Pattern to encapsulate everything specific to a package manager in a single class.

The factory returns an instance of NPM, Yarn 1, or Yarn 2 proxy class according to the package manager used/preferred in the end project.

All CLI's code will be package manager agnostic and will just call functions of JsPackageManager interface.

The PR is quite big but reading commit by commit should be ok as it really follows how I worked on this.

There is still to do to centralize things but I prefer to open a working PR now and continue later than working on a branch for weeks and then have a 895612456756 conflicts to solve. 🙃

How to test

  • All CI checks should be 🟢 especially CLI tests and examples-v2

@gaetanmaisse gaetanmaisse added in progress maintenance User-facing maintenance tasks cli labels Jun 7, 2020
@gaetanmaisse gaetanmaisse force-pushed the refactor-cli branch 2 times, most recently from fbaa504 to 2c153cf Compare June 8, 2020 15:25
… subclasses for NPM, Yarn and Yarn 2.

The goal of this will be to use the Factory Pattern to encapsulate everything specific to a package manager in a single class.
Then a factory will return an instance of NPM, Yarn or Yarn 2 proxy class according to the package manager used/prefered in end project.
All CLI code will be package manager agnostic and will just call functions of JsPackageManager interface.
It returns an NPMProxy, YarnProxy or Yarn2Proxy according to user project setup:
Are `yarn` or `npm` command available? Is there any `yarn.lock` file?
It avoids to use a ternary expression to define the displayed string and encapsulate specific package managers behaviors in their own classes.
As `hasYarn` function is called when `npm_init.ts` is parsed and loaded there are some issue with mock in unit tests.
Also rename it to `addDependencies` (a.k.a. `yarn add`) to avoid confusion with `installDepsFromPackageJson` (a.k.a. `yarn install`).
…rn` or `npm` calls

It move the `spawnSync` usage to only a single place which is simpler to maintain.
It also simplify testing as there is no more need to mock an external dep
…ng `package.json`

Doing this just after having read the file enable us to remove duplication and unneeded calls to `writePackageJson`.
@gaetanmaisse
Copy link
Member Author

gaetanmaisse commented Jun 9, 2020

⚠️ It will surely generate a lot of conflicts with #10913 but it worth it as with both these PRs we will greatly improve CLI codebase :slight_smile: 🎉

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Nice one!! ❤️

@ndelangen ndelangen self-assigned this Jun 10, 2020
@ndelangen ndelangen merged commit e28d0fe into next Jun 10, 2020
@ndelangen ndelangen deleted the refactor-cli branch June 10, 2020 16:08
return parsedOutput;
}
} catch (e) {
throw new Error(`Unable to find versions of ${packageName} using yarn`);
Copy link
Contributor

Choose a reason for hiding this comment

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

This refers to yarn instead of npm.

@shilman shilman mentioned this pull request Jul 2, 2021
@shilman shilman added this to the 6.4 PRs milestone Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli maintenance User-facing maintenance tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants