Skip to content

Commit

Permalink
continue: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattchewone committed Jan 28, 2025
1 parent 622dde5 commit dc5380b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Utils from './utils';
import { toBase64 } from './utils/index';

export interface PRResult {
prUrl: string;
Expand Down Expand Up @@ -102,7 +102,7 @@ export default {
headers,
body: JSON.stringify({
message: 'Update SCSS variables from Figma',
content: Utils.toBase64(content),
content: toBase64(content),
branch: branchName,
...(fileSha && { sha: fileSha }) // Include SHA if file exists
})
Expand Down
4 changes: 2 additions & 2 deletions src/processors/border.processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StyleProcessor, ProcessedValue } from '../types';
import Utils from '../utils';
import { rgbaToString } from '../utils/index';

export const borderProcessors: StyleProcessor[] = [
{
Expand All @@ -19,7 +19,7 @@ export const borderProcessors: StyleProcessor[] = [
if (stroke?.type === "SOLID") {
const { r, g, b } = stroke.color;
const a = stroke.opacity ?? 1;
const value = Utils.rgbaToString(r, g, b, a);
const value = rgbaToString(r, g, b, a);
return { value, rawValue: value };
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/transformers/css.transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StyleToken, TokenCollection } from '../types';
import Utils from '../utils';
import { groupBy } from '../utils/index';

export function transformToCss(tokens: TokenCollection): string {
let output = "/* Generated CSS */";

Expand All @@ -8,7 +9,7 @@ export function transformToCss(tokens: TokenCollection): string {
token.type === 'style'
);

const variantGroups = Utils.groupBy(styleTokens, t => t.path.join('_'));
const variantGroups = groupBy(styleTokens, t => t.path.join('_'));
Object.entries(variantGroups).forEach(([variantPath, groupTokens]) => {
if (!variantPath) return;
// Remove properties with zero values and unnecessary defaults
Expand Down
10 changes: 5 additions & 5 deletions src/transformers/scss.transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TokenCollection, StyleToken } from '../types';
import Utils from '../utils';
import { sanitizeName, groupBy } from '../utils/index';

export function transformToScss(tokens: TokenCollection): string {
let output = "";
Expand All @@ -8,7 +8,7 @@ export function transformToScss(tokens: TokenCollection): string {
const colorVariables = new Map<string, string>();
tokens.tokens.forEach(token => {
if (token.type === 'variable') {
colorVariables.set(Utils.sanitizeName(token.name), token.rawValue);
colorVariables.set(sanitizeName(token.name), token.rawValue);
}
});

Expand All @@ -27,7 +27,7 @@ export function transformToScss(tokens: TokenCollection): string {
if (token.type === 'style' &&
token.property === 'fills' &&
(token.rawValue.includes('gradient'))) {
const name = `gradient-${Utils.sanitizeName(token.name)}`;
const name = `gradient-${sanitizeName(token.name)}`;
gradientVariables.set(name, token);
}
});
Expand All @@ -54,7 +54,7 @@ export function transformToScss(tokens: TokenCollection): string {
token.type === 'style'
);

const variantGroups = Utils.groupBy(styleTokens, t => t.path.join('_'));
const variantGroups = groupBy(styleTokens, t => t.path.join('_'));

Object.entries(variantGroups).forEach(([variantPath, groupTokens]) => {
if (!variantPath) return;
Expand All @@ -68,7 +68,7 @@ export function transformToScss(tokens: TokenCollection): string {
if (token.property === 'fills' && token.rawValue.includes('gradient')) {
// Only use CSS variables if the token has associated variables
if (token.variables && token.variables.length > 0) {
const gradientName = `gradient-${Utils.sanitizeName(token.name)}`;
const gradientName = `gradient-${sanitizeName(token.name)}`;
output += ` ${token.property}: var(--${gradientName}, #{$${gradientName}})\n`;
} else {
// Use the raw value directly if no variables are involved
Expand Down
53 changes: 0 additions & 53 deletions src/utils.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/utils/array.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function groupBy<T>(arr: T[], key: (item: T) => string): Record<string, T[]> {
return arr.reduce((groups, item) => {
const k = key(item);
if (!groups[k]) groups[k] = [];
groups[k].push(item);
return groups;
}, {} as Record<string, T[]>);
}
5 changes: 5 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './string.utils';
export * from './color.utils';
export * from './array.utils';
export * from './gradient.utils';
export * from './node.utils';
6 changes: 3 additions & 3 deletions src/utils/node.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Utils from '../utils';
import { sanitizeSegment } from "./string.utils";

export function getNodePathName(node: SceneNode): string {
const pathParts: string[] = [];
Expand All @@ -19,7 +19,7 @@ export function getNodePathName(node: SceneNode): string {
export function parseVariantWithoutKey(variant: string): string {
const [_, valueRaw] = variant.split("=");
if (!valueRaw) {
return Utils.sanitizeSegment(variant);
return sanitizeSegment(variant);
}
return Utils.sanitizeSegment(valueRaw);
return sanitizeSegment(valueRaw);
}
29 changes: 29 additions & 0 deletions src/utils/string.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function toBase64(str: string): string {
const base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const utf8str = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
(_, p1) => String.fromCharCode(parseInt(p1, 16))
);
let i = 0;
let result = '';
while (i < utf8str.length) {
const char1 = utf8str.charCodeAt(i++);
const char2 = i < utf8str.length ? utf8str.charCodeAt(i++) : NaN;
const char3 = i < utf8str.length ? utf8str.charCodeAt(i++) : NaN;
const enc1 = char1 >> 2;
const enc2 = ((char1 & 3) << 4) | (char2 >> 4);
const enc3 = ((char2 & 15) << 2) | (char3 >> 6);
const enc4 = char3 & 63;
result += base64chars[enc1] + base64chars[enc2] +
(isNaN(char2) ? '=' : base64chars[enc3]) +
(isNaN(char3) ? '=' : base64chars[enc4]);
}
return result;
}

export function sanitizeName(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9]/g, '-');
}

export function sanitizeSegment(segment: string): string {
return segment.toLowerCase().replace(/[^a-z0-9]/g, '-');
}

0 comments on commit dc5380b

Please sign in to comment.