node-firestore-import-export
Version:
Firestore data import and export
91 lines (90 loc) • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const colors = require("colors");
const process = require("process");
const fs = require("fs");
const export_1 = require("../lib/export");
const firestore_helpers_1 = require("../lib/firestore-helpers");
const packageInfo = require('../../package.json');
const accountCredentialsPathParamKey = 'accountCredentials';
const accountCredentialsPathParamDescription = 'Google Cloud account credentials JSON file';
const backupFileParamKey = 'backupFile';
const backupFileParamDescription = 'Filename to store backup. (e.g. backups/full-backup.json)';
const nodePathParamKey = 'nodePath';
const nodePathParamDescription = 'Path to database node to start (e.g. collectionA/docB/collectionC). ' +
'Backs up entire database from the root if missing';
const prettyPrintParamKey = 'prettyPrint';
const prettyPrintParamDescription = 'JSON backups done with pretty-printing.';
commander.version(packageInfo.version)
.option(`-a, --${accountCredentialsPathParamKey} <path>`, accountCredentialsPathParamDescription)
.option(`-b, --${backupFileParamKey} <path>`, backupFileParamDescription)
.option(`-n, --${nodePathParamKey} <path>`, nodePathParamDescription)
.option(`-p, --${prettyPrintParamKey}`, prettyPrintParamDescription)
.parse(process.argv);
const accountCredentialsPath = commander[accountCredentialsPathParamKey];
if (!accountCredentialsPath) {
console.log(colors.bold(colors.red('Missing: ')) + colors.bold(accountCredentialsPathParamKey) + ' - ' + accountCredentialsPathParamDescription);
commander.help();
process.exit(1);
}
if (!fs.existsSync(accountCredentialsPath)) {
console.log(colors.bold(colors.red('Account credentials file does not exist: ')) + colors.bold(accountCredentialsPath));
commander.help();
process.exit(1);
}
const backupPath = commander[backupFileParamKey] || 'firebase-export.json';
if (!backupPath) {
console.log(colors.bold(colors.red('Missing: ')) + colors.bold(backupFileParamKey) + ' - ' + backupFileParamDescription);
commander.help();
process.exit(1);
}
const writeResults = (results, filename) => {
return new Promise((resolve, reject) => {
fs.writeFile(filename, results, 'utf8', err => {
if (err) {
reject(err);
}
else {
resolve(filename);
}
});
});
};
const prettyPrint = commander[prettyPrintParamKey] !== undefined && commander[prettyPrintParamKey] !== null;
const nodePath = commander[nodePathParamKey];
firestore_helpers_1.getCredentialsFromFile(accountCredentialsPath)
.then(credentials => {
const db = firestore_helpers_1.getFirestoreDBReference(credentials);
const pathReference = firestore_helpers_1.getDBReferenceFromPath(db, nodePath);
return pathReference;
})
.then(pathReference => export_1.default(pathReference))
.then(results => {
let stringResults;
if (prettyPrint) {
stringResults = JSON.stringify(results, null, 2);
}
else {
stringResults = JSON.stringify(results);
}
return stringResults;
})
.then((dataToWrite) => writeResults(dataToWrite, backupPath))
.then((filename) => {
console.log(colors.yellow(`Results were saved to ${filename}`));
return;
})
.then(() => {
console.log(colors.bold(colors.green('All done 🎉')));
})
.catch((error) => {
if (error instanceof Error) {
console.log(colors.red(error.message));
process.exit(1);
}
else {
console.log(colors.red(error));
}
});