From 4fbc6928242bfb26eb8dff1248e92c22cf1671c3 Mon Sep 17 00:00:00 2001 From: Ben Conolly Date: Mon, 11 Nov 2019 16:40:18 +1100 Subject: [PATCH] update to get-workspaces plugin (#32) * update to get-workspaces plugin * fix build to include index so this is isntallable --- .changeset/modern-windows-appear.md | 5 +++++ .changeset/old-knives-smile.md | 5 +++++ .../gatsby-source-workspace/gatsby-node.js | 19 +++++++++++++++---- packages/gatsby-source-workspace/package.json | 3 ++- test-gatsby/gatsby-config.js | 12 +++++++++++- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 .changeset/modern-windows-appear.md create mode 100644 .changeset/old-knives-smile.md diff --git a/.changeset/modern-windows-appear.md b/.changeset/modern-windows-appear.md new file mode 100644 index 0000000..d9c365a --- /dev/null +++ b/.changeset/modern-windows-appear.md @@ -0,0 +1,5 @@ +--- +"@manypkg/gatsby-source-workspace": minor +--- + +Add new config option, which allows the user to specify the package.json fields they want to include diff --git a/.changeset/old-knives-smile.md b/.changeset/old-knives-smile.md new file mode 100644 index 0000000..45bb085 --- /dev/null +++ b/.changeset/old-knives-smile.md @@ -0,0 +1,5 @@ +--- +"@manypkg/gatsby-source-workspace": patch +--- + +Include `version` field by default diff --git a/packages/gatsby-source-workspace/gatsby-node.js b/packages/gatsby-source-workspace/gatsby-node.js index f2d4171..7ead98e 100644 --- a/packages/gatsby-source-workspace/gatsby-node.js +++ b/packages/gatsby-source-workspace/gatsby-node.js @@ -10,31 +10,36 @@ to 'meta' - this might still be the wrong name but I want to try it out. (meta represents the full contents of the package.json) */ -exports.createSchemaCustomization = ({ actions }) => { +exports.createSchemaCustomization = ({ actions }, { extraFields }) => { let { createTypes } = actions; let typeDefs = ` type workspaceInfo implements Node { name: String + version: String dir: String relativeDir: String manifest: JSON! + ${extraFields + .map(({ name, definition }) => `${name}: ${definition}`) + .join("\n ")} } `; createTypes(typeDefs); }; -exports.sourceNodes = async ({ actions }, pluginOptions) => { +exports.sourceNodes = async ({ actions }, { cwd, extraFields }) => { let { createNode } = actions; - let cwd = pluginOptions.cwd || process.cwd(); + cwd = cwd || process.cwd(); let repoRoot = await findWorkspacesRoot(cwd); let workspaces = await getWorkspaces({ cwd: repoRoot }); for (let { name, dir, config: manifest } of workspaces) { - createNode({ + let newNode = { name, dir, relativeDir: path.relative(cwd, dir), + version: manifest.version, manifest, id: name, internal: { @@ -43,6 +48,12 @@ exports.sourceNodes = async ({ actions }, pluginOptions) => { }), type: "workspaceInfo" } + }; + + extraFields.forEach(({ name }) => { + newNode[name] = manifest[name]; }); + + createNode(newNode); } }; diff --git a/packages/gatsby-source-workspace/package.json b/packages/gatsby-source-workspace/package.json index 0525cd8..8851f61 100644 --- a/packages/gatsby-source-workspace/package.json +++ b/packages/gatsby-source-workspace/package.json @@ -15,6 +15,7 @@ "get-workspaces": "^0.5.0" }, "files": [ - "gatsby-node.js" + "gatsby-node.js", + "index.js" ] } diff --git a/test-gatsby/gatsby-config.js b/test-gatsby/gatsby-config.js index 7f44d66..8ddfb4a 100644 --- a/test-gatsby/gatsby-config.js +++ b/test-gatsby/gatsby-config.js @@ -14,7 +14,17 @@ async function getGatsbyConfig() { path: `${__dirname}/src/pages` } }, - "@manypkg/gatsby-source-workspace", + { + resolve: "@manypkg/gatsby-source-workspace", + options: { + extraFields: [ + { + name: "license", + definition: `String` + } + ] + } + }, { resolve: `gatsby-source-filesystem`, options: {