Created
November 26, 2021 09:51
-
-
Save udovidio/15232c58def060cf343d7c848d6d2bf0 to your computer and use it in GitHub Desktop.
Download all schemas of schemastore.org
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 url = 'https://www.schemastore.org/api/json/catalog.json'; | |
const https = require('https'); | |
fs = require('fs'); | |
const directory = 'schemas'; | |
const downloadFile = (url) => { | |
return new Promise((resolve, reject) => { | |
https.get(url, (resp) => { | |
let html = ''; | |
// A chunk of data has been received. | |
resp.on('data', (chunk) => { | |
html += chunk; | |
}); | |
// The whole response has been received. Print out the result. | |
resp.on('end', () => { | |
resolve(html); | |
}); | |
}).on("error", (err) => { | |
console.log("Error: " + err.message); | |
}); | |
}); | |
} | |
const saveFile = (fileName, file) => { | |
fs.writeFile(`${directory}/${fileName}`, file, (err) => { | |
if (err) { | |
return console.error(err); | |
} | |
console.log(`file: ${fileName} saved`); | |
}); | |
} | |
const downloadAndSaveSchemas = (schemas) => { | |
schemas.forEach(schema => { | |
downloadFile(schema.url).then((file) => saveFile(schema.name, file)); | |
}); | |
} | |
downloadFile(url).then((html) => { | |
const json = JSON.parse(html); | |
console.log(json.schemas) | |
const schemas = json.schemas.map(s => { | |
return { | |
url: s.url, | |
name: s.name | |
} | |
}); | |
downloadAndSaveSchemas(schemas); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment