@sit-onyx/figma-utils 1.0.0-alpha.4 → 1.0.0-alpha.5
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.
package/README.md
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
<div align="center" style="text-align: center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" type="image/svg+xml" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-light.svg">
|
|
4
|
+
<source media="(prefers-color-scheme: light)" type="image/svg+xml" srcset="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg">
|
|
5
|
+
<img alt="onyx logo" src="https://raw.githubusercontent.com/SchwarzIT/onyx/main/.github/onyx-logo-dark.svg" width="160px">
|
|
6
|
+
</picture>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
<br>
|
|
10
|
+
|
|
1
11
|
# @sit-onyx/figma-utils
|
|
2
12
|
|
|
3
13
|
Utility functions and CLI for importing data from the Figma API into different formats (e.g. CSS, SCSS etc.).
|
|
14
|
+
Created by [Schwarz IT](https://it.schwarz).
|
|
4
15
|
|
|
5
16
|
## Use as CLI
|
|
6
17
|
|
|
@@ -10,39 +21,6 @@ For a list of supported commands and options, run:
|
|
|
10
21
|
npx @sit-onyx/figma-utils@latest --help
|
|
11
22
|
```
|
|
12
23
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
This package also provides utility functions for importing data from the Figma API.
|
|
16
|
-
This is useful if you want customize the CLI commands to have full control over the output.
|
|
17
|
-
|
|
18
|
-
```sh
|
|
19
|
-
npm install @sit-onyx/figma-utils
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
### Examples
|
|
24
|
+
## Documentation
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
import fs from "node:fs";
|
|
26
|
-
import path from "node:path";
|
|
27
|
-
import { fetchFigmaVariables, generateAsCSS, parseFigmaVariables } from "@sit-onyx/figma-utils";
|
|
28
|
-
|
|
29
|
-
const FILE_KEY = "your-figma-file-key";
|
|
30
|
-
const FIGMA_TOKEN = "your-figma-access-token";
|
|
31
|
-
|
|
32
|
-
// fetch variables from Figma API
|
|
33
|
-
const data = await fetchFigmaVariables(FILE_KEY, FIGMA_TOKEN);
|
|
34
|
-
|
|
35
|
-
// parse variables into a readable and normalized format
|
|
36
|
-
const parsedVariables = parseFigmaVariables(data);
|
|
37
|
-
|
|
38
|
-
// generate .css files for every Figma mode
|
|
39
|
-
parsedVariables.forEach((mode) => {
|
|
40
|
-
// get .css file content
|
|
41
|
-
const fileContent = generateAsCSS(mode);
|
|
42
|
-
|
|
43
|
-
// write content as a file
|
|
44
|
-
const filename = mode.modeName ? `variables-${mode.modeName}.css` : "variables.css";
|
|
45
|
-
const fullPath = path.join(process.cwd(), filename);
|
|
46
|
-
fs.writeFileSync(fullPath, fileContent);
|
|
47
|
-
});
|
|
48
|
-
```
|
|
26
|
+
You can find our documentation [here](https://onyx.schwarz/development/packages/figma-utils.html).
|
|
@@ -7,7 +7,7 @@ export const importCommand = new Command("import-variables")
|
|
|
7
7
|
.requiredOption("-k, --file-key <string>", "Figma file key (required)")
|
|
8
8
|
.requiredOption("-t, --token <string>", "Figma access token with scope `file_variables:read` (required)")
|
|
9
9
|
.option("-f, --format <strings...>", "Output formats. Supported are: CSS, SCSS, JSON", ["CSS"])
|
|
10
|
-
.option("-n, --filename <string>", "Base name of the generated variables file
|
|
10
|
+
.option("-n, --filename <string>", "Base name / prefix of the generated variables file. Will append the mode name")
|
|
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
13
|
.option("-s, --selector <string>", 'CSS selector to use for the CSS format. You can use {mode} as placeholder for the mode name, so e.g. for the mode named "dark", passing the selector "html.{mode}" will result in "html.dark"', ":root")
|
|
@@ -42,8 +42,12 @@ export async function importCommandAction(options) {
|
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
const outputDirectory = options.dir ?? process.cwd();
|
|
45
|
-
const filename = options.filename ?? "variables";
|
|
46
45
|
console.log(`Generating ${options.format} variables...`);
|
|
46
|
+
const getBaseFileName = (modeName = "default") => {
|
|
47
|
+
if (options.filename)
|
|
48
|
+
return `${options.filename}${modeName}`;
|
|
49
|
+
return modeName;
|
|
50
|
+
};
|
|
47
51
|
options.format.forEach((format) => {
|
|
48
52
|
console.log(`Generating ${format} variables...`);
|
|
49
53
|
parsedVariables.forEach((data) => {
|
|
@@ -54,7 +58,7 @@ export async function importCommandAction(options) {
|
|
|
54
58
|
const isModeIncluded = !options.modes?.length || !data.modeName || options.modes.includes(data.modeName);
|
|
55
59
|
if (!isModeIncluded)
|
|
56
60
|
return;
|
|
57
|
-
const baseName = data.modeName
|
|
61
|
+
const baseName = getBaseFileName(data.modeName);
|
|
58
62
|
const fullPath = path.join(outputDirectory, `${baseName}.${format.toLowerCase()}`);
|
|
59
63
|
fs.writeFileSync(fullPath, generators[format](data));
|
|
60
64
|
});
|
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 different formats (e.g. CSS, SCSS etc.)",
|
|
4
|
-
"version": "1.0.0-alpha.
|
|
4
|
+
"version": "1.0.0-alpha.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"@sit-onyx/figma-utils": "./dist/cli.js"
|
|
@@ -19,8 +19,17 @@
|
|
|
19
19
|
"import": "./dist/index.js"
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
|
+
"homepage": "https://onyx.schwarz/development/packages/figma-utils.html",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/SchwarzIT/onyx",
|
|
26
|
+
"directory": "packages/figma-utils"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/SchwarzIT/onyx/issues"
|
|
30
|
+
},
|
|
22
31
|
"dependencies": {
|
|
23
|
-
"commander": "^
|
|
32
|
+
"commander": "^12.1.0"
|
|
24
33
|
},
|
|
25
34
|
"scripts": {
|
|
26
35
|
"build": "pnpm run '/type-check|build-only/'",
|