Skip to content

Commit

Permalink
feat(gatsby-recipes): Create default gatsby-config.js when installing…
Browse files Browse the repository at this point in the history
… a plugin if one doesn't exist (#23602)
  • Loading branch information
KyleAMathews authored Apr 29, 2020
1 parent c0c8875 commit 293e171
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-plugin resource creates default gatsby-config.js if there isn't one already 1`] = `
Object {
"_message": "Installed gatsby-plugin-foo in gatsby-config.js",
"id": "gatsby-plugin-foo",
"name": "gatsby-plugin-foo",
"options": undefined,
}
`;

exports[`gatsby-plugin resource e2e plugin resource test with hello world starter: GatsbyPlugin create 1`] = `
Object {
"_message": "Installed gatsby-plugin-foo in gatsby-config.js",
Expand Down
55 changes: 37 additions & 18 deletions packages/gatsby-recipes/src/providers/gatsby/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,46 @@ const getPluginsFromConfig = src => {
return getPlugins.state
}

const create = async ({ root }, { name, options, key }) => {
const configPath = path.join(root, `gatsby-config.js`)
const configSrc = await fs.readFile(configPath, `utf8`)
const getConfigPath = root => path.join(root, `gatsby-config.js`)

const readConfigFile = async root => {
let src
try {
src = await fs.readFile(getConfigPath(root), `utf8`)
} catch (e) {
if (e.code === `ENOENT`) {
src = `/**
* Configure your Gatsby site with this file.
*
* See: https://www.gatsbyjs.org/docs/gatsby-config/
*/
module.exports = {
plugins: [],
}`
} else {
throw e
}
}

return src
}

const create = async ({ root }, { name, options, key }) => {
const configSrc = await readConfigFile(root)
const prettierConfig = await prettier.resolveConfig(root)

let code = addPluginToConfig(configSrc, { name, options, key })
code = prettier.format(code, { ...prettierConfig, parser: `babel` })

await fs.writeFile(configPath, code)
await fs.writeFile(getConfigPath(root), code)

return await read({ root }, key || name)
}

const read = async ({ root }, id) => {
try {
const configPath = path.join(root, `gatsby-config.js`)
const configSrc = await fs.readFile(configPath, `utf8`)
const configSrc = await readConfigFile(root)

const plugin = getPluginsFromConfig(configSrc).find(
plugin => plugin.key === id || plugin.name === id
Expand All @@ -154,8 +176,7 @@ const read = async ({ root }, id) => {
}

const destroy = async ({ root }, { id, name }) => {
const configPath = path.join(root, `gatsby-config.js`)
const configSrc = await fs.readFile(configPath, `utf8`)
const configSrc = await readConfigFile(root)

const addPlugins = new BabelPluginAddPluginsToGatsbyConfig({
pluginOrThemeName: name,
Expand All @@ -168,7 +189,7 @@ const destroy = async ({ root }, { id, name }) => {
configFile: false,
})

await fs.writeFile(configPath, code)
await fs.writeFile(getConfigPath(root), code)
}

class BabelPluginAddPluginsToGatsbyConfig {
Expand Down Expand Up @@ -289,9 +310,8 @@ module.exports.destroy = destroy
module.exports.config = {}

module.exports.all = async ({ root }) => {
const configPath = path.join(root, `gatsby-config.js`)
const src = await fs.readFile(configPath, `utf8`)
const plugins = getPluginsFromConfig(src)
const configSrc = await readConfigFile(root)
const plugins = getPluginsFromConfig(configSrc)

// TODO: Consider mapping to read function
return plugins.map(name => {
Expand Down Expand Up @@ -338,15 +358,14 @@ exports.validate = validate

module.exports.plan = async ({ root }, { id, key, name, options }) => {
const fullName = id || name
const configPath = path.join(root, `gatsby-config.js`)
const prettierConfig = await prettier.resolveConfig(root)
let src = await fs.readFile(configPath, `utf8`)
src = prettier.format(src, {
let configSrc = await readConfigFile(root)
configSrc = prettier.format(configSrc, {
...prettierConfig,
parser: `babel`,
})

let newContents = addPluginToConfig(src, {
let newContents = addPluginToConfig(configSrc, {
id,
key: id || key,
name: fullName,
Expand All @@ -356,13 +375,13 @@ module.exports.plan = async ({ root }, { id, key, name, options }) => {
...prettierConfig,
parser: `babel`,
})
const diff = await getDiff(src, newContents)
const diff = await getDiff(configSrc, newContents)

return {
id: fullName,
name,
diff,
currentState: src,
currentState: configSrc,
newState: newContents,
describe: `Install ${fullName} in gatsby-config.js`,
}
Expand Down
11 changes: 11 additions & 0 deletions packages/gatsby-recipes/src/providers/gatsby/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ describe(`gatsby-plugin resource`, () => {
let starterBlogRoot
let helloWorldRoot
let configPath
let emptyRoot
beforeAll(async () => {
tmpDir = await tmp.dir({
unsafeCleanup: true,
})
starterBlogRoot = path.join(tmpDir.path, `gatsby-starter-blog`)
helloWorldRoot = path.join(tmpDir.path, `gatsby-starter-hello-world`)
configPath = path.join(helloWorldRoot, `gatsby-config.js`)
emptyRoot = path.join(tmpDir.path, `empty-site-directory`)
await fs.ensureDir(emptyRoot)
await fs.ensureDir(starterBlogRoot)
await fs.copy(STARTER_BLOG_FIXTURE, starterBlogRoot)
await fs.ensureDir(helloWorldRoot)
Expand Down Expand Up @@ -166,4 +169,12 @@ describe(`gatsby-plugin resource`, () => {

expect(result).toMatchSnapshot()
})

test(`creates default gatsby-config.js if there isn't one already`, async () => {
const result = await plugin.create(
{ root: emptyRoot },
{ name: `gatsby-plugin-foo` }
)
expect(result).toMatchSnapshot()
})
})

0 comments on commit 293e171

Please sign in to comment.