Socket
Socket
Sign inDemoInstall

figma-api-exporter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

figma-api-exporter - npm Package Compare versions

Comparing version 0.0.2 to 1.0.0

LICENSE

2

dist/downloadSvgs.d.ts
import { SvgData } from "./getSvgs";
declare type DownloadSvgsConfig = {
type DownloadSvgsConfig = {
svgsData: SvgData[];

@@ -4,0 +4,0 @@ saveDirectory: string;

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;

@@ -21,3 +37,3 @@ };

if (!fs.existsSync(dir)) {
fs.mkdir(dir, null, resolve);
fs.mkdir(dir, null, () => resolve());
}

@@ -29,5 +45,2 @@ else {

};
const removeFilesFromDirectory = async (directory) => {
return new Promise(resolve => rimraf_1.default(directory, resolve));
};
const getDataFromConfig = async (configFilePath) => {

@@ -61,6 +74,6 @@ return new Promise(resolve => {

const DOWNLOAD_CONFIG_FILENAME = "downloadData.json";
const componentIds = ramda_1.map((data) => data.id)(config.svgsData);
const componentIds = (0, ramda_1.map)((data) => data.id)(config.svgsData);
const dataFromConfig = await getDataFromConfig(path.join(config.saveDirectory, DOWNLOAD_CONFIG_FILENAME));
const shouldDownloadSvgs = config.lastModified !== dataFromConfig.lastModified ||
!ramda_1.equals(componentIds, dataFromConfig.componentIds);
!(0, ramda_1.equals)(componentIds, dataFromConfig.componentIds);
if (!shouldDownloadSvgs) {

@@ -70,3 +83,3 @@ return Promise.resolve();

if (config.clearDirectory) {
await removeFilesFromDirectory(config.saveDirectory);
await (0, rimraf_1.default)(config.saveDirectory);
}

@@ -73,0 +86,0 @@ await createDir(config.saveDirectory);

import { ClientInterface, Node } from "figma-js";
import TransformerNode from "./TransformerNode";
declare type CanvasFilterFunction = (canvas: TransformerNode) => boolean;
declare type CanvasFilterParam = string | CanvasFilterFunction;
declare type NodeFilterFunction = (node: Node) => boolean;
declare type NodeFilterParam = string | NodeFilterFunction;
export declare type SvgData = {
type CanvasFilterFunction = (canvas: TransformerNode) => boolean;
type CanvasFilterParam = string | CanvasFilterFunction;
type NodeFilterFunction = (node: Node) => boolean;
type NodeFilterParam = string | NodeFilterFunction;
export type SvgData = {
id: string;

@@ -12,12 +12,13 @@ url: string;

};
declare type GetSvgsReturn = {
type GetSvgsReturn = {
svgs: SvgData[];
lastModified: string;
};
export declare type GetSvgsConfig = {
export type GetSvgsConfig = {
fileId: string;
canvas?: CanvasFilterParam;
component?: NodeFilterParam;
batchSize?: number;
};
declare const _default: (client: ClientInterface) => (config: GetSvgsConfig) => Promise<GetSvgsReturn>;
export default _default;

@@ -6,16 +6,16 @@ "use strict";

const filterByCanvas = (canvasFilter) => (data) => {
const canvasList = data.shortcuts
.CANVAS;
const pages = data.shortcuts
.pages;
if (typeof canvasFilter === "string") {
return ramda_1.filter((canvas) => canvas.name === canvasFilter)(canvasList);
return (0, ramda_1.filter)((canvas) => canvas.name === canvasFilter)(pages);
}
return ramda_1.filter(canvasFilter)(canvasList);
return (0, ramda_1.filter)(canvasFilter)(pages);
};
const filterByNode = (nodeFilter) => (data) => {
if (typeof nodeFilter === "string") {
return ramda_1.filter((node) => node.name === nodeFilter)(data);
return (0, ramda_1.filter)((node) => node.name === nodeFilter)(data);
}
return ramda_1.filter(nodeFilter)(data);
return (0, ramda_1.filter)(nodeFilter)(data);
};
const getComponents = (transformerNode) => ramda_1.chain((node) => node.shortcuts.COMPONENT)(transformerNode);
const getComponents = (transformerNode) => (0, ramda_1.chain)((node) => node.shortcuts.components)(transformerNode);
const getSvgDataFromImageData = (svgsUrls) => (node) => {

@@ -26,4 +26,5 @@ return { id: node.id, url: svgsUrls[node.id], name: node.name };

const fileData = await client.file(config.fileId);
const processedFile = figma_transformer_1.processFile(fileData.data, config.fileId);
const processedFile = (0, figma_transformer_1.processFile)(fileData.data, config.fileId);
const fileLastModified = fileData.data.lastModified;
const batchSize = config.batchSize || 100;
const optionallyFilterByCanvas = (config.canvas

@@ -35,14 +36,17 @@ ? filterByCanvas(config.canvas)

: ramda_1.identity);
const componentsData = ramda_1.pipe(optionallyFilterByCanvas, getComponents, optionallyFilterByComponent)(processedFile);
if (ramda_1.isEmpty(componentsData)) {
const componentsData = (0, ramda_1.pipe)(optionallyFilterByCanvas, getComponents, optionallyFilterByComponent)(processedFile);
if ((0, ramda_1.isEmpty)(componentsData)) {
return { svgs: [], lastModified: fileLastModified };
}
const svgsIds = ramda_1.map(ramda_1.prop("id"))(componentsData);
const svgsExportResponse = await client.fileImages(config.fileId, {
const svgsIds = (0, ramda_1.map)((0, ramda_1.prop)("id"))(componentsData);
const batchCount = Math.ceil(svgsIds.length / batchSize);
const promises = Array.from(Array(batchCount), (_, i) => client.fileImages(config.fileId, {
format: "svg",
ids: svgsIds
ids: svgsIds.slice(i * batchSize, (i + 1) * batchSize)
}));
const svgsData = (await Promise.all(promises)).flatMap((response, index) => {
const svgUrls = response.data.images;
return (0, ramda_1.map)(getSvgDataFromImageData(svgUrls))(componentsData.slice(index * batchSize, (index + 1) * batchSize));
});
const svgsUrls = svgsExportResponse.data.images;
const svgsData = ramda_1.map(getSvgDataFromImageData(svgsUrls))(componentsData);
return { svgs: svgsData, lastModified: fileLastModified };
};
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;

@@ -21,5 +37,5 @@ };

return {
getSvgs: getSvgs_1.default(client),
getSvgs: (0, getSvgs_1.default)(client),
downloadSvgs: downloadSvgs_1.default
};
};
import { Node } from "figma-js";
import { Shortcut } from "figma-transformer";
declare type TransformerNode = Node & {
shortcuts: Record<Shortcut, Node[]>;
import { Shortcuts } from "figma-transformer";
type TransformerNode = Node & {
shortcuts: Shortcuts;
};
export default TransformerNode;
{
"name": "figma-api-exporter",
"author": "Sławomir Kołodziej",
"version": "0.0.2",
"version": "1.0.0",
"scripts": {

@@ -12,16 +12,14 @@ "start": "ts-node-dev src/index.ts",

"devDependencies": {
"@types/node": "^13.1.6",
"@types/ramda": "^0.26.39",
"@types/rimraf": "^2.0.3",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.7.4"
"@types/node": "18.13.0",
"@types/ramda": "0.28.23",
"ts-node-dev": "2.0.0",
"typescript": "4.9.5"
},
"dependencies": {
"axios": "^0.19.1",
"fs": "^0.0.1-security",
"ramda": "^0.26.1",
"rimraf": "^3.0.0",
"figma-js": "^1.9.0",
"figma-transformer": "^1.0.0"
"axios": "1.3.2",
"ramda": "0.28.0",
"rimraf": "4.1.2",
"figma-js": "1.13.0",
"figma-transformer": "2.1.0"
}
}

@@ -27,2 +27,20 @@ # Figma API Exporter

);
```
```
## API
### getSvgs
Function which fetches urls of component exports as svgs.
Arguments:
- fileId (required) (string) - id of figma files you want to extract svgs from
- canvas (optional) (string|function) - filter exported components by Page name, you can use a custom filter function with additional information about page
- component (optional) (string|function) - filter by exported components by Component name, you can use a custom filter function with additional information about component
### downloadSvgs
Function which downloads svg files to your directory. It checks Figma File modification date and component ids to find out if your local copy is up to date before downloading everything. You can replace this function with your own script.
Arguments:
- saveDirectory (required) (string) - path to save svg files
- svgsData (required) (array) - '.svgs' values returned from `.getSvgs()`
- lastModified (required) (string) - `.lastModified` value returned from `.getSvgs()`, used to check if Figma file was modified and it should continue downloading
- clearDirectory (optional) (boolean) (defaults to: `false`) - change to `true` if you want to clear the `saveDirectory` before downloading files, it comes handy when you want to delete old icons, which don't exist in Figma anymore

@@ -66,3 +66,3 @@ {

},
"exclude": ["node_mocules", "dist"]
"exclude": ["node_modules", "dist"]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc