Skip to content

Commit

Permalink
[gatsby-source-filesystem] Add AllFiles to directory queries gatsbyjs…
Browse files Browse the repository at this point in the history
…#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
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 283 deletions.
23 changes: 12 additions & 11 deletions packages/gatsby-source-filesystem/package.json
Expand Up @@ -7,21 +7,21 @@
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"@babel/runtime": "^7.0.0-beta.42",
"bluebird": "^3.5.0",
"chokidar": "^1.7.0",
"@babel/runtime": "latest",
"chokidar": "^2.0.3",
"fs-extra": "^5.0.0",
"got": "^7.1.0",
"md5-file": "^3.1.1",
"mime": "^2.2.0",
"got": "^8.3.0",
"md5-file": "^4.0.0",
"mime": "^2.3.1",
"pretty-bytes": "^4.0.2",
"slash": "^1.0.0",
"slash": "^2.0.0",
"valid-url": "^1.0.9",
"xstate": "^3.1.0"
"xstate": "^3.1.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.42",
"@babel/core": "^7.0.0-beta.42",
"@babel/cli": "latest",
"@babel/core": "latest",
"@babel/plugin-proposal-object-rest-spread": "latest",
"cross-env": "^5.1.4"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem#readme",
Expand All @@ -30,10 +30,11 @@
"gatsby-plugin"
],
"license": "MIT",
"main": "index.js",
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore __tests__"
}
}
133 changes: 64 additions & 69 deletions packages/gatsby-source-filesystem/src/create-file-node.js
@@ -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
66 changes: 29 additions & 37 deletions packages/gatsby-source-filesystem/src/extend-file-node.js
Expand Up @@ -2,44 +2,36 @@ const { GraphQLString } = require(`graphql`)
const fs = require(`fs-extra`)
const path = require(`path`)

module.exports = ({ type, getNodeAndSavePathDependency, pathPrefix = `` }) => {
if (type.name !== `File`) {
return {}
}
const staticPath = path.join(process.cwd(), `public`, `static`)

return {
publicURL: {
type: GraphQLString,
args: {},
description: `Copy file to static directory and return public url to it`,
resolve: (file, fieldArgs, context) => {
const details = getNodeAndSavePathDependency(file.id, context.path)
const fileName = `${file.name}-${file.internal.contentDigest}${
details.ext
}`
module.exports = ({ getNodeAndSavePathDependency, pathPrefix = ``, type }) =>
type.name === `File`
? {
publicURL: {
args: {},
description: `Copy file to static directory and return its public URL`,
resolve: (file, _fieldArgs, context) => {
const details = getNodeAndSavePathDependency(file.id, context.path)
const digest = file.internal.contentDigest.slice(0, 6)
const fileName = `${file.name}-${digest}${details.ext}`
const publicPath = path.join(staticPath, fileName)

const publicPath = path.join(
process.cwd(),
`public`,
`static`,
fileName
)

if (!fs.existsSync(publicPath)) {
fs.copy(details.absolutePath, publicPath, err => {
if (err) {
console.error(
`error copying file from ${
details.absolutePath
} to ${publicPath}`,
err
)
if (!fs.existsSync(publicPath)) {
fs.copy(details.absolutePath, publicPath, err => {
if (err) {
console.error(
`error copying "${
details.absolutePath
}" to "${publicPath}"`,
err
)
}
})
}
})
}

return `${pathPrefix}/static/${fileName}`
},
},
}
}
return `${pathPrefix}/static/${fileName}`
},
type: GraphQLString,
},
}
: {}

0 comments on commit c7bec93

Please sign in to comment.