scratch-www/bin/get-localized-urls
Matthew Taylor 2dd82e858e use writeFileSync
Thanks @thisandagain!
2017-11-16 09:58:08 -05:00

48 lines
1.3 KiB
JavaScript
Executable file

#!/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);
});