mirror of
https://github.com/penpot/penpot-export.git
synced 2025-01-06 14:50:10 -05:00
refactor(core): separate file writting logic from output adapters
This commit is contained in:
parent
f33dc7a9b8
commit
af856a642e
6 changed files with 54 additions and 62 deletions
|
@ -11,7 +11,12 @@ import {
|
||||||
normalizePenpotExportUserConfig,
|
normalizePenpotExportUserConfig,
|
||||||
AssetConfig,
|
AssetConfig,
|
||||||
} from './config'
|
} from './config'
|
||||||
import { writeJsonFile, writeCssFile, writeScssFile } from './outputters'
|
import {
|
||||||
|
writeTextFile,
|
||||||
|
cssOutputter,
|
||||||
|
scssOutputter,
|
||||||
|
jsonOutputter,
|
||||||
|
} from './outputters'
|
||||||
import { CSSClassDefinition, CSSCustomPropertyDefinition } from './types'
|
import { CSSClassDefinition, CSSCustomPropertyDefinition } from './types'
|
||||||
|
|
||||||
const processOutput = ({
|
const processOutput = ({
|
||||||
|
@ -24,13 +29,13 @@ const processOutput = ({
|
||||||
content: CSSClassDefinition[] | CSSCustomPropertyDefinition[]
|
content: CSSClassDefinition[] | CSSCustomPropertyDefinition[]
|
||||||
}) => {
|
}) => {
|
||||||
if (outputFormat === 'css') {
|
if (outputFormat === 'css') {
|
||||||
return writeCssFile(outputPath, content)
|
return writeTextFile(outputPath, cssOutputter, content)
|
||||||
}
|
}
|
||||||
if (outputFormat === 'scss') {
|
if (outputFormat === 'scss') {
|
||||||
return writeScssFile(outputPath, content)
|
return writeTextFile(outputPath, scssOutputter, content)
|
||||||
}
|
}
|
||||||
if (outputFormat === 'json') {
|
if (outputFormat === 'json') {
|
||||||
return writeJsonFile(outputPath, content)
|
return writeTextFile(outputPath, jsonOutputter, content)
|
||||||
}
|
}
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Unable to process output format. This is an error in penpot-export code, please contact their authors.',
|
'Unable to process output format. This is an error in penpot-export code, please contact their authors.',
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
import fs from 'node:fs'
|
import {
|
||||||
import path from 'node:path'
|
CSSClassDefinition,
|
||||||
|
CSSCustomPropertyDefinition,
|
||||||
|
isCssClassDefinition,
|
||||||
|
} from '../../types'
|
||||||
|
|
||||||
|
import { OutputterFunction } from '../types'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
camelToKebab,
|
camelToKebab,
|
||||||
|
@ -7,12 +12,6 @@ import {
|
||||||
textToCssCustomPropertyName,
|
textToCssCustomPropertyName,
|
||||||
} from './syntax'
|
} from './syntax'
|
||||||
|
|
||||||
import {
|
|
||||||
CSSClassDefinition,
|
|
||||||
CSSCustomPropertyDefinition,
|
|
||||||
isCssClassDefinition,
|
|
||||||
} from '../../types'
|
|
||||||
|
|
||||||
const areCssCustomPropertiesDefinitions = (
|
const areCssCustomPropertiesDefinitions = (
|
||||||
objects: Array<object>,
|
objects: Array<object>,
|
||||||
): objects is Array<CSSCustomPropertyDefinition> => {
|
): objects is Array<CSSCustomPropertyDefinition> => {
|
||||||
|
@ -41,7 +40,7 @@ const serializeCssCustomProperty = (
|
||||||
return `${padding}${key}: ${value};`
|
return `${padding}${key}: ${value};`
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializeCss = (
|
const serializeCss: OutputterFunction = (
|
||||||
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
||||||
): string => {
|
): string => {
|
||||||
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
|
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
|
||||||
|
@ -55,16 +54,4 @@ const serializeCss = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeCssFile(
|
export default serializeCss
|
||||||
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')
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,3 +1,23 @@
|
||||||
export * from './css'
|
import fs from 'node:fs'
|
||||||
export * from './scss'
|
import path from 'node:path'
|
||||||
export * from './json'
|
|
||||||
|
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')
|
||||||
|
}
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
import fs from 'node:fs'
|
|
||||||
import path from 'node:path'
|
|
||||||
import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types'
|
import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types'
|
||||||
|
|
||||||
const serializeJson = (
|
import { OutputterFunction } from './types'
|
||||||
|
|
||||||
|
const serializeJson: OutputterFunction = (
|
||||||
cssClassDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
cssClassDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
||||||
): string => {
|
): string => {
|
||||||
return JSON.stringify(cssClassDefinitions, null, 2)
|
return JSON.stringify(cssClassDefinitions, null, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeJsonFile(
|
export default serializeJson
|
||||||
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')
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
import fs from 'node:fs'
|
|
||||||
import path from 'node:path'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CSSClassDefinition,
|
CSSClassDefinition,
|
||||||
CSSCustomPropertyDefinition,
|
CSSCustomPropertyDefinition,
|
||||||
|
@ -9,6 +6,8 @@ import {
|
||||||
|
|
||||||
import { camelToKebab } from '../css/syntax'
|
import { camelToKebab } from '../css/syntax'
|
||||||
|
|
||||||
|
import { OutputterFunction } from '../types'
|
||||||
|
|
||||||
import { textToScssVariableName } from './syntax'
|
import { textToScssVariableName } from './syntax'
|
||||||
|
|
||||||
const areCssCustomPropertiesDefinitions = (
|
const areCssCustomPropertiesDefinitions = (
|
||||||
|
@ -40,7 +39,7 @@ const serializeScssVariable = (
|
||||||
return `${property}: ${value};`
|
return `${property}: ${value};`
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializeScss = (
|
const serializeScss: OutputterFunction = (
|
||||||
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
||||||
): string => {
|
): string => {
|
||||||
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
|
if (areCssCustomPropertiesDefinitions(cssDefinitions)) {
|
||||||
|
@ -53,16 +52,4 @@ const serializeScss = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeScssFile(
|
export default serializeScss
|
||||||
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')
|
|
||||||
}
|
|
||||||
|
|
5
packages/core/src/lib/outputters/types.ts
Normal file
5
packages/core/src/lib/outputters/types.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types'
|
||||||
|
|
||||||
|
export type OutputterFunction = (
|
||||||
|
cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[],
|
||||||
|
) => string
|
Loading…
Reference in a new issue