Last active
February 2, 2020 14:44
-
-
Save d4rekanguok/cc54bf49f46246b19ced4a03292a0012 to your computer and use it in GitHub Desktop.
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
const fs = require('fs') | |
const path = require('path') | |
const toUpperCase = ([first, ...rest]) => [first.toUpperCase(), ...rest].join('') | |
exports.sourceNodes = ({ actions, createContentDigest, store }) => { | |
const { directory } = store.getState().program | |
const raw = fs.readFileSync(path.join(directory, 'package.json')) | |
const content = JSON.parse(raw) | |
;['dependencies', 'devDependencies'].forEach(field => { | |
const newFieldName = 'all' + toUpperCase(field) | |
content[newFieldName] = Object | |
.keys(content[field]) | |
.map(key => ({ name: key, version: content[field][key] })) | |
}) | |
actions.createNode({ | |
...content, | |
id: 'package-json', | |
internal: { | |
contentDigest: createContentDigest(content), | |
type: 'PackageJson', | |
}, | |
}) | |
} | |
/** | |
localhost:8000/___graphql | |
{ | |
packageJson { | |
allDependencies { | |
name | |
version | |
} | |
allDevDependencies { | |
name | |
version | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment