figma-api-exporter
Advanced tools
Comparing version 0.0.2 to 1.0.0
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"] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18640
5
4
13
0
289
1
46
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.3.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedfigma-js@1.13.0(transitive)
+ Addedfigma-transformer@2.1.0(transitive)
+ Addedform-data@4.0.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedramda@0.28.0(transitive)
+ Addedrimraf@4.1.2(transitive)
- Removedfs@^0.0.1-security
- Removedaxios@0.21.4(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfigma-js@1.16.0(transitive)
- Removedfigma-transformer@1.0.0(transitive)
- Removedfs@0.0.1-security(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedramda@0.26.1(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedaxios@1.3.2
Updatedfigma-js@1.13.0
Updatedfigma-transformer@2.1.0
Updatedramda@0.28.0
Updatedrimraf@4.1.2