@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
92 lines • 3.03 kB
JavaScript
import inquirer from 'inquirer';
import { getRequiredFirebasercProjects } from '../../utils/firebase.js';
import { getFirestoreLocations } from '../../utils/gcloud.js';
import { logger } from '../../utils/logger.js';
import { execSync } from '../../utils/index.js';
const DATABASES = ['session', 'cache'];
const normalizeLocationLabel = location => location.split(' ')[0].toLowerCase();
export const confirmCreateFirestoreDatabase = async ({
database,
execSyncImpl = execSync,
getFirestoreLocationsImpl = getFirestoreLocations,
loggerImpl = logger,
projectId,
promptImpl = inquirer.prompt
}) => {
const {
shouldCreate
} = await promptImpl([{
type: 'confirm',
name: 'shouldCreate',
default: false,
message: `Do you want to create the Firestore database ${database} ` + `for project ${projectId}?`
}]);
if (!shouldCreate) {
return 'skipped';
}
const locations = await getFirestoreLocationsImpl(projectId);
const {
location
} = await promptImpl([{
type: 'select',
name: 'location',
default: locations.find(loc => loc.startsWith('europe-west1')) || locations[0],
message: `Select the region for the Firestore database ${database}.\n` + 'Choose the same region where App Engine is deployed to reduce latency.',
choices: locations
}]);
const {
deleteProtection
} = await promptImpl([{
type: 'select',
name: 'deleteProtection',
default: 'ENABLED',
message: `Enable delete protection for the Firestore database ${database}:`,
choices: ['ENABLED', 'DISABLED']
}]);
const spinner = loggerImpl.spinner(`Creating Firestore database ${database}...`);
try {
await execSyncImpl(`firebase firestore:databases:create ${database}`, {
location: normalizeLocationLabel(location),
stdio: 'inherit',
project: projectId,
'delete-protection': deleteProtection
});
} catch (error) {
spinner.fail(`Failed to create Firestore database ${database}: ${error.message}`);
throw error;
}
spinner.succeed(`Firestore database ${database} created successfully.`);
return 'created';
};
export default async (dependencies = {}) => {
const {
execSync: execSyncImpl = execSync,
getFirestoreLocations: getFirestoreLocationsImpl = getFirestoreLocations,
getRequiredFirebasercProjects: getRequiredFirebasercProjectsImpl = getRequiredFirebasercProjects,
logger: loggerImpl = logger,
prompt: promptImpl = inquirer.prompt
} = dependencies;
const {
developmentProjectId,
productionProjectId
} = getRequiredFirebasercProjectsImpl();
for (const database of DATABASES) {
await confirmCreateFirestoreDatabase({
database,
execSyncImpl,
getFirestoreLocationsImpl,
loggerImpl,
projectId: productionProjectId,
promptImpl
});
await confirmCreateFirestoreDatabase({
database,
execSyncImpl,
getFirestoreLocationsImpl,
loggerImpl,
projectId: developmentProjectId,
promptImpl
});
}
return true;
};