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

get-yarn-workspaces index.js file is different than in the repository #28

Open
RobinCsl opened this issue Dec 11, 2018 · 2 comments
Open

Comments

@RobinCsl
Copy link

Hello @dariocravero,

Thank you very much for this library!

I found something interesting:

when installing get-yarn-workspaces, I get the following index.js file:

const findRoot = require('find-root');
const flatten = require('flatten');
const fs = require('fs');
const path = require('path');
const glob = require('glob');

// as per https://yarnpkg.com/blog/2018/02/15/nohoist/ -
// "workspaces" can be an array or an object that contains "packages"
function getPackages(packageJson) {
  if (!('workspaces' in packageJson)) {
    return null;
  }
  const {workspaces} = packageJson;
  if (Array.isArray(workspaces)) {
    return workspaces;
  }
  return workspaces.packages || null;
}

module.exports = function getWorkspaces(from) {
  const root = findRoot(from, dir => {
    const pkg = path.join(dir, 'package.json');
    return fs.existsSync(pkg) && getPackages(require(pkg)) !== null;
  });

  const packages = getPackages(require(path.join(root, 'package.json')));
  return flatten(packages.map(name => glob.sync(path.join(root, name))));
};

You can see that the penultimate line

return flatten(packages.map(name => glob.sync(path.join(root, name))));

does not filter out files, unlike the version of this file on this repository:

return flatten(packages.map(name => glob.sync(path.join(root, `${name}/`))));

The package.json files downloaded in my node_modules has the correct version, namely 1.0.2.

This caused some nasty bug which was very difficult to pinpoint: a README.md file in the workspace folder seems so innocent! 😄

Could you please inquire? It could be worth bumping the patch version and then publish it to npm.

If I can be of any assistance, please let me know. Thank you very much.

@lukebatchelor
Copy link

lukebatchelor commented Jan 8, 2019

If it helps, you can actually replicate exactly what yarn workspaces does by shelling out to yarn itself

const cp = require('child_process');

function getWorkspaces(fromDir) {
  const cwd = fromDir || process.cwd();
  const workspacesStr = cp.execSync('yarn -s workspaces info', { cwd }).toString();
  return JSON.parse(workspacesStr)
}

Works from any directory, will always match whatever new rules are added by yarn and doesn't need any extra deps. You'll get something with this shape

{
  "pkg-bar": {
    "location": "packages/bar",
    "workspaceDependencies": ["pkg-baz"],
    "mismatchedWorkspaceDependencies": ["pkg-foo"]
  },
  "pkg-baz": {
    "location": "packages/baz",
    "workspaceDependencies": [],
    "mismatchedWorkspaceDependencies": []
  },
  "pkg-foo": {
    "location": "packages/foo",
    "workspaceDependencies": [],
    "mismatchedWorkspaceDependencies": []
  }
}

@RobinCsl
Copy link
Author

RobinCsl commented Jan 9, 2019

Thanks @lukebatchelor, I will try and have a look if that would my situation simpler.

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

No branches or pull requests

2 participants