Created
October 13, 2019 22:49
-
-
Save camjc/cab25c7432230d125e07f60172ab11b6 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 parallelLimit = require("async/parallelLimit"); | |
const fs = require("fs"); | |
const glob = require("glob"); | |
const sanityClient = require("@sanity/client"); | |
const client = sanityClient({ | |
projectId: 'get a project id', // TODO: projectId needed | |
dataset: "production", | |
token: 'get a write token', // TODO: token needed | |
useCdn: false | |
}); | |
var getDirectories = (src, callback) => { | |
glob(src + "/**/*.*", callback); | |
}; | |
getDirectories("files", (err, res) => { | |
if (err) { | |
console.log("Error", err); | |
} else { | |
const tasks = res.map((fileName, index) => cb => { | |
client.assets | |
.upload(getUploadType(fileName), fs.createReadStream("./" + fileName)) | |
.then(data => { | |
console.log(index, 'of', res.length); | |
cb(null, data); | |
}); | |
}); | |
parallelLimit(tasks, 24, arrayOfResults => { | |
console.log(arrayOfResults); | |
}); | |
} | |
}); | |
function getUploadType(fileName) { | |
if (fileName.endsWith(".png") || fileName.endsWith(".jpg")) { | |
return "image"; | |
} | |
return "file"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment