feat: sort dependencies according to detected package manager, support devEngines field - #382
Conversation
keithamus
left a comment
There was a problem hiding this comment.
I'd like @fisker to review this though, and perhaps @pfe-nazaries who also commented in #363
|
@Tobbe I pushed a refactor to simplify logic and reduce diff, can you check if there are adjustments (I didn't check comments) needed? |
| const hasYarnOrPnpmLock = (packageJson) => { | ||
| if (!cache.has(packageJson)) { | ||
| cache.set( | ||
| packageJson, |
There was a problem hiding this comment.
@keithamus Do you think we should cache this based on process.cwd()?
There was a problem hiding this comment.
Maybe also detect .yarn dir, .yarnrc.yml, pnpm-workspace.yaml
There was a problem hiding this comment.
The one thing that's left to resolve is the caching. There's a comment regarding using
cwdthat I don't fully understand. Was the idea to use onlyprocess.cwd(), or bothprocess.cwd()andpackageJsontogether?
You can memoize this function by the process.cwd(), as it should invariably return the same answer for a single execution. The packageJson is irrelevant as it's not an input to the question this function answers, only the directory is. So I would remove the packageJson arg and use a dir arg (and change the calls to check the dir + filename).
There was a problem hiding this comment.
I had to add cache.clear() to the start of sortPackageJson() for tests to pass. Since they're all using the same dir they'll reuse the same cache entry
There was a problem hiding this comment.
I could mock process.cwd() in the tests instead if you prefer
|
I've merged in the latest code from The one thing that's left to resolve is the caching. There's a comment regarding using |
|
I made a minor refactor |
| } | ||
|
|
||
| function sortPackageJson(jsonIsh, options = {}) { | ||
| cache.clear() |
There was a problem hiding this comment.
If we are doing this, maybe just use a variable instead? (sorry, didn't notice this change)
There was a problem hiding this comment.
But this makes the cache meaningless. Let's remove it, mock process.cwd() instead in test.
There was a problem hiding this comment.
I don't think clearing the cache once at the start makes the cache meaningless. sortDependencies is called multiple times during one execution of sortPackageJson(), each of those sortDependencies invocations could potentially try to read the filesystem, which the cache still prevents even when clearing it once at the start
I did He also wanted me to pass the dir to the function (same comment, #382 (comment)) @fisker you have now removed both those changes. So I'll let you two decide what solution you want here 🙂 |
There is no difference between pass or call it in |
| packageJson.pnpm || | ||
| (typeof packageManager === 'string' && | ||
| (packageManager.startsWith('yarn@') || | ||
| packageManager.startsWith('pnpm@'))) |
There was a problem hiding this comment.
Maybe corepack also support {packageManager: 'npm@1.0.0'}?
There was a problem hiding this comment.
Yeah, turns out packageManager can also be an object. bb0c487 (#382)
There was a problem hiding this comment.
And there are devEngines.packageManager ...
There was a problem hiding this comment.
Maybe corepack also support {packageManager: 'npm@1.0.0'}?
No, npm is not supported as a value for packageManager: nodejs/node#51888
devEngines field
| ? { ...object, [property]: over(object[property], ...args) } | ||
| : object | ||
| : object, | ||
| ) |
There was a problem hiding this comment.
@keithamus This should be a bug, we didn't check the object is not null or undefined before, so it can crash. Some other changes removing onObject in this PR also related to this. Found this adding support for devEngines, do you want me to extract this part to separated PRs?
There was a problem hiding this comment.
Nice find! We can keep it here.
|
|
||
| // packageManager | ||
| t.deepEqual( | ||
| getDependencyOrders({ packageManager: 'npm@1.0.0' }, () => { |
There was a problem hiding this comment.
npm is not supported as a value for packageManager nodejs/node#51888
There was a problem hiding this comment.
Block this PR you mean? No, I'd rather get it merged
| ? { ...object, [property]: over(object[property], ...args) } | ||
| : object | ||
| : object, | ||
| ) |
There was a problem hiding this comment.
Nice find! We can keep it here.
|
🎉 This PR is included in version 3.6.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
Thanks for your work on this @Tobbe! |
|
@keithamus and @fisker Thanks for guiding me through this 🙏 |
Different package managers sort dependencies using different sorting orders. This PR tries to detect the package manager that's used by the project, and chooses the sorting order based on the detected package manager
Fixes #363