From af856a642ece70d3a64099ebe6e5ef851d2943ec Mon Sep 17 00:00:00 2001 From: Roberto Redradix Date: Thu, 7 Sep 2023 17:23:56 +0200 Subject: [PATCH] refactor(core): separate file writting logic from output adapters --- packages/core/src/lib/index.ts | 13 +++++--- packages/core/src/lib/outputters/css/index.ts | 31 ++++++------------- packages/core/src/lib/outputters/index.ts | 26 ++++++++++++++-- packages/core/src/lib/outputters/json.ts | 20 +++--------- .../core/src/lib/outputters/scss/index.ts | 21 +++---------- packages/core/src/lib/outputters/types.ts | 5 +++ 6 files changed, 54 insertions(+), 62 deletions(-) create mode 100644 packages/core/src/lib/outputters/types.ts diff --git a/packages/core/src/lib/index.ts b/packages/core/src/lib/index.ts index dcfd47c..065e6e2 100644 --- a/packages/core/src/lib/index.ts +++ b/packages/core/src/lib/index.ts @@ -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.', diff --git a/packages/core/src/lib/outputters/css/index.ts b/packages/core/src/lib/outputters/css/index.ts index 972deed..dd4fd5c 100644 --- a/packages/core/src/lib/outputters/css/index.ts +++ b/packages/core/src/lib/outputters/css/index.ts @@ -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, ): objects is Array => { @@ -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 diff --git a/packages/core/src/lib/outputters/index.ts b/packages/core/src/lib/outputters/index.ts index 708143c..407b6f3 100644 --- a/packages/core/src/lib/outputters/index.ts +++ b/packages/core/src/lib/outputters/index.ts @@ -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 +) { + const textContents = outputter(...outputterParams) + const dirname = path.dirname(outputPath) + + if (!fs.existsSync(dirname)) { + fs.mkdirSync(dirname, { recursive: true }) + } + + fs.writeFileSync(outputPath, textContents, 'utf-8') +} diff --git a/packages/core/src/lib/outputters/json.ts b/packages/core/src/lib/outputters/json.ts index fdbec07..7339079 100644 --- a/packages/core/src/lib/outputters/json.ts +++ b/packages/core/src/lib/outputters/json.ts @@ -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 diff --git a/packages/core/src/lib/outputters/scss/index.ts b/packages/core/src/lib/outputters/scss/index.ts index 99ef89c..790f5ca 100644 --- a/packages/core/src/lib/outputters/scss/index.ts +++ b/packages/core/src/lib/outputters/scss/index.ts @@ -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 diff --git a/packages/core/src/lib/outputters/types.ts b/packages/core/src/lib/outputters/types.ts new file mode 100644 index 0000000..a8a0a22 --- /dev/null +++ b/packages/core/src/lib/outputters/types.ts @@ -0,0 +1,5 @@ +import { CSSClassDefinition, CSSCustomPropertyDefinition } from '../types' + +export type OutputterFunction = ( + cssDefinitions: CSSClassDefinition[] | CSSCustomPropertyDefinition[], +) => string