0
Fork 0
mirror of https://github.com/penpot/penpot-export.git synced 2025-01-04 13:50:05 -05:00

refactor(core): separate file writting logic from output adapters

This commit is contained in:
Roberto Redradix 2023-09-07 17:23:56 +02:00
parent f33dc7a9b8
commit af856a642e
6 changed files with 54 additions and 62 deletions

View file

@ -11,7 +11,12 @@ import {
normalizePenpotExportUserConfig,
AssetConfig,
} from './config'
import { writeJsonFile, writeCssFile, writeScssFile } from './outputters'
import {
writeTextFile,
cssOutputter,
scssOutputter,
jsonOutputter,
} from './outputters'
import { CSSClassDefinition, CSSCustomPropertyDefinition } from './types'
const processOutput = ({
@ -24,13 +29,13 @@ const processOutput = ({
content: CSSClassDefinition[] | CSSCustomPropertyDefinition[]
}) => {
if (outputFormat === 'css') {
return writeCssFile(outputPath, content)
return writeTextFile(outputPath, cssOutputter, content)
}
if (outputFormat === 'scss') {
return writeScssFile(outputPath, content)
return writeTextFile(outputPath, scssOutputter, content)
}
if (outputFormat === 'json') {
return writeJsonFile(outputPath, content)
return writeTextFile(outputPath, jsonOutputter, content)
}
throw new Error(
'Unable to process output format. This is an error in penpot-export code, please contact their authors.',

View file

@ -1,5 +1,10 @@
import fs from 'node:fs'
import path from 'node:path'
import {
CSSClassDefinition,
CSSCustomPropertyDefinition,
isCssClassDefinition,
} from '../../types'
import { OutputterFunction } from '../types'
import {
camelToKebab,
@ -7,12 +12,6 @@ import {
textToCssCustomPropertyName,
} from './syntax'
import {
CSSClassDefinition,
CSSCustomPropertyDefinition,
isCssClassDefinition,
} from '../../types'
const areCssCustomPropertiesDefinitions = (
objects: Array<object>,
): objects is Array<CSSCustomPropertyDefinition> => {
@ -41,7 +40,7 @@ const serializeCssCustomProperty = (
return `${padding}${key}: ${value};`
}
const serializeCss = (
const serializeCss: OutputterFunction = (
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
): string => {
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
@ -55,16 +54,4 @@ const serializeCss = (
}
}
export function writeCssFile(
outputPath: string,
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
) {
const css = serializeCss(cssDefinitions)
const dirname = path.dirname(outputPath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
fs.writeFileSync(outputPath, css, 'utf-8')
}
export default serializeCss

View file

@ -1,3 +1,23 @@
export * from './css'
export * from './scss'
export * from './json'
import fs from 'node:fs'
import path from 'node:path'
import { OutputterFunction } from './types'
export { default as cssOutputter } from './css'
export { default as scssOutputter } from './scss'
export { default as jsonOutputter } from './json'
export function writeTextFile(
outputPath: string,
outputter: OutputterFunction,
...outputterParams: Parameters<OutputterFunction>
) {
const textContents = outputter(...outputterParams)
const dirname = path.dirname(outputPath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
fs.writeFileSync(outputPath, textContents, 'utf-8')
}

View file

@ -1,23 +1,11 @@
import fs from 'node:fs'
import path from 'node:path'
import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types'
const serializeJson = (
import { OutputterFunction } from './types'
const serializeJson: OutputterFunction = (
cssClassDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
): string => {
return JSON.stringify(cssClassDefinitions, null, 2)
}
export function writeJsonFile(
outputPath: string,
cssClassDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
) {
const json = serializeJson(cssClassDefinitions)
const dirname = path.dirname(outputPath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
fs.writeFileSync(outputPath, json, 'utf-8')
}
export default serializeJson

View file

@ -1,6 +1,3 @@
import fs from 'node:fs'
import path from 'node:path'
import {
CSSClassDefinition,
CSSCustomPropertyDefinition,
@ -9,6 +6,8 @@ import {
import { camelToKebab } from '../css/syntax'
import { OutputterFunction } from '../types'
import { textToScssVariableName } from './syntax'
const areCssCustomPropertiesDefinitions = (
@ -40,7 +39,7 @@ const serializeScssVariable = (
return `${property}: ${value};`
}
const serializeScss = (
const serializeScss: OutputterFunction = (
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
): string => {
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
@ -53,16 +52,4 @@ const serializeScss = (
}
}
export function writeScssFile(
outputPath: string,
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
) {
const css = serializeScss(cssDefinitions)
const dirname = path.dirname(outputPath)
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname, { recursive: true })
}
fs.writeFileSync(outputPath, css, 'utf-8')
}
export default serializeScss

View file

@ -0,0 +1,5 @@
import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types'
export type OutputterFunction = (
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
) => string