@sit-onyx/figma-utils 0.0.0 → 1.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -10,9 +10,10 @@ export const importCommand = new Command("import-variables")
|
|
|
10
10
|
.option("-n, --filename <string>", "Base name of the generated variables file", "variables")
|
|
11
11
|
.option("-d, --dir <string>", "Working directory to use. Defaults to current working directory of the script.")
|
|
12
12
|
.option("-m, --modes <strings...>", "Can be used to only export specific Figma modes. If unset, all modes will be exported as a separate file.")
|
|
13
|
+
.option("-s, --selector <string>", 'CSS selector to use for the CSS format. The mode name will be added to the selector if it is set to something other than ":root", e.g. for the mode named "dark", passing the selector "html" will result in "html.dark"', ":root")
|
|
13
14
|
.action(async (options) => {
|
|
14
15
|
const generators = {
|
|
15
|
-
CSS: generateAsCSS,
|
|
16
|
+
CSS: (data) => generateAsCSS(data, options.selector),
|
|
16
17
|
SCSS: generateAsSCSS,
|
|
17
18
|
};
|
|
18
19
|
if (!(options.format in generators)) {
|
package/dist/utils/generate.d.ts
CHANGED
|
@@ -2,15 +2,22 @@ import { ParsedVariable } from "../types/figma.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* Generates the given parsed Figma variables into CSS variables.
|
|
4
4
|
*
|
|
5
|
+
* @param data Parsed Figma variables
|
|
6
|
+
* @param selector CSS selector to use for the CSS format. The mode name will be added to the selector
|
|
7
|
+
* if it is set to something other than ":root", e.g. for the mode named "dark", passing the selector "html" will result in "html.dark"
|
|
5
8
|
* @returns File content of the .css file
|
|
6
9
|
*/
|
|
7
|
-
export declare const generateAsCSS: (data: ParsedVariable) => string;
|
|
10
|
+
export declare const generateAsCSS: (data: ParsedVariable, selector?: string) => string;
|
|
8
11
|
/**
|
|
9
12
|
* Generates the given parsed Figma variables into SCSS variables.
|
|
10
13
|
*
|
|
11
14
|
* @returns File content of the .scss file
|
|
12
15
|
*/
|
|
13
16
|
export declare const generateAsSCSS: (data: ParsedVariable) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Generates the timestamp comment that is added to the start of every generated file.
|
|
19
|
+
*/
|
|
20
|
+
export declare const generateTimestampComment: (modeName?: string) => string;
|
|
14
21
|
/**
|
|
15
22
|
* Checks whether the given variable value is an alias / variable reference to another variable.
|
|
16
23
|
* Alias values are enclosed by curly braces.
|
package/dist/utils/generate.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generates the given parsed Figma variables into CSS variables.
|
|
3
3
|
*
|
|
4
|
+
* @param data Parsed Figma variables
|
|
5
|
+
* @param selector CSS selector to use for the CSS format. The mode name will be added to the selector
|
|
6
|
+
* if it is set to something other than ":root", e.g. for the mode named "dark", passing the selector "html" will result in "html.dark"
|
|
4
7
|
* @returns File content of the .css file
|
|
5
8
|
*/
|
|
6
|
-
export const generateAsCSS = (data) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
export const generateAsCSS = (data, selector = ":root") => {
|
|
10
|
+
const variableContent = Object.entries(data.variables).map(([name, value]) => {
|
|
11
|
+
const { isAlias, variableName } = isAliasVariable(value);
|
|
12
|
+
const variableValue = isAlias ? `var(--${variableName})` : value;
|
|
13
|
+
return ` --${name}: ${variableValue};`;
|
|
11
14
|
});
|
|
15
|
+
let fullSelector = selector.trim();
|
|
16
|
+
if (fullSelector !== ":root")
|
|
17
|
+
fullSelector += `.${data.modeName}`;
|
|
18
|
+
return `${generateTimestampComment(data.modeName)}
|
|
19
|
+
${fullSelector} {\n${variableContent.join("\n")}\n}\n`;
|
|
12
20
|
};
|
|
13
21
|
/**
|
|
14
22
|
* Generates the given parsed Figma variables into SCSS variables.
|
|
@@ -16,29 +24,21 @@ export const generateAsCSS = (data) => {
|
|
|
16
24
|
* @returns File content of the .scss file
|
|
17
25
|
*/
|
|
18
26
|
export const generateAsSCSS = (data) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
const variableContent = Object.entries(data.variables).map(([name, value]) => {
|
|
28
|
+
const { isAlias, variableName } = isAliasVariable(value);
|
|
29
|
+
const variableValue = isAlias ? `$${variableName}` : value;
|
|
30
|
+
return `$${name}: ${variableValue};`;
|
|
23
31
|
});
|
|
32
|
+
return `${generateTimestampComment(data.modeName)}\n${variableContent.join("\n")}\n`;
|
|
24
33
|
};
|
|
25
34
|
/**
|
|
26
|
-
*
|
|
27
|
-
* Will take care of defining selectors and formatting.
|
|
35
|
+
* Generates the timestamp comment that is added to the start of every generated file.
|
|
28
36
|
*/
|
|
29
|
-
const
|
|
30
|
-
const variableContent = Object.entries(options.data.variables).map(([name, value]) => {
|
|
31
|
-
const { isAlias, variableName } = isAliasVariable(value);
|
|
32
|
-
const variableValue = isAlias ? options.aliasTransformer(variableName) : value;
|
|
33
|
-
return ` ${options.nameTransformer(name)}: ${variableValue};`;
|
|
34
|
-
});
|
|
35
|
-
const timestamp = new Date().toUTCString();
|
|
36
|
-
const mode = options.data.modeName;
|
|
37
|
+
export const generateTimestampComment = (modeName) => {
|
|
37
38
|
return `/**
|
|
38
|
-
* Do not edit directly.${
|
|
39
|
-
* Imported from Figma API on ${
|
|
40
|
-
|
|
41
|
-
:root {\n${variableContent.join("\n")}\n}\n`;
|
|
39
|
+
* Do not edit directly.${modeName ? `\n * This file contains the specific variables for the "${modeName}" theme.` : ""}
|
|
40
|
+
* Imported from Figma API on ${new Date().toUTCString()}
|
|
41
|
+
*/`;
|
|
42
42
|
};
|
|
43
43
|
/**
|
|
44
44
|
* Checks whether the given variable value is an alias / variable reference to another variable.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sit-onyx/figma-utils",
|
|
3
3
|
"description": "Utility functions and CLI for importing data from the Figma API into CSS/SCSS variables",
|
|
4
|
-
"version": "0.0.0",
|
|
4
|
+
"version": "1.0.0-alpha.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"@sit-onyx/figma-utils": "./dist/cli.js"
|