forked from gatsbyjs/gatsby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gatsby-source-filesystem] Add AllFiles to directory queries gatsbyjs…
…#3727 Signed-off-by: Peter T Bosse II <ptb@ioutime.com>
- Loading branch information
Peter T Bosse II
committed
Apr 16, 2018
1 parent
f086f7e
commit c7bec93
Showing
5 changed files
with
207 additions
and
283 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
133 changes: 64 additions & 69 deletions
133
packages/gatsby-source-filesystem/src/create-file-node.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,79 +1,74 @@ | ||
const slash = require(`slash`) | ||
const path = require(`path`) | ||
const crypto = require(`crypto`) | ||
const fs = require(`fs-extra`) | ||
const mime = require(`mime`) | ||
const path = require(`path`) | ||
const md5File = require(`md5-file/promise`) | ||
const prettyBytes = require(`pretty-bytes`) | ||
const slash = require(`slash`) | ||
|
||
const md5File = require(`bluebird`).promisify(require(`md5-file`)) | ||
const crypto = require(`crypto`) | ||
const createFileNode = async (src, createNodeId, opts = {}) => { | ||
let node = { | ||
absolutePath: slash(src), | ||
children: [], | ||
cwd: slash(opts.path || process.cwd()), | ||
id: createNodeId(src), | ||
parent: `___SOURCE___`, | ||
sourceInstanceName: opts.name || `__PROGRAMATTIC__`, | ||
} | ||
|
||
const stats = await fs.stat(node.absolutePath) | ||
|
||
exports.createFileNode = async ( | ||
pathToFile, | ||
createNodeId, | ||
pluginOptions = {} | ||
) => { | ||
const slashed = slash(pathToFile) | ||
const parsedSlashed = path.parse(slashed) | ||
const slashedFile = { | ||
...parsedSlashed, | ||
absolutePath: slashed, | ||
// Useful for limiting graphql query with certain parent directory | ||
relativeDirectory: path.relative( | ||
pluginOptions.path || process.cwd(), | ||
parsedSlashed.dir | ||
), | ||
node = { | ||
...node, | ||
...path.parse(node.absolutePath), | ||
...stats, | ||
absPath: slash(path.resolve(node.absolutePath)), | ||
relativePath: slash(path.relative(node.cwd, node.absolutePath)), | ||
} | ||
|
||
const stats = await fs.stat(slashedFile.absolutePath) | ||
let internal | ||
if (stats.isDirectory()) { | ||
const contentDigest = crypto | ||
.createHash(`md5`) | ||
.update( | ||
JSON.stringify({ stats: stats, absolutePath: slashedFile.absolutePath }) | ||
) | ||
.digest(`hex`) | ||
internal = { | ||
contentDigest, | ||
type: `Directory`, | ||
} | ||
} else { | ||
const contentDigest = await md5File(slashedFile.absolutePath) | ||
const mediaType = mime.getType(slashedFile.ext) | ||
internal = { | ||
contentDigest, | ||
type: `File`, | ||
mediaType: mediaType ? mediaType : `application/octet-stream`, | ||
} | ||
node = { | ||
...node, | ||
accessTime: node.atime, | ||
birthTime: node.birthtime, | ||
changeTime: node.ctime, | ||
extension: node.ext.slice(1).toLowerCase(), | ||
internal: stats.isDirectory() | ||
? { | ||
contentDigest: crypto | ||
.createHash(`md5`) | ||
.update( | ||
JSON.stringify({ | ||
absolutePath: node.absolutePath, | ||
stats: stats, | ||
}) | ||
) | ||
.digest(`hex`), | ||
type: `Directory`, | ||
} | ||
: { | ||
contentDigest: await md5File(node.absolutePath), | ||
mediaType: mime.getType(node.ext) || `application/octet-stream`, | ||
type: `File`, | ||
}, | ||
modifiedTime: node.mtime, | ||
prettySize: prettyBytes(node.size), | ||
relativeDirectory: path.relative(node.cwd, node.dir), | ||
} | ||
|
||
// Stringify date objects. | ||
return JSON.parse( | ||
JSON.stringify({ | ||
// Don't actually make the File id the absolute path as otherwise | ||
// people will use the id for that and ids shouldn't be treated as | ||
// useful information. | ||
id: createNodeId(pathToFile), | ||
children: [], | ||
parent: `___SOURCE___`, | ||
internal, | ||
sourceInstanceName: pluginOptions.name || `__PROGRAMATTIC__`, | ||
absolutePath: slashedFile.absolutePath, | ||
relativePath: slash( | ||
path.relative( | ||
pluginOptions.path || process.cwd(), | ||
slashedFile.absolutePath | ||
) | ||
), | ||
extension: slashedFile.ext.slice(1).toLowerCase(), | ||
size: stats.size, | ||
prettySize: prettyBytes(stats.size), | ||
modifiedTime: stats.mtime, | ||
accessTime: stats.atime, | ||
changeTime: stats.ctime, | ||
birthTime: stats.birthtime, | ||
...slashedFile, | ||
...stats, | ||
}) | ||
) | ||
node = { | ||
...node, | ||
allFile: { | ||
edges: stats.isDirectory() | ||
? await fs | ||
.readdir(node.absolutePath) | ||
.map(file => path.join(node.absolutePath, file)) | ||
.filter(file => fs.statSync(file).isFile()) | ||
.map(file => {return { node___NODE: createNodeId(file) }}) | ||
: [], | ||
}, | ||
} | ||
|
||
return JSON.parse(JSON.stringify(node)) | ||
} | ||
|
||
module.exports = createFileNode |
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
Oops, something went wrong.