@figma-export/core
Advanced tools
Comparing version 4.8.0-alpha.3 to 4.8.0-alpha.4
@@ -14,3 +14,3 @@ "use strict"; | ||
const figma_1 = require("./figma"); | ||
const components = ({ token, fileId, version, onlyFromPages = [], filterComponent = () => true, transformers = [], outputters = [], concurrency = 30, retries = 3, log = (msg) => { | ||
const components = ({ token, fileId, version, ids = [], onlyFromPages = [], filterComponent = () => true, includeTypes = ['COMPONENT'], transformers = [], outputters = [], concurrency = 30, retries = 3, log = (msg) => { | ||
// eslint-disable-next-line no-console | ||
@@ -24,5 +24,6 @@ console.log(msg); | ||
version, | ||
ids, | ||
onlyFromPages, | ||
}); | ||
const pages = (0, figma_1.getPagesWithComponents)(figmaDocument, { filterComponent }); | ||
const pages = (0, figma_1.getPagesWithComponents)(figmaDocument, { filterComponent, includeTypes }); | ||
log('preparing components'); | ||
@@ -29,0 +30,0 @@ const pagesWithSvg = yield (0, figma_1.enrichPagesWithSvg)(client, fileId, pages, version, { |
@@ -15,3 +15,3 @@ "use strict"; | ||
const figmaStyles_1 = require("./figmaStyles"); | ||
const styles = ({ token, fileId, version, onlyFromPages = [], outputters = [], log = (msg) => { | ||
const styles = ({ token, fileId, version, ids = [], onlyFromPages = [], outputters = [], log = (msg) => { | ||
// eslint-disable-next-line no-console | ||
@@ -25,2 +25,3 @@ console.log(msg); | ||
version, | ||
ids, | ||
onlyFromPages, | ||
@@ -27,0 +28,0 @@ }); |
@@ -9,5 +9,5 @@ /// <reference types="react" /> | ||
export declare const getClient: (token: string) => Figma.ClientInterface; | ||
export declare const getComponents: (children?: readonly Figma.Node[], filter?: FigmaExport.ComponentFilter, pathToComponent?: FigmaExport.ComponentExtras['pathToComponent']) => FigmaExport.ComponentNode[]; | ||
export declare const getDocument: (client: Figma.ClientInterface, options: PickOption<FigmaExport.ComponentsCommand, 'fileId' | 'version' | 'onlyFromPages'>) => Promise<Figma.Document>; | ||
export declare const getStyles: (client: Figma.ClientInterface, options: PickOption<FigmaExport.ComponentsCommand, 'fileId' | 'version' | 'onlyFromPages'>) => Promise<{ | ||
export declare const getComponents: (children: readonly Figma.Node[], { filterComponent, includeTypes }: Required<PickOption<FigmaExport.ComponentsCommand, 'filterComponent' | 'includeTypes'>>, pathToComponent?: FigmaExport.ComponentExtras['pathToComponent']) => FigmaExport.ComponentNode[]; | ||
export declare const getDocument: (client: Figma.ClientInterface, options: PickOption<FigmaExport.ComponentsCommand, 'fileId' | 'version' | 'ids' | 'onlyFromPages'>) => Promise<Figma.Document>; | ||
export declare const getStyles: (client: Figma.ClientInterface, options: PickOption<FigmaExport.StylesCommand, 'fileId' | 'version' | 'ids' | 'onlyFromPages'>) => Promise<{ | ||
readonly [key: string]: Figma.Style; | ||
@@ -34,5 +34,5 @@ }>; | ||
export declare const fileSvgs: (client: Figma.ClientInterface, fileId: string, ids: string[], version?: string, { concurrency, retries, transformers, onFetchCompleted, }?: FileSvgOptions) => Promise<FigmaExportFileSvg>; | ||
export declare const getPagesWithComponents: (document: Figma.Document, options?: PickOption<FigmaExport.ComponentsCommand, 'filterComponent'>) => FigmaExport.PageNode[]; | ||
export declare const getPagesWithComponents: (document: Figma.Document, options: Required<PickOption<FigmaExport.ComponentsCommand, 'filterComponent' | 'includeTypes'>>) => FigmaExport.PageNode[]; | ||
export declare const enrichPagesWithSvg: (client: Figma.ClientInterface, fileId: string, pages: FigmaExport.PageNode[], version?: string, svgOptions?: FileSvgOptions) => Promise<FigmaExport.PageNode[]>; | ||
export {}; | ||
//# sourceMappingURL=figma.d.ts.map |
@@ -73,6 +73,6 @@ "use strict"; | ||
const getPagesFromDocument = (document, options = {}) => { | ||
const onlyFromPages = (0, utils_1.sanitizeOnlyFromPages)(options.onlyFromPages); | ||
const onlyFromPages = (0, utils_1.forceArray)(options.onlyFromPages); | ||
return document.children | ||
.filter((node) => { | ||
return node.type === 'CANVAS' && (onlyFromPages.length === 0 || onlyFromPages.includes(node.name)); | ||
return node.type === 'CANVAS' && (onlyFromPages.length === 0 || onlyFromPages.includes(node.name) || onlyFromPages.includes(node.id)); | ||
}); | ||
@@ -93,10 +93,21 @@ }; | ||
if (pageIds.length === 0) { | ||
throw new Error(`Cannot find any page with "onlyForPages" equal to [${(0, utils_1.sanitizeOnlyFromPages)(options.onlyFromPages).join(', ')}].`); | ||
const errorAsString = (0, utils_1.forceArray)(options.onlyFromPages) | ||
.map((page) => `"${page}"`) | ||
.join(', '); | ||
throw new Error(`Cannot find any page with "onlyForPages" equal to [${errorAsString}].`); | ||
} | ||
return pageIds; | ||
}); | ||
const getComponents = (children = [], filter = () => true, pathToComponent = []) => { | ||
/** | ||
* Determines whether the `searchElement.type` is included in the `availableTypes` list, returning true or false as appropriate. | ||
* @param availableTypes List of available node types. | ||
* @param searchNode The node to search for. | ||
*/ | ||
function isNodeOfType(availableTypes, searchNode) { | ||
return availableTypes.includes(searchNode.type); | ||
} | ||
const getComponents = (children, { filterComponent, includeTypes }, pathToComponent = []) => { | ||
let components = []; | ||
children.forEach((node) => { | ||
if (node.type === 'COMPONENT' && filter(node)) { | ||
if (isNodeOfType(includeTypes, node) && filterComponent(node)) { | ||
components.push(Object.assign(Object.assign({}, node), { svg: '', figmaExport: { | ||
@@ -113,3 +124,3 @@ id: node.id, | ||
...components, | ||
...(0, exports.getComponents)((node.children), filter, [...pathToComponent, { name: node.name, type: node.type }]), | ||
...(0, exports.getComponents)((node.children), { filterComponent, includeTypes }, [...pathToComponent, { name: node.name, type: node.type }]), | ||
]; | ||
@@ -125,5 +136,8 @@ } | ||
// when `onlyFromPages` is set, we avoid traversing all the document tree, but instead we get only requested ids. | ||
ids: (0, utils_1.sanitizeOnlyFromPages)(options.onlyFromPages).length > 0 | ||
? yield getAllPageIds(client, options) | ||
: undefined, | ||
// eslint-disable-next-line no-nested-ternary | ||
ids: (0, utils_1.forceArray)(options.ids).length > 0 | ||
? options.ids | ||
: (0, utils_1.forceArray)(options.onlyFromPages).length > 0 | ||
? yield getAllPageIds(client, options) | ||
: undefined, | ||
}); | ||
@@ -202,6 +216,6 @@ }); | ||
exports.fileSvgs = fileSvgs; | ||
const getPagesWithComponents = (document, options = {}) => { | ||
const getPagesWithComponents = (document, options) => { | ||
const pages = getPagesFromDocument(document); | ||
return pages | ||
.map((page) => (Object.assign(Object.assign({}, page), { components: (0, exports.getComponents)(page.children, options.filterComponent) }))) | ||
.map((page) => (Object.assign(Object.assign({}, page), { components: (0, exports.getComponents)(page.children, options) }))) | ||
.filter((page) => page.components.length > 0); | ||
@@ -208,0 +222,0 @@ }; |
@@ -26,5 +26,5 @@ import * as FigmaExport from '@figma-export/types'; | ||
/** | ||
* Sanitize `onlyFromPages` option by converting to a not nullish and not empty string array. | ||
* Sanitize an array by converting it to a not nullish and not empty string array. | ||
*/ | ||
export declare function sanitizeOnlyFromPages(onlyFromPages: PickOption<FigmaExport.ComponentsCommand | FigmaExport.StylesCommand, 'onlyFromPages'>['onlyFromPages']): string[]; | ||
export declare function forceArray(maybeArray: string[] | undefined): string[]; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sanitizeOnlyFromPages = exports.notEmptyString = exports.notNullish = exports.fetchAsSvgXml = exports.chunk = exports.promiseSequentially = exports.fromEntries = exports.emptySvg = exports.toArray = void 0; | ||
exports.forceArray = exports.notEmptyString = exports.notNullish = exports.fetchAsSvgXml = exports.chunk = exports.promiseSequentially = exports.fromEntries = exports.emptySvg = exports.toArray = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
@@ -76,8 +76,9 @@ const toArray = (any) => (Array.isArray(any) ? any : [any]); | ||
/** | ||
* Sanitize `onlyFromPages` option by converting to a not nullish and not empty string array. | ||
* Sanitize an array by converting it to a not nullish and not empty string array. | ||
*/ | ||
function sanitizeOnlyFromPages(onlyFromPages) { | ||
return (onlyFromPages !== null && onlyFromPages !== void 0 ? onlyFromPages : []).filter((v) => (0, exports.notNullish)(v) && notEmptyString(v)); | ||
function forceArray(maybeArray) { | ||
return (maybeArray !== null && maybeArray !== void 0 ? maybeArray : []) | ||
.filter((v) => (0, exports.notNullish)(v) && notEmptyString(v)); | ||
} | ||
exports.sanitizeOnlyFromPages = sanitizeOnlyFromPages; | ||
exports.forceArray = forceArray; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@figma-export/core", | ||
"version": "4.8.0-alpha.3", | ||
"version": "4.8.0-alpha.4", | ||
"description": "@figma-export core functionalities", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@figma-export/types": "^4.8.0-alpha.3", | ||
"@figma-export/types": "^4.8.0-alpha.4", | ||
"axios": "^1.6.7", | ||
@@ -39,3 +39,3 @@ "figma-js": "~1.16.0", | ||
}, | ||
"gitHead": "f32461a8a167e607fe5d8442f986c3e7f369ebb1" | ||
"gitHead": "e883adb11395faa225a228beb7126909430bfb46" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
65751
809