0
Fork 0
mirror of https://github.com/penpot/penpot-export.git synced 2025-03-12 07:21:20 -05:00

refactor(core): remove PenpotExportFile cumbersome abstraction

This commit is contained in:
Roberto Redradix 2023-09-05 12:03:54 +02:00
parent 924d5bec7b
commit bef9cd65ef
6 changed files with 22 additions and 35 deletions

View file

@ -1,6 +1,7 @@
import { textToCssCustomProperyName } from '../../css/helpers' import { textToCssCustomProperyName } from '../../css/helpers'
import { CSSClassDefinition, PenpotExportFile } from '../../types' import { PenpotApiFile } from '../../api'
import { CSSClassDefinition } from '../../types'
const toRgbaCssValue = (hex: string, alpha: number = 1) => { const toRgbaCssValue = (hex: string, alpha: number = 1) => {
const channels = hex.match(/\w{2}/g) const channels = hex.match(/\w{2}/g)
@ -14,11 +15,11 @@ const toRgbaCssValue = (hex: string, alpha: number = 1) => {
} }
export const adaptColorsToCssVariables = ( export const adaptColorsToCssVariables = (
penpotFile: PenpotExportFile, penpotFile: PenpotApiFile,
): CSSClassDefinition => { ): CSSClassDefinition => {
const { colors } = penpotFile const colors = Object.values(penpotFile.data.colors ?? {})
const cssPropsEntries = Object.values(colors).map((color) => { const cssPropsEntries = colors.map((color) => {
const objectClassName = textToCssCustomProperyName(color.name) const objectClassName = textToCssCustomProperyName(color.name)
return [objectClassName, toRgbaCssValue(color.color, color.opacity)] return [objectClassName, toRgbaCssValue(color.color, color.opacity)]

View file

@ -5,8 +5,8 @@ import {
} from '../../api/helpers' } from '../../api/helpers'
import { textToCssClassSelector } from '../../css/helpers' import { textToCssClassSelector } from '../../css/helpers'
import { PenpotApiObject } from '../../api' import { PenpotApiFile, PenpotApiObject } from '../../api'
import { CSSClassDefinition, PenpotExportFile } from '../../types' import { CSSClassDefinition } from '../../types'
const extractObjectCssProps = (object: PenpotApiObject) => { const extractObjectCssProps = (object: PenpotApiObject) => {
let { textDecoration, ...styles } = object.positionData[0] let { textDecoration, ...styles } = object.positionData[0]
@ -35,17 +35,17 @@ const getTextObjectCssProps = (object: PenpotApiObject) => {
} }
export const adaptPageComponentsToCssClassDefinitions = ( export const adaptPageComponentsToCssClassDefinitions = (
penpotFile: PenpotExportFile, penpotFile: PenpotApiFile,
options: { pageId: string }, options: { pageId: string },
): CSSClassDefinition[] => { ): CSSClassDefinition[] => {
const cssClassDefinitions = [] const pages = penpotFile.data.pagesIndex ?? {}
const page = pages[options.pageId]
const page = penpotFile.pages[options.pageId] const pageObjects = Object.values(page.objects)
const components = pageObjects
const components = Object.values(page.objects)
.filter(isComponent) .filter(isComponent)
.map((object) => getObjectShapesFromPage(object, page)) .map((object) => getObjectShapesFromPage(object, page))
const cssClassDefinitions = []
for (const component of components) { for (const component of components) {
for (const objectId in component.objects) { for (const objectId in component.objects) {
const object = component.objects[objectId] const object = component.objects[objectId]

View file

@ -1,7 +1,7 @@
import { textToCssClassSelector } from '../../css/helpers' import { textToCssClassSelector } from '../../css/helpers'
import { PenpotApiTypography, CssTextProperty } from '../../api' import { PenpotApiTypography, CssTextProperty, PenpotApiFile } from '../../api'
import { CSSClassDefinition, PenpotExportFile } from '../../types' import { CSSClassDefinition } from '../../types'
const getTypographyAssetCssProps = ( const getTypographyAssetCssProps = (
typography: PenpotApiTypography, typography: PenpotApiTypography,
@ -18,11 +18,12 @@ const getTypographyAssetCssProps = (
} }
export const adaptTypographiesToCssClassDefinitions = ( export const adaptTypographiesToCssClassDefinitions = (
penpotFile: PenpotExportFile, penpotFile: PenpotApiFile,
): CSSClassDefinition[] => { ): CSSClassDefinition[] => {
const { fileName, typographies } = penpotFile const fileName = penpotFile.name
const typographies = Object.values(penpotFile.data.typographies ?? {})
const cssClassDefinitions = Object.values(typographies).map((typography) => { const cssClassDefinitions = typographies.map((typography) => {
const cssProps = getTypographyAssetCssProps(typography) const cssProps = getTypographyAssetCssProps(typography)
const selector = textToCssClassSelector(`${fileName}--${typography.name}`) const selector = textToCssClassSelector(`${fileName}--${typography.name}`)

View file

@ -6,7 +6,6 @@ import type {
PenpotClientGetFileOptions, PenpotClientGetFileOptions,
PenpotClientSettings, PenpotClientSettings,
} from './types' } from './types'
import { PenpotExportFile } from '../types'
class PenpotApiError extends Error { class PenpotApiError extends Error {
constructor(parsedError: PenpotApiErrorResponse) { constructor(parsedError: PenpotApiErrorResponse) {
@ -65,9 +64,7 @@ export class Penpot {
return json as ResultType return json as ResultType
} }
async getFile( async getFile(options: PenpotClientGetFileOptions): Promise<PenpotApiFile> {
options: PenpotClientGetFileOptions,
): Promise<PenpotExportFile> {
const file = await this.fetcher<PenpotApiFile>({ const file = await this.fetcher<PenpotApiFile>({
command: 'get-file', command: 'get-file',
body: { body: {
@ -75,11 +72,6 @@ export class Penpot {
}, },
}) })
return { return file
fileName: file.name,
colors: file.data.colors ?? {},
typographies: file.data.typographies ?? {},
pages: file.data.pagesIndex ?? {},
}
} }
} }

View file

@ -24,7 +24,7 @@ export default async function penpotExport(
fileId: fileConfig.fileId, fileId: fileConfig.fileId,
}) })
console.log('🎨 Processing Penpot file: %s', penpotFile.fileName) console.log('🎨 Processing Penpot file: %s', penpotFile.name)
for (const colorsConfig of fileConfig.colors) { for (const colorsConfig of fileConfig.colors) {
const cssPath = path.resolve(rootProjectPath, colorsConfig.output) const cssPath = path.resolve(rootProjectPath, colorsConfig.output)

View file

@ -6,13 +6,6 @@ import type {
export type { UserConfig } from './config/types' export type { UserConfig } from './config/types'
export interface PenpotExportFile {
fileName: string
colors: Record<string, PenpotApiColor>
typographies: Record<string, PenpotApiTypography>
pages: Record<string, PenpotApiPage>
}
export interface CSSClassDefinition { export interface CSSClassDefinition {
selector: string selector: string
cssProps: Record<string, string> cssProps: Record<string, string>