@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
84 lines • 3.41 kB
JavaScript
import { ensureSyncConfigSection } from './config/syncConfig.js';
import { getFirebaserc, logger, normalizeOptionalString, resolveProjectSelection } from '../../utils/index.js';
const createSyncInitSummaryRows = (configResult, projectSelection) => [projectSelection ? {
label: 'Project',
value: projectSelection.projectId
} : null, projectSelection?.environment ? {
label: 'Environment',
value: projectSelection.environment
} : null, {
label: 'Config',
value: configResult.configPath
}, {
label: 'Workloads',
value: configResult.rootConfig.workloads?.length ?? 0
}, {
label: 'Project bindings',
value: Object.keys(configResult.projectConfig?.workloadBindings ?? {}).length
}];
const logSyncInitOutcome = (loggerImpl, {
configResult,
projectSelection
}) => {
loggerImpl.summary('Init summary', createSyncInitSummaryRows(configResult, projectSelection));
loggerImpl.summary('Created config files', configResult.createdFiles, {
emptyMessage: 'No new config files were created.'
});
loggerImpl.summary('Updated files', configResult.updatedFiles, {
emptyMessage: 'No files were updated.'
});
if (!projectSelection) {
loggerImpl.warning('Atlas sync config was scaffolded locally without a resolved project. ' + 'Re-run "atlas sync init" with --project or --environment once .firebaserc is available.');
}
};
export const resolveSyncInitProjectSelection = async (options, dependencies = {}, cwd = process.cwd()) => {
const getFirebasercImpl = dependencies.getFirebaserc ?? getFirebaserc;
const resolveProjectSelectionImpl = dependencies.resolveProjectSelection ?? resolveProjectSelection;
const explicitProject = normalizeOptionalString(options.project);
const explicitEnvironment = normalizeOptionalString(options.environment);
const hasExplicitProjectSelection = explicitProject !== null || explicitEnvironment !== null;
const firebaserc = getFirebasercImpl({
allowMissing: true
}, {
cwd
});
if (!hasExplicitProjectSelection && !firebaserc?.projects) {
return null;
}
return resolveProjectSelectionImpl({
...options,
environment: explicitEnvironment ?? undefined,
project: explicitProject ?? undefined
});
};
export const runSyncInit = async (options = {}, dependencies = {}, cwd = process.cwd()) => {
const loggerImpl = dependencies.logger ?? logger;
const ensureSyncConfigSectionImpl = dependencies.ensureSyncConfigSection ?? ensureSyncConfigSection;
const resolveProjectSelectionForInit = dependencies.resolveSyncInitProjectSelection ?? resolveSyncInitProjectSelection;
let spinner;
try {
const projectSelection = await resolveProjectSelectionForInit(options, dependencies, cwd);
const configResult = ensureSyncConfigSectionImpl(cwd, {
...(projectSelection ? {
environment: projectSelection.environment ?? undefined,
projectId: projectSelection.projectId
} : {})
});
spinner = loggerImpl.spinner('Initializing Atlas sync config...');
spinner.succeed('Atlas sync config is ready.');
logSyncInitOutcome(loggerImpl, {
configResult,
projectSelection
});
return {
configResult,
projectSelection,
status: 'ready'
};
} catch (error) {
spinner?.fail('Failed to initialize Atlas sync config.');
loggerImpl.error(error.message);
return null;
}
};
export default async options => runSyncInit(options);