-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
111 lines (85 loc) · 2.71 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { processAndWriteColors } from "./processColors.js";
import { processAndWriteSemanticAndComponentTokens } from "./processTokens.js";
import fs from "fs-extra";
import ora from "ora";
import prompts from "prompts";
import "dotenv/config";
const ENV_PATH = "./.env";
export const supportedBrandNames = ["finn", "tori", "dba", "blocket", "dataviz", "neutral"];
const spinner = ora();
export async function fetchAndTransformTokens({
figmaProjectId,
outputFilePath,
tokenVariableCollection,
isDataviz = false,
}) {
spinner.start("Reading Figma access token");
let token = process.env.FIGMA_TOKEN;
if (token) {
spinner.succeed("Using Figma access token from environment variables");
} else {
spinner.warn("No Figma access token found");
token = await getTokenFromPrompt();
}
spinner.start("Loading Figma project");
const res = await fetch(
`https://api.figma.com/v1/files/${figmaProjectId}/variables/local`,
{
headers: {
"X-FIGMA-TOKEN": token,
},
}
);
const json = await res.json();
if (!res.ok) {
throw new Error(json.err || "Failed to fetch Figma data");
}
try {
fs.ensureDirSync("./data/");
fs.outputFileSync(outputFilePath, JSON.stringify(json, null, 2), "utf8");
spinner.succeed(`Loaded Figma tokens`);
} catch (e) {
spinner.fail(`Unable to load Figma tokens: ${e.message}`);
return;
}
spinner.start("Transforming Figma tokens to design tokens");
const sourceData = JSON.parse(fs.readFileSync(outputFilePath, "utf8"));
processAndWriteColors({
variableCollections: sourceData.meta.variableCollections,
variables: sourceData.meta.variables,
tokenVariableCollection,
getBrandName: (collection) =>
isDataviz ? "dataviz" : collection.modes[0].name,
});
processAndWriteSemanticAndComponentTokens(
sourceData,
tokenVariableCollection
);
spinner.succeed("Design tokens written to disk");
}
function writeEnvVarToDisk({ name, value }) {
if (fs.existsSync(ENV_PATH)) {
return fs.appendFileSync(ENV_PATH, `\n${name}=${value}`, "utf8");
}
return fs.writeFileSync(ENV_PATH, `${name}=${value}`, "utf8");
}
async function getTokenFromPrompt() {
let figmaToken;
const tokenPrompt = await prompts({
type: "text",
name: "figmaToken",
message:
"Enter your Figma access token (https://www.figma.com/developers/api#access-tokens)",
});
figmaToken = tokenPrompt.figmaToken;
const { saveToken } = await prompts({
type: "confirm",
name: "saveToken",
message: "Would you like to save the token?",
});
if (saveToken) {
writeEnvVarToDisk({ name: "FIGMA_TOKEN", value: figmaToken });
spinner.succeed("Saved token to " + ENV_PATH);
}
return figmaToken;
}