@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
58 lines • 1.75 kB
JavaScript
import fs from 'fs';
import path from 'path';
const MAPPING_FILE = '.atlas-export.json';
const getMappingFilePath = () => path.join(process.cwd(), 'functions', 'atlas-export', MAPPING_FILE);
export const readCollectionMappings = () => {
const mappingFilePath = getMappingFilePath();
if (!fs.existsSync(mappingFilePath)) {
return {};
}
try {
const content = fs.readFileSync(mappingFilePath, 'utf-8');
const data = JSON.parse(content);
const collections = data?.collections ?? {};
const normalizedCollections = {};
for (const [key, value] of Object.entries(collections)) {
if (typeof value === 'string') {
normalizedCollections[key] = {
sanitizedName: key,
collectionName: value
};
} else {
normalizedCollections[key] = value;
}
}
return normalizedCollections;
} catch (error) {
console.error('Error reading collection mappings:', error);
return {};
}
};
export const writeCollectionMapping = (sanitizedName, originalName) => {
const mappingFilePath = getMappingFilePath();
const mappingsDir = path.dirname(mappingFilePath);
if (!fs.existsSync(mappingsDir)) {
fs.mkdirSync(mappingsDir, {
recursive: true
});
}
let data = {
collections: {}
};
if (fs.existsSync(mappingFilePath)) {
try {
const content = fs.readFileSync(mappingFilePath, 'utf-8');
data = JSON.parse(content);
} catch (error) {
console.error('Error reading existing mappings:', error);
}
}
if (!data.collections) {
data.collections = {};
}
data.collections[originalName] = {
sanitizedName,
collectionName: originalName
};
fs.writeFileSync(mappingFilePath, JSON.stringify(data, null, 4));
};