-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Don't overwrite files by default * Handle object style plugin config * Handle object style plugin config * Update todo * Use fs-extra, improve plugin config tests * Fix file check * Remove overwrite logic, this will be part of the plan step
- Loading branch information
Showing
7 changed files
with
139 additions
and
57 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 |
---|---|---|
@@ -1,35 +1,42 @@ | ||
const fs = require(`fs`) | ||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
const { promisify } = require(`util`) | ||
const mkdirp = require(`mkdirp`) | ||
|
||
const readFile = promisify(fs.readFile) | ||
const writeFile = promisify(fs.writeFile) | ||
const destroyFile = promisify(fs.unlink) | ||
const fileExists = ({ root }, { path: filePath }) => { | ||
const fullPath = path.join(root, filePath) | ||
try { | ||
fs.accessSync(fullPath, fs.constants.F_OK) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
const create = async ({ root }, { path: filePath, content, children }) => { | ||
const create = async ({ root }, { path: filePath, content }) => { | ||
const fullPath = path.join(root, filePath) | ||
const { dir } = path.parse(fullPath) | ||
|
||
content = content || children | ||
|
||
await mkdirp(dir) | ||
await writeFile(fullPath, content) | ||
await fs.writeFile(fullPath, content) | ||
} | ||
|
||
const update = create | ||
|
||
const read = async ({ root }, { path: filePath }) => { | ||
const fullPath = path.join(root, filePath) | ||
const content = await readFile(fullPath, `utf8`) | ||
const content = await fs.readFile(fullPath, `utf8`) | ||
|
||
return { content } | ||
} | ||
|
||
const destroy = async ({ root }, { path: filePath }) => { | ||
const fullPath = path.join(root, filePath) | ||
await destroyFile(fullPath) | ||
await fs.unlink(fullPath) | ||
} | ||
|
||
module.exports.exists = fileExists | ||
|
||
module.exports.create = create | ||
module.exports.update = create | ||
module.exports.update = update | ||
module.exports.read = read | ||
module.exports.destroy = destroy |
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,30 @@ | ||
const file = require(`./file`) | ||
|
||
const root = __dirname | ||
const content = `Hello, world!` | ||
|
||
test(`create writes a file`, async () => { | ||
const filePath = `bar.txt` | ||
|
||
await file.create({ root }, { path: filePath, content }) | ||
|
||
const result = await file.read({ root }, { path: filePath }) | ||
|
||
expect(result.content).toEqual(content) | ||
|
||
await file.destroy({ root }, { path: filePath }) | ||
}) | ||
|
||
test(`update overwrites a file`, async () => { | ||
const filePath = `bar.txt` | ||
const newContent = `new content!` | ||
|
||
await file.create({ root }, { path: filePath, content }) | ||
await file.update({ root }, { path: filePath, content: newContent }) | ||
|
||
const result = await file.read({ root }, { path: filePath }) | ||
|
||
expect(result.content).toEqual(newContent) | ||
|
||
await file.destroy({ root }, { path: filePath }) | ||
}) |
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
35 changes: 28 additions & 7 deletions
35
packages/gatsby/src/recipes/providers/gatsby/plugin.test.js
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,14 +1,35 @@ | ||
const fs = require(`fs`) | ||
const path = require(`path`) | ||
|
||
const { addPluginToConfig } = require(`./plugin`) | ||
const plugin = require(`./plugin`) | ||
|
||
const CONFIG_PATH = path.join(__dirname, `./fixtures/gatsby-config.js`) | ||
const root = path.join(__dirname, `./fixtures`) | ||
const name = `gatsby-plugin-foo` | ||
|
||
test(`babel plugin to get list of plugins and their options!`, () => { | ||
const src = fs.readFileSync(CONFIG_PATH, `utf8`) | ||
describe(`gatsby-config`, () => { | ||
test(`adds a plugin to a gatsby config`, async () => { | ||
await plugin.create({ root }, { name }) | ||
|
||
const result = addPluginToConfig(src, `gatsby-plugin-foo`) | ||
const result = await plugin.read({ root }) | ||
|
||
expect(result).toMatch(`gatsby-plugin-foo`) | ||
expect(result).toContain(`gatsby-plugin-foo`) | ||
|
||
await plugin.destroy({ root }, { name }) | ||
}) | ||
|
||
test(`retrieves plugins from a config`, async () => { | ||
const result = await plugin.read({ root }) | ||
|
||
expect(result).toEqual([ | ||
`gatsby-source-filesystem`, | ||
`gatsby-transformer-sharp`, | ||
`gatsby-plugin-emotion`, | ||
`gatsby-plugin-typography`, | ||
`gatsby-transformer-remark`, | ||
`gatsby-plugin-sharp`, | ||
`gatsby-plugin-google-analytics`, | ||
`gatsby-plugin-manifest`, | ||
`gatsby-plugin-offline`, | ||
`gatsby-plugin-react-helmet`, | ||
]) | ||
}) | ||
}) |
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