Created
February 1, 2019 09:54
-
-
Save kmelve/c2ad73fcdc149523d666c56480a57d6f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 axios = require("axios"); | |
| const sanityClient = require('@sanity/client') | |
| const client = token => sanityClient({ | |
| projectId: '3do82whm', | |
| dataset: 'production', | |
| token | |
| }) | |
| const NPM_API_SEARCH = "https://api.npms.io/v2/search/suggestions" | |
| const NPM_API_PGKINFO = "https://api.npms.io/v2/package/" | |
| function checkIfPlugin({package: {name}}) { | |
| return name.match(/^sanity-plugin/) | |
| } | |
| function getReadmes(pkgs) { | |
| return pkgs | |
| .filter(checkIfPlugin) | |
| .map(({ package }) => { | |
| return axios(NPM_API_PGKINFO + package.name) | |
| .then(({data: { collected: { metadata }}}) => ({ name: metadata.name, readme: metadata.readme})) | |
| }) | |
| } | |
| module.exports = async function(context, cb) { | |
| const pkgs = await axios(NPM_API_SEARCH + '?q=sanity&size=100').then(({data}) => data) | |
| console.log(`Fetched ${pkgs.length} plugins`) | |
| const readMeResults = await Promise.all(getReadmes(pkgs)) | |
| const readMesMap = readMeResults.reduce((acc, curr) => ({ ...acc, [curr.name]: curr.readme }), {}) | |
| const preparedResults = pkgs | |
| .filter(checkIfPlugin) | |
| .map(({ package }) => ({ | |
| _id: package.name, | |
| _type: 'plugin', | |
| installWith: package.name.split('sanity-plugin-')[1], | |
| name: package.name, | |
| npm: { | |
| ...package, | |
| _type: 'npm', | |
| links: { | |
| _type: 'pkgLinks', | |
| ...package.links | |
| }, | |
| readme: readMesMap[package.name], | |
| publisher: { | |
| _type: "publisher", | |
| ...(package.publisher && {pluginAuthor: { | |
| _type: 'reference', | |
| _ref: `pluginAuthor-${(package.publisher || {}).username}`, | |
| _weak: true | |
| }, | |
| ...package.publisher | |
| }), | |
| maintainers: package.maintainers.map((maintainer, i) => ({ | |
| _type: "pkgMaintainer", | |
| _key: 'pkgMaintainer' + i + maintainer.username, | |
| ...maintainer | |
| })) | |
| } | |
| })) | |
| const res = await preparedResults | |
| .reduce((trans, doc) => | |
| trans | |
| .createIfNotExists(doc) | |
| .patch(doc._id, patch => | |
| patch.setIfMissing({npm: {}}).set({npm: doc.npm}) | |
| ), | |
| client(context.secrets.API_TOKEN).transaction() | |
| ).commit().catch(() => cb(null, 500)) | |
| cb(null, 200) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment