-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc266d5
commit 96f2f6c
Showing
34 changed files
with
21,438 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
apps/dashboard/src/app/(dashboard)/tokens/converter/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Text } from '@ds-project/components'; | ||
import { JsonBlock, MainContent } from '@/components'; | ||
import { fetchTokens } from '../_actions'; | ||
|
||
export default async function Tokens() { | ||
const figmaVariables = await fetchTokens(); | ||
|
||
if (!figmaVariables) { | ||
return ( | ||
<Text> | ||
<p>Error fetching variables</p> | ||
</Text> | ||
); | ||
} | ||
|
||
return ( | ||
<MainContent | ||
description="Compare the extraction from Figma Variables to the conversion to Design Tokens" | ||
title="Design Tokens" | ||
> | ||
<div className="flex flex-col gap-4"> | ||
<JsonBlock src={figmaVariables} /> | ||
</div> | ||
</MainContent> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { NextRequest } from 'next/server'; | ||
import { eq } from 'drizzle-orm'; | ||
import { database } from '@/lib/drizzle'; | ||
import { insertResourcesSchema, resourcesTable } from '@/lib/drizzle/schema'; | ||
import { isAuthenticated } from '@/lib/supabase/server/utils/is-authenticated'; | ||
|
||
export async function POST(request: NextRequest) { | ||
if (!(await isAuthenticated(request))) { | ||
return new Response('Not authenticated', { status: 401 }); | ||
} | ||
|
||
try { | ||
const validatedData = insertResourcesSchema | ||
.pick({ designTokens: true, projectId: true }) | ||
.parse(await request.json()); | ||
|
||
// Update database | ||
await database | ||
.update(resourcesTable) | ||
.set({ | ||
designTokens: validatedData.designTokens, | ||
}) | ||
.where(eq(resourcesTable.projectId, validatedData.projectId)); | ||
} catch (error) { | ||
return new Response(JSON.stringify(error), { status: 400 }); | ||
} | ||
|
||
return new Response('OK', { status: 200 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/figma-plugin/src/plugin/extract-variables/extract-variables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { extractLocalVariableCollections } from './extractors'; | ||
|
||
export async function extractVariables(figma: PluginAPI) { | ||
const variableCollections = await extractLocalVariableCollections(figma); | ||
|
||
return variableCollections; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/figma-plugin/src/plugin/extract-variables/extractors/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './local-variable-collections'; |
7 changes: 7 additions & 0 deletions
7
packages/figma-plugin/src/plugin/extract-variables/extractors/local-variable-collections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { extractVariableCollection } from './variable-collection'; | ||
|
||
export async function extractLocalVariableCollections(figma: PluginAPI) { | ||
const collections = await figma.variables.getLocalVariableCollectionsAsync(); | ||
|
||
return await Promise.all(collections.map(extractVariableCollection(figma))); | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/figma-plugin/src/plugin/extract-variables/extractors/variable-collection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { FigmaExtractedVariableCollection } from '@ds-project/types/src/types/figma'; | ||
import { nonNullable } from '../utils/non-nullable'; | ||
import { extractVariable } from './variable'; | ||
|
||
export const extractVariableCollection = | ||
(figma: PluginAPI) => | ||
async ( | ||
variableCollection: VariableCollection | ||
): Promise<FigmaExtractedVariableCollection> => { | ||
const variables = ( | ||
await Promise.all( | ||
variableCollection.variableIds.map(extractVariable(figma)) | ||
) | ||
).filter(nonNullable); | ||
|
||
return { | ||
defaultModeId: variableCollection.defaultModeId, | ||
hiddenFromPublishing: variableCollection.hiddenFromPublishing, | ||
id: variableCollection.id, | ||
key: variableCollection.key, | ||
modes: variableCollection.modes, | ||
name: variableCollection.name, | ||
remote: variableCollection.remote, | ||
variableIds: variableCollection.variableIds, | ||
variables, | ||
}; | ||
}; |
27 changes: 27 additions & 0 deletions
27
packages/figma-plugin/src/plugin/extract-variables/extractors/variable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { FigmaExtractedVariable } from '@ds-project/types/src/types/figma'; | ||
|
||
export const extractVariable = | ||
(figma: PluginAPI) => | ||
async (variableId: string): Promise<FigmaExtractedVariable | undefined> => { | ||
const variable = await figma.variables.getVariableByIdAsync(variableId); | ||
|
||
if (!variable) { | ||
return undefined; | ||
} | ||
|
||
const publishStatus = await variable?.getPublishStatusAsync(); | ||
|
||
return { | ||
publishStatus: publishStatus, | ||
description: variable.description, | ||
hiddenFromPublishing: variable.hiddenFromPublishing, | ||
name: variable.name, | ||
resolvedType: variable.resolvedType, | ||
valuesByMode: variable.valuesByMode, | ||
id: variable.id, | ||
key: variable.key, | ||
remote: variable.remote, | ||
scopes: variable.scopes, | ||
variableCollectionId: variable.variableCollectionId, | ||
}; | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/figma-plugin/src/plugin/extract-variables/utils/non-nullable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function nonNullable<T>(value: T): value is NonNullable<T> { | ||
return value !== null && value !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
const featureFlags = { | ||
shouldUpdateTokens: true, | ||
} as Record<string, boolean>; | ||
|
||
export const config = { | ||
AUTH_API_HOST: 'https://localhost:3000', | ||
READ_INTERVAL: 5 * 1000, // 5 seconds | ||
CREDENTIALS_KEY: 'ds-project-credentials', | ||
PROJECT_ID_KEY: 'ds-project-id', | ||
features: featureFlags, | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@ds-project/types", | ||
"type": "module", | ||
"main": "./src/index.ts", | ||
"types": "./src/index.ts", | ||
"private": true, | ||
"scripts": { | ||
"build": "tsup src/index.ts", | ||
"test": "tsx src/test.ts" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@figma/plugin-typings": "^1.98.0", | ||
"style-dictionary": "^4.0.1", | ||
"tsup": "^8.2.4", | ||
"tsx": "^4.17.0" | ||
}, | ||
"dependencies": { | ||
"color2k": "^2.0.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DesignToken, DesignTokens } from 'style-dictionary/types'; | ||
import { FigmaExtractedVariableCollection } from './types/figma'; | ||
import { extractVariable } from './extractors/extract-variable'; | ||
import { notImplemented } from './filters/not-implemented'; | ||
|
||
export function convertFigmaVariablesToDesignTokens( | ||
variableCollections: Array<FigmaExtractedVariableCollection> | ||
): DesignTokens { | ||
return { | ||
$type: 'figma-design-tokens', | ||
collections: variableCollections.flatMap<DesignTokens>((collection) => ({ | ||
$type: 'collection', | ||
$description: 'Figma Variable Collections', | ||
attributes: { | ||
figma: { | ||
id: collection.id, | ||
key: collection.key, | ||
name: collection.name, | ||
hiddenFromPublishing: collection.hiddenFromPublishing, | ||
modes: collection.modes, | ||
}, | ||
}, | ||
...collection.modes.reduce( | ||
(collectionModeVariables, mode) => ({ | ||
...collectionModeVariables, | ||
[mode.name]: { | ||
...collection.variables.filter(notImplemented).reduce<DesignToken>( | ||
(variables, variable) => ({ | ||
...variables, | ||
[variable.name]: extractVariable(variable, mode.modeId), | ||
}), | ||
{} | ||
), | ||
}, | ||
}), | ||
{} | ||
), | ||
})), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DesignToken } from 'style-dictionary/types'; | ||
import { toHex } from 'color2k'; | ||
import { FigmaExtractedVariable } from '../types'; | ||
|
||
export function extractBoolean( | ||
value: FigmaExtractedVariable['valuesByMode'][string] | ||
): DesignToken['$value'] { | ||
if ( | ||
typeof value === 'string' || | ||
typeof value === 'number' || | ||
(typeof value === 'object' && ('b' in value || 'id' in value)) | ||
) { | ||
// string | number | RGB | RGBA | VariableAlias | ||
// We should not receive these types under boolean. If that happens, something went wrong during the extraction from Figma. | ||
throw new Error('Unexpected boolean type'); | ||
} | ||
|
||
throw new Error('Boolean extraction is not yet implemented'); | ||
} |
Oops, something went wrong.