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

How to get workspaces in given directory #712

Closed
futagoza opened this issue Jan 20, 2020 · 3 comments
Closed

How to get workspaces in given directory #712

futagoza opened this issue Jan 20, 2020 · 3 comments

Comments

@futagoza
Copy link

futagoza commented Jan 20, 2020

I've been using get-yarn-workspaces to get a list of my workspaces (for various custom scripts), but going forward I thought I should make my own package that I can easily update if required.

There are 3 ways I can proceed with this:

  1. Copy/paste get-yarn-workspaces and update
  2. Run yarn -s workspaces info via the child_process module (see this post)
  3. Use an official API

I decided to start with 3 and fall back to 2 if it doesn't work.

Looking at @yarnpkg/core I found WorkspaceFetcher but glancing at the code I get the impression this gets information (or something else) of a known workspace passed by arguments, am I right?

If so, can you tell me how I can use the official API to simply return a list of workspaces like the get-yarn-workspaces package does.

@arcanis
Copy link
Member

arcanis commented Jan 20, 2020

Hey! This is what listing the workspaces would look like with the core functions:

const {getPluginConfiguration} = require(`@yarnpkg/cli`);
const {Configuration, Project, structUtils} = require(`@yarnpkg/core`);

async function main() {
    const cwd = process.cwd();

    const configuration = await Configuration.find(cwd, getPluginConfiguration());
    const {project} = await Project.find(configuration, cwd);

    for (const workspace of project.workspaces) {
        console.log(structUtils.prettyLocator(configuration, workspace.locator));
    }
}

main().catch(error => {
    process.exitCode = 1;
    console.error(error.stack);
});

@futagoza
Copy link
Author

futagoza commented Jan 20, 2020

Thank you @arcanis, will try that tomorrow when I get home 👍

@arcanis
Copy link
Member

arcanis commented Jan 22, 2020

Closing for now, feel free to reopen if it doesn't work 🙂

@arcanis arcanis closed this as completed Jan 22, 2020
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