Below works but it looks like I'm missing little chunks of data. What's the best way to achieve what I'm trying to do here. I run node updateFiles.js 5.5.9
and it's lightning fast but like I said, when I eyeball the result file, it appears I miss some chunks...?
Below is the contents of updateFiles.js
.
const fs = require('fs');
const fetch = require('node-fetch');
var version = process.argv[2];
var filePath = 'source/js/firebase.js';
var sdks = [
'https://www.gstatic.com/firebasejs/' + version + '/firebase-app.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-auth.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-database.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-firestore.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-messaging.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-functions.js'
];
//fetch each firebase file and write it to source/js/firebase.js
for (var sdk of sdks){
fetch(sdk).then(res => {
const dest = fs.createWriteStream(filePath,{flags:'a'});
res.body.pipe(dest);
});
}
UPDATE: I ended up doing this and it seems to work, but, if there's a guru out there that can indicate a better way please let me know. Thank you in advance.
const fs = require('fs');
const fetch = require('node-fetch');
var version = process.argv[2];
var promisesArray = [];
var sdks = [
// Firebase App is always required and must be first
'https://www.gstatic.com/firebasejs/' + version + '/firebase-app.js',
// Add additional services that you want to use
'https://www.gstatic.com/firebasejs/' + version + '/firebase-auth.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-database.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-firestore.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-messaging.js',
'https://www.gstatic.com/firebasejs/' + version + '/firebase-functions.js'
];
var filePath = 'source/js/firebase.js';
for (var sdk of sdks){
promisesArray.push(fetch(sdk));
}
Promise.all(promisesArray).then(sdkFiles => {
for (var file of sdkFiles){
const dest = fs.createWriteStream(filePath,{flags:'a'});
file.body.pipe(dest);
}
});