figgo
Version:
A CLI tool make your design tokens stay up to date with your Figma design styleguide
143 lines • 5.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = require("node-fetch");
const helper_1 = require("./helper");
const color_1 = require("./models/color");
const spacing_1 = require("./models/spacing");
const typography_1 = require("./models/typography");
function getAccount(token) {
return __awaiter(this, void 0, void 0, function* () {
const url = `https://api.figma.com/v1/me`;
try {
const result = yield node_fetch_1.default(url, {
headers: { "X-FIGMA-TOKEN": token },
method: "get"
});
return result.json();
}
catch (err) {
console.error(err);
}
});
}
exports.getAccount = getAccount;
function getBoard(token, board) {
return __awaiter(this, void 0, void 0, function* () {
const url = `https://api.figma.com/v1/files/${board}`;
try {
const result = yield node_fetch_1.default(url, {
headers: { "X-FIGMA-TOKEN": token },
method: "get"
});
return result.json();
}
catch (err) {
console.log(err);
}
});
}
exports.getBoard = getBoard;
function auth(token, board) {
return __awaiter(this, void 0, void 0, function* () {
const url = `https://api.figma.com/v1/files/${board}`;
let response;
try {
response = yield node_fetch_1.default(url, {
headers: { "X-FIGMA-TOKEN": token },
method: "get"
});
if (response.status === 200) {
return yield response.json();
}
else {
switch (response.status) {
case 403:
throw new Error(`${response.status} - Figgo cannot authenicate you`);
case 404:
throw new Error(`${response.status} - Figgo cannot find board (id:${board})`);
default:
break;
}
return;
}
}
catch (e) {
console.log(helper_1.errorLog(e));
}
});
}
exports.auth = auth;
function getColors(token, board, type) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield auth(token, board);
if (data) {
const frames = data.document.children[0].children;
const array = [];
const colorFrame = frames.filter(frame => frame.name === "Palette");
const colorBlocks = colorFrame[0].children.filter(block => block.type === "RECTANGLE");
for (const i in colorBlocks) {
if (colorBlocks[i].fills[0].type === "SOLID") {
const name = colorBlocks[i].name;
const rgba = colorBlocks[i].fills[0].color;
const newColor = new color_1.default(name, rgba.r, rgba.g, rgba.b, rgba.a);
array.push(newColor[type]);
}
}
return array;
}
});
}
exports.getColors = getColors;
function getSpaces(token, board, type) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield auth(token, board);
if (data) {
const frames = data.document.children[0].children;
const array = [];
const spaceFrame = frames.filter(frame => frame.name === "Space");
const spaceBlocks = spaceFrame[0].children.filter(blocks => blocks.type === "RECTANGLE");
for (const i in spaceBlocks) {
if (spaceBlocks) {
const name = spaceBlocks[i].name;
const value = spaceBlocks[i].absoluteBoundingBox.width;
const newSpace = new spacing_1.default(name, value);
array.push(newSpace[type]);
}
}
return array;
}
});
}
exports.getSpaces = getSpaces;
function getTypographics(token, board, type) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield auth(token, board);
if (data) {
const frames = data.document.children[0].children;
const array = [];
const typoFrame = frames.filter(frame => frame.name === "Typography");
const typoBlocks = typoFrame[0].children.filter(blocks => blocks.type === "TEXT");
for (const i in typoBlocks) {
if (typoBlocks[i]) {
const name = typoBlocks[i].name;
const style = typoBlocks[i].style;
const { fontFamily, fontWeight, fontSize, letterSpacing, lineHeightPx } = style;
const newTypo = new typography_1.default(name, fontFamily, fontWeight, fontSize, letterSpacing, lineHeightPx);
array.push(newTypo[type]);
}
}
return array;
}
});
}
exports.getTypographics = getTypographics;
//# sourceMappingURL=service.js.map