UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

74 lines 3.76 kB
import fs from 'fs'; import path from 'path'; import { readJsonFile, writeJsonFile } from './file.js'; export const ATLAS_GENERATED_DIR = '.atlas'; export const ATLAS_GENERATED_CACHE_DIR = path.join(ATLAS_GENERATED_DIR, 'cache'); export const ATLAS_GENERATED_SERVER_DIR = path.join(ATLAS_GENERATED_DIR, 'server'); export const ATLAS_GENERATED_ARTIFACTS_DIR = path.join(ATLAS_GENERATED_CACHE_DIR, 'artifacts'); export const ATLAS_GENERATED_CONFIG_DIR = path.join(ATLAS_GENERATED_SERVER_DIR, 'config'); export const ATLAS_GENERATED_TERRAFORM_DIR = path.join(ATLAS_GENERATED_CACHE_DIR, 'terraform'); export const resolveRootPath = (fileName, cwd = process.cwd()) => path.join(cwd, fileName); export const getAtlasWorkloadDirectoryPath = (workloadName, cwd = process.cwd()) => resolveRootPath(path.join('services', workloadName), cwd); export const resolveAtlasWorkloadFile = (workloadName, relativeFilePath, cwd = process.cwd(), dependencies = {}) => { const { existsSyncImpl = fs.existsSync } = dependencies; const preferredPath = path.join(getAtlasWorkloadDirectoryPath(workloadName, cwd), relativeFilePath); const hasPreferred = existsSyncImpl(preferredPath); return { activePath: preferredPath, hasPreferred, preferredPath }; }; export const getAtlasGeneratedDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_DIR, cwd); export const getAtlasGeneratedArtifactsDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_ARTIFACTS_DIR, cwd); export const getAtlasGeneratedCacheDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_CACHE_DIR, cwd); export const getAtlasGeneratedServerDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_SERVER_DIR, cwd); export const getAtlasGeneratedConfigDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_CONFIG_DIR, cwd); export const getAtlasGeneratedTerraformDir = (cwd = process.cwd()) => resolveRootPath(ATLAS_GENERATED_TERRAFORM_DIR, cwd); export const getAtlasGeneratedFeatureConfigPath = (featureName, projectId, cwd = process.cwd()) => path.join(getAtlasGeneratedConfigDir(cwd), `${featureName}.${projectId}.json`); export const getAtlasFeatureCacheDir = (featureName, cwd = process.cwd()) => path.join(getAtlasGeneratedCacheDir(cwd), featureName); export const getAtlasFeatureCachePath = (featureName, projectId, cwd = process.cwd()) => path.join(getAtlasFeatureCacheDir(featureName, cwd), projectId, 'context'); export const ensureAtlasGeneratedStructure = (cwd = process.cwd()) => { const directories = [getAtlasGeneratedDir(cwd), getAtlasGeneratedCacheDir(cwd), getAtlasGeneratedServerDir(cwd)]; const createdDirectories = []; for (const directoryPath of directories) { if (!fs.existsSync(directoryPath)) { fs.mkdirSync(directoryPath, { recursive: true }); createdDirectories.push(directoryPath); } } return { createdDirectories, directories }; }; export const writeAtlasGeneratedFeatureConfig = (featureName, projectId, data, cwd = process.cwd()) => { ensureAtlasGeneratedStructure(cwd); const filePath = getAtlasGeneratedFeatureConfigPath(featureName, projectId, cwd); writeJsonFile(filePath, data); return { filePath }; }; export const writeAtlasFeatureCache = (featureName, projectId, data, cwd = process.cwd()) => { ensureAtlasGeneratedStructure(cwd); const filePath = getAtlasFeatureCachePath(featureName, projectId, cwd); writeJsonFile(filePath, data); return { filePath }; }; export const loadAtlasFeatureCache = (featureName, projectId, cwd = process.cwd()) => { const filePath = getAtlasFeatureCachePath(featureName, projectId, cwd); const cache = readJsonFile(filePath, { allowMissing: true }); return { cache, filePath }; };