viewstools / yarn-workspaces-cra-crna

How to use yarn workspaces with Create React App and Create React Native App (Expo) to share common code across
151 stars 23 forks source link

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

Open RobinCsl opened 5 years ago

RobinCsl commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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