Skip to content

Commit

Permalink
get-workspaces initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Noviny committed May 7, 2019
1 parent 4498cd8 commit b93d04a
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 65 deletions.
10 changes: 10 additions & 0 deletions .changeset/07f050af/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"releases": [{ "name": "get-workspaces", "type": "minor" }],
"dependents": [
{
"name": "@changesets/cli",
"type": "patch",
"dependencies": ["get-workspaces"]
}
]
}
1 change: 1 addition & 0 deletions .changeset/07f050af/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Initial release of get-workspaces
4 changes: 4 additions & 0 deletions .changeset/4c4740df/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "@changesets/cli", "type": "patch" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/4c4740df/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Consume get-workspaces as dependency
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cli-table": "^0.3.1",
"detect-indent": "^6.0.0",
"fs-extra": "^7.0.1",
"get-workspaces": "0.1.0",
"fuzzy": "^0.1.3",
"globby": "^9.2.0",
"inquirer": "^3.3.0",
Expand All @@ -39,4 +40,4 @@
"devDependencies": {
"jest-fixtures": "^0.5.0"
}
}
}
55 changes: 3 additions & 52 deletions packages/cli/src/utils/bolt-replacements/getWorkspaces.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,5 @@
// This is a modified version of the package-getting in bolt
// It supports yarn workspaces as well, and can fall back through
// several options
import getWorkspaces from "get-workspaces";

import fs from "fs-extra";
import path from "path";
import globby from "globby";

export default async function getWorkspaces(opts) {
const cwd = opts.cwd || process.cwd();
const tools = opts.tools || ["yarn", "bolt", "root"]; // We also support root, but don't do it by default

const pkg = await fs
.readFile(path.join(cwd, "package.json"), "utf-8")
.then(JSON.parse);

let workspaces;

if (tools.includes("yarn") && pkg.workspaces) {
if (Array.isArray(pkg.workspaces)) {
workspaces = pkg.workspaces;
} else if (pkg.workspaces.package) {
workspaces = pkg.workspaces.package;
}
} else if (tools.includes("bolt") && pkg.bolt && pkg.bolt.workspaces) {
workspaces = pkg.bolt.workspaces;
}

if (!workspaces) {
if (tools.includes("root")) {
return [{ config: pkg, dir: cwd, name: pkg.name }];
}
return null;
}

const folders = await globby(workspaces, {
cwd,
onlyDirectories: true,
absolute: true,
expandDirectories: false
});

const results = await Promise.all(
folders
.filter(dir => fs.existsSync(path.join(dir, "package.json")))
.map(async dir =>
fs.readFile(path.join(dir, "package.json")).then(contents => {
const config = JSON.parse(contents);
return { config, name: config.name, dir };
})
)
);
return results;
export default function(opts) {
return getWorkspaces({ tools: ["yarn", "bolt", "root"], ...opts });
}
41 changes: 41 additions & 0 deletions packages/get-workspaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Get Workspaces

> A simple utility to get workspaces, whether they be yarn or bolt.
This library exports a very simple function that looks at a package.json, and generates
the glob of accepted workspaces from the `workspaces` field. It is intended mostly for
use of developers building tools that want to support both kinds of mono-repos as an easy
way to write tools for both.

```javascript
import getWorkspaces from "get-workspaces";

const workspaces = await getWorkspaces();
```

Workspaces have the shape:

```
{
name // The name from the package.json
config // The package.json of the package
dir // The directory of the package
}
```

## Config

We assume the function is being run from a directory with the package.json you want to target,
however you can pass in a working directory if you want. In addition, you can change what tools
the package will scan for.

```javascript
const workspaces = await getWorkspaces({ cwd, tools });
```

The tools supported are `yarn`, `bolt`, and `root`, which returns the root package as a single workspace if passed.
Tools is an array, so you can try for one type of workspace and then another, so you could do:

```javascript
getWorkspaces({ tools: ["bolt", "yarn", "root"] });
```
11 changes: 11 additions & 0 deletions packages/get-workspaces/__fixtures__/bolt-workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "bolt-workspaces",
"description": "bolt-workspaces",
"version": "1.0.0",
"bolt": {
"workspaces": [
"packages/*"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "bolt-workspace-pkg-a",
"version": "1.0.0",
"dependencies": {
"pkg-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "bolt-workspace-pkg-b",
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions packages/get-workspaces/__fixtures__/root-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"name": "root-only",
"description": "Nothing but the root project, a non-mono-repo",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"name": "yarn-workspace-base",
"description": "Base yarn workspace work",
"version": "1.0.0",
"workspaces": [
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "yarn-workspace-base-pkg-a",
"version": "1.0.0",
"dependencies": {
"pkg-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "yarn-workspace-base-pkg-b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "yarn-workspace-package",
"description": "Yarn workspace using the nested package field",
"version": "1.0.0",
"workspaces": {
"package": [
"packages/*"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "yarn-workspace-package-pkg-a",
"version": "1.0.0",
"dependencies": {
"pkg-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "yarn-workspace-package-pkg-b",
"version": "1.0.0"
}
2 changes: 1 addition & 1 deletion packages/get-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "get-workspaces",
"version": "0.1.0",
"version": "0.0.0",
"private": true,
"description": "Get workspaces for yarn workspaces, bolt workspaces, or the root package for building repo-agnostic tools",
"main": "dist/get-workspaces.cjs.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/get-workspaces/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import globby from "globby";

export default async function getWorkspaces(opts) {
const cwd = opts.cwd || process.cwd();
const tools = opts.tools || ["yarn", "bolt", "root"]; // We also support root, but don't do it by default
const tools = opts.tools || ["yarn", "bolt"]; // We also support root, but don't do it by default

const pkg = await fs
.readFile(path.join(cwd, "package.json"), "utf-8")
Expand Down
52 changes: 42 additions & 10 deletions packages/get-workspaces/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
import { getFixturePath } from "jest-fixtures";
import getWorkspaces from "./";

describe.skip("get-workspaces", () => {
it("should resolve yarn workspaces if the yarn option is passed", () =>
false);
it("should resolve bolt workspaces if the bolt option is passed", () =>
false);
it("should resolve main package if root option is passed", () => false);
it("should by default resolve yarn workspaces", () => false);
it("should by default resolve bolt workspaces if yarn workspaces are absent", () =>
false);
it("should by return an empty array if no workspaces are found", () => false);
describe("get-workspaces", () => {
it("should resolve yarn workspaces if the yarn option is passed", async () => {
let cwd = await getFixturePath(__dirname, "yarn-workspace-base");
const workspaces = await getWorkspaces({ cwd, tools: ["yarn"] });
expect(workspaces[0].name).toEqual("yarn-workspace-base-pkg-a");
expect(workspaces[1].name).toEqual("yarn-workspace-base-pkg-b");
});
it("should resolve yarn workspaces if the yarn option is passed and package field is used", async () => {
let cwd = await getFixturePath(__dirname, "yarn-workspace-package");
const workspaces = await getWorkspaces({ cwd, tools: ["yarn"] });
expect(workspaces[0].name).toEqual("yarn-workspace-package-pkg-a");
expect(workspaces[1].name).toEqual("yarn-workspace-package-pkg-b");
});
it("should resolve bolt workspaces if the bolt option is passed", async () => {
let cwd = await getFixturePath(__dirname, "bolt-workspace");
const workspaces = await getWorkspaces({ cwd, tools: ["bolt"] });
expect(workspaces[0].name).toEqual("bolt-workspace-pkg-a");
expect(workspaces[1].name).toEqual("bolt-workspace-pkg-b");
});
it("should resolve main package if root option is passed", async () => {
let cwd = await getFixturePath(__dirname, "root-only");
const workspaces = await getWorkspaces({ cwd, tools: ["root"] });
expect(workspaces.length).toEqual(1);
});
it("should by default resolve yarn workspaces", async () => {
let cwd = await getFixturePath(__dirname, "yarn-workspace-base");
const workspaces = await getWorkspaces({ cwd });
expect(workspaces[0].name).toEqual("yarn-workspace-base-pkg-a");
expect(workspaces[1].name).toEqual("yarn-workspace-base-pkg-b");
});
it("should by default resolve bolt workspaces if yarn workspaces are absent", async () => {
let cwd = await getFixturePath(__dirname, "bolt-workspace");
const workspaces = await getWorkspaces({ cwd });
expect(workspaces[0].name).toEqual("bolt-workspace-pkg-a");
expect(workspaces[1].name).toEqual("bolt-workspace-pkg-b");
});
it("should return an empty array if no workspaces are found", async () => {
let cwd = await getFixturePath(__dirname, "root-only");
const workspaces = await getWorkspaces({ cwd });
expect(workspaces).toEqual(null);
});
});

0 comments on commit b93d04a

Please sign in to comment.