A CLI for publishing standard.site documents to ATProto

chore: refactored mapping

+18 -19
+18 -19
packages/cli/src/commands/init.ts
··· 133 133 ); 134 134 135 135 // Build frontmatter mapping object 136 - let frontmatterMapping: FrontmatterMapping | undefined = {}; 136 + const fieldMappings: Array<[keyof FrontmatterMapping, string, string]> = [ 137 + ["title", frontmatterConfig.titleField, "title"], 138 + ["description", frontmatterConfig.descField, "description"], 139 + ["publishDate", frontmatterConfig.dateField, "publishDate"], 140 + ["coverImage", frontmatterConfig.coverField, "ogImage"], 141 + ["tags", frontmatterConfig.tagsField, "tags"], 142 + ]; 137 143 138 - if (frontmatterConfig.titleField !== "title") { 139 - frontmatterMapping.title = frontmatterConfig.titleField; 140 - } 141 - if (frontmatterConfig.descField !== "description") { 142 - frontmatterMapping.description = frontmatterConfig.descField; 143 - } 144 - if (frontmatterConfig.dateField !== "publishDate") { 145 - frontmatterMapping.publishDate = frontmatterConfig.dateField; 146 - } 147 - if (frontmatterConfig.coverField !== "ogImage") { 148 - frontmatterMapping.coverImage = frontmatterConfig.coverField; 149 - } 150 - if (frontmatterConfig.tagsField !== "tags") { 151 - frontmatterMapping.tags = frontmatterConfig.tagsField; 152 - } 144 + const builtMapping = fieldMappings.reduce<FrontmatterMapping>( 145 + (acc, [key, value, defaultValue]) => { 146 + if (value !== defaultValue) { 147 + acc[key] = value; 148 + } 149 + return acc; 150 + }, 151 + {}, 152 + ); 153 153 154 154 // Only keep frontmatterMapping if it has any custom fields 155 - if (Object.keys(frontmatterMapping).length === 0) { 156 - frontmatterMapping = undefined; 157 - } 155 + const frontmatterMapping = 156 + Object.keys(builtMapping).length > 0 ? builtMapping : undefined; 158 157 159 158 // Publication setup 160 159 const publicationChoice = await select({