@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
84 lines • 3.16 kB
JavaScript
import fs from 'fs';
import * as features from '../../utils/feature.js';
import { logger } from '../../utils/index.js';
import { resolveSearchConfigLocation } from './config/searchConfig.js';
import deploySearch, { generateSearchRuntimeConfig } from './deploy.js';
export const runSearchApply = async (options = {}, dependencies = {}, cwd = process.cwd()) => {
if (options.generateConfigOnly !== true) {
return (dependencies.runSearchDeploy ?? deploySearch)(undefined, options, dependencies);
}
const {
existsSyncImpl = fs.existsSync,
generateSearchRuntimeConfigImpl = generateSearchRuntimeConfig,
loadFeatureContextImpl = features.loadFeatureContext,
loggerImpl = logger,
resolveProjectSelectionImpl = features.resolveProjectSelection
} = dependencies;
const shouldLog = options.log !== false;
const projectSelection = await resolveProjectSelectionImpl(options);
const configLocation = resolveSearchConfigLocation(cwd, {
existsSyncImpl
});
if (!configLocation.hasPreferred) {
if (shouldLog) {
loggerImpl.info(`Atlas search config is not configured for project ${projectSelection.projectId}. Skipping runtime config generation.`);
}
return {
projectId: projectSelection.projectId,
reason: 'missing-config',
status: 'skipped'
};
}
const spinner = shouldLog ? loggerImpl.spinner('Generating Atlas search runtime config...') : null;
try {
const context = await loadFeatureContextImpl('search', options, {
cwd,
resolveProjectSelectionImpl: async () => projectSelection
});
const generationResult = generateSearchRuntimeConfigImpl(context, {
dryRun: options.dryRun === true,
runCommand: dependencies.runCommand,
writeGeneratedFeatureConfig: dependencies.writeGeneratedFeatureConfig
}, cwd);
if (shouldLog) {
spinner?.succeed(options.dryRun === true ? 'Atlas search runtime config dry run is ready.' : 'Atlas search runtime config generated.');
loggerImpl.summary('Atlas search runtime config', [{
label: 'Project',
value: generationResult.projectId
}, context.environment ? {
label: 'Environment',
value: context.environment
} : null, {
label: 'Config path',
value: generationResult.runtimeConfigArtifact.filePath
}, {
label: 'Region',
value: generationResult.region
}, {
label: 'API URL',
value: generationResult.apiServiceUrl
}, {
label: 'Sync URL',
value: generationResult.syncServiceUrl
}]);
if (options.dryRun === true) {
loggerImpl.info('Dry run requested: Atlas did not write the generated runtime config.');
}
}
return {
...generationResult,
environment: context.workloadProjectConfig?.environment ?? context.environment ?? null
};
} catch (error) {
if (shouldLog) {
spinner?.fail('Atlas search runtime config generation failed.');
loggerImpl.error(error.message);
}
return {
error,
projectId: projectSelection.projectId,
status: 'failed'
};
}
};
export default runSearchApply;