-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Conversation
fbaa504
to
2c153cf
Compare
… 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.
…ger class and subclasses
Also rename it to `addDependencies` (a.k.a. `yarn add`) to avoid confusion with `installDepsFromPackageJson` (a.k.a. `yarn install`).
2c153cf
to
2ec49eb
Compare
… behavior in NPM, Yarn 1 and Yarn 2 classes.
All this variables were used to check if user was running Yarn and do this accordingly. Now we use the factory to get the "right" version of JsPackageManager instance so there is no need to worry anywhere about that.
2195244
to
f0db5dc
Compare
f0db5dc
to
1504dd5
Compare
…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`.
1504dd5
to
9dc7c55
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one!! ❤️
return parsedOutput; | ||
} | ||
} catch (e) { | ||
throw new Error(`Unable to find versions of ${packageName} using yarn`); |
There was a problem hiding this comment.
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
.
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