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

Refactor package manager detection, add support for detecting pnpm #876

Merged
merged 16 commits into from
Sep 6, 2023

Conversation

askoufis
Copy link
Contributor

The primary change in this PR is refactoring how we detect package managers. This code is contained within the new packageManager lib, leveraging @manypkg/find-root.

During this refactor I found a few bugs too. They're detailed within the changesets, but I'll add comments explaining them a bit too.

There will be a followup PR after this one that will add pnpm support for sku init, likely via CLI flag. That change will be released alongside the changes in this PR.

@askoufis askoufis requested a review from a team as a code owner August 24, 2023 01:25
@changeset-bot
Copy link

changeset-bot bot commented Aug 24, 2023

🦋 Changeset detected

Latest commit: 09eedb2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
sku Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -14,9 +14,7 @@ try {
const globs = ['node_modules/@seek/*/package.json'];
const cwd = skuCwd();

const { rootDir, tool } = findRootSync(cwd);

if (tool === 'pnpm') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tool is an object, so this was always false.

*/
const findPackageScript = (scriptContents) => {
let pkg;
try {
pkg = requireFromCwd('package.json');
// CWD should be the root of the package
pkg = requireFromCwd('./package.json');
} catch (err) {
pkg = { scripts: {} };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolving package.json from cwd (package root) would always fail, so the catch clause was executing every time, so scripts from package.json were never suggested.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 🤭


module.exports = () => {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
Copy link
Contributor Author

@askoufis askoufis Aug 24, 2023

Choose a reason for hiding this comment

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

yarnpkg is an alias present on debian, but potentially only if the cmdtest package is also installed. Ubuntu (debian) use at SEEK is basically non-existant, so detecting yarn instead of yarnpkg may actually be a bugfix that will make yarn detection work correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somehow, on my machine this command returns a version number, but in my terminal yarnpkg isn't a valid command.

@askoufis
Copy link
Contributor Author

askoufis commented Aug 25, 2023

Tests are failing because multiple storybooks are running simultaneously. There was only one fixture that used storybook, until this PR.

One fixture has a preview.tsx file, which gets symlinked to sku's node modules for storybook to find. There is a cleanup step before the symlinking happens, so the fixtures clash and one cleans up the other one's symlinked file before storybook starts, causing errors.

This issue only happens if the two test suites happen to run at the same time, which seems deterministically based on something, though I don't know exactly what. It would be nice if jest had a way to explicitly disallow a test suite to run in parallel. We could make a separate folder to isolate the test, but that's not great.

I think the best option would be to stop doing all our storybook config shenanigans and just emit files to a .storybook folder (gitignoring main.js only), like we do with other config files, and point storybook there, rather than pointing it to sku's node modules.

This would be beneficial for a few reasons:

  • Remove the symlinking shenanigans for preview config. Users can just write the file if they need it.
  • Remove the dummy middleware file. Users can just require/copy/symlink their existing sku dev middleware to .storybook if they want it to run during sku storybook.
  • There are other config files, namely preview-head.html and preview-body.html that sku doesn't currently support. They allow injecting HTML into the story iframe. These could be useful for webfonts, among other things. Just using the .storybook folder would automatically implement support for these files too.

This concludes my TED talk.

EDIT: Addressed in #878.

@askoufis
Copy link
Contributor Author

Depends on #878.

packages/sku/package.json Outdated Show resolved Hide resolved
packages/sku/telemetry/provider.js Outdated Show resolved Hide resolved
*/
const findPackageScript = (scriptContents) => {
let pkg;
try {
pkg = requireFromCwd('package.json');
// CWD should be the root of the package
pkg = requireFromCwd('./package.json');
} catch (err) {
pkg = { scripts: {} };
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 🤭

Copy link
Contributor

@mrm007 mrm007 left a comment

Choose a reason for hiding this comment

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

A few comments related to @antfu/ni, but looks good otherwise 👍

packages/sku/lib/suggestScript.js Show resolved Hide resolved
packages/sku/lib/validatePeerDeps.js Outdated Show resolved Hide resolved
packages/sku/lib/packageManager.js Outdated Show resolved Hide resolved
packages/sku/lib/packageManager.js Outdated Show resolved Hide resolved
.changeset/gorgeous-nails-film.md Outdated Show resolved Hide resolved
mrm007

This comment was marked as duplicate.

@askoufis askoufis requested a review from mrm007 September 5, 2023 07:07
Copy link
Contributor

@mrm007 mrm007 left a comment

Choose a reason for hiding this comment

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

Nice work 👍

Comment on lines +1 to +2
const { packageManager, isYarn } = require('../lib/packageManager');
const { getCommand } = require('@antfu/ni');
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking it would be ideal to absorb @antfu/ni into lib/packageManager so there's only one way to interface with the package manager. But we can iterate on that later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I've got a follow up PR that will use getCommand a bit more, I can refactor it there.

@askoufis askoufis merged commit 79abdbb into master Sep 6, 2023
4 checks passed
@askoufis askoufis deleted the detect-pnpm branch September 6, 2023 00:33
@seek-oss-ci seek-oss-ci mentioned this pull request Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants