-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
176 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Initial release of get-workspaces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"releases": [{ "name": "@changesets/cli", "type": "patch" }], | ||
"dependents": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Consume get-workspaces as dependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
packages/get-workspaces/__fixtures__/bolt-workspace/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*" | ||
] | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/get-workspaces/__fixtures__/bolt-workspace/packages/pkg-a/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/get-workspaces/__fixtures__/bolt-workspace/packages/pkg-b/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "bolt-workspace-pkg-b", | ||
"version": "1.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/get-workspaces/__fixtures__/yarn-workspace-base/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*" | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/get-workspaces/__fixtures__/yarn-workspace-base/packages/pkg-a/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/get-workspaces/__fixtures__/yarn-workspace-base/packages/pkg-b/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "yarn-workspace-base-pkg-b", | ||
"version": "1.0.0" | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/get-workspaces/__fixtures__/yarn-workspace-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*" | ||
] | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/get-workspaces/__fixtures__/yarn-workspace-package/packages/pkg-a/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/get-workspaces/__fixtures__/yarn-workspace-package/packages/pkg-b/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "yarn-workspace-package-pkg-b", | ||
"version": "1.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |