Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save camjc/cab25c7432230d125e07f60172ab11b6 to your computer and use it in GitHub Desktop.
Save camjc/cab25c7432230d125e07f60172ab11b6 to your computer and use it in GitHub Desktop.
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