Skip to content

Commit

Permalink
Add yarn workspace support (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Jan 8, 2020
1 parent 03103eb commit 73c57e4
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/__snapshots__/cli.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ package-a
"
`;
exports[`CLI qnm <module>] should work in monorepo with yarn workspaces 1`] = `
"package-foo
└── 1.0.0
package-a
└─┬ package-foo
└── 2.0.0
"
`;
exports[`CLI qnm list should --disable-colors 1`] = `
"anotherDependency
└── 1.0.0
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ describe('CLI', () => {

expect(output).toMatchSnapshot();
});

it.only('should work in monorepo with yarn workspaces', () => {
const cwd = resolveFixture('monorepo-with-workspaces');
const output = runCommand('package-foo', { cwd });

expect(output).toMatchSnapshot();
});
});

describe('qnm list', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/monorepo-with-workspaces/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lerna": "3.15.0",
"useWorkspaces": true
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/__tests__/fixtures/monorepo-with-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "monorepo-with-workspaces",
"private": true,
"workspaces": [
"packages/*"
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "package-a",
"version": "1.0.0"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "package-b",
"version": "1.0.0"
}
43 changes: 32 additions & 11 deletions src/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { isTruthy } from '../utils';
import ModulesMap from './modules-map';
import NodeModule from './node-module';

type Workspaces =
| Array<string>
| { packages: Array<string> | undefined }
| undefined;

type YarnLock = Record<
string,
{ version: string; dependencies: Record<string, string> }
Expand Down Expand Up @@ -191,17 +196,33 @@ export default class Workspace {
const lernaJsonPath = path.join(this.root, 'lerna.json');
const lernaJson = JSON.parse(fs.readFileSync(lernaJsonPath, 'utf8'));

// TODO - add yarn workspaces support
const packages = globby.sync(lernaJson.packages, {
absolute: true,
onlyDirectories: true,
});

packages.forEach(location => {
try {
this.packages.push(Workspace.loadSync(location, false));
} catch (error) {}
});
let packages = lernaJson.packages;

if (lernaJson.useWorkspaces === true) {
const workspaces = this.packageJson.workspaces as Workspaces;

if (!Array.isArray(workspaces)) {
packages = workspaces?.packages;
} else {
packages = workspaces;
}
}

if (!packages) {
console.warn('No packages found for monorepo');
console.warn(`packages data wasn't loaded`);
}

globby
.sync(packages, {
absolute: true,
onlyDirectories: true,
})
.forEach(location => {
try {
this.packages.push(Workspace.loadSync(location, false));
} catch (error) {}
});
}

static loadSync(cwd = process.cwd(), traverse = true): Workspace {
Expand Down

0 comments on commit 73c57e4

Please sign in to comment.