scratch-www/bin/get-localized-urls

49 lines
1.3 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env node
/*
Get localized-urls.json from resources.scratch.mit.edu
*/
var https = require('https');
var fs = require('fs');
var path = require('path');
var urlData = '';
var args = process.argv.slice(2);
if (!args.length) {
process.stdout.write('A output filename must be specified.\n');
process.exit(1);
}
var filename = path.resolve(__dirname, 'lib', args.shift());
https.get('https://resources.scratch.mit.edu/localized-urls.json', (res) => {
var statusCode = res && res.statusCode;
var error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
}
if (error) {
process.stdout.write(`${error.message}\n`);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
res.on('data', (chunk) => { urlData += chunk; });
res.on('end', () => {
try {
var urlJson = JSON.parse(urlData);
fs.writeFileSync(filename, JSON.stringify(urlJson, null, ' '));
} catch (e) {
process.stdout.write(`Failed parsing url data: ${e.message}\n`);
process.exit(1);
}
});
}).on('error', (e) => {
process.stdout.write(`Got error: ${e.message}\n`);
process.exit(1);
});