Skip to content

Commit

Permalink
chore: clean-up code avoiding duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Nov 22, 2023
1 parent d5200cd commit aeb0c20
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .figmaexportrc.example.local.ts
Expand Up @@ -39,7 +39,7 @@ const styleOptions: StylesCommandOptions = {

const componentOptions: ComponentsCommandOptions = {
fileId: 'fzYhvQpqwhZDUImRz431Qo',
onlyFromPages: ['icons', 'unit-test', 'octicons-by-github'],
onlyFromPages: ['icons', 'unit-test', 'icons/octicons-by-github'],
// concurrency: 30,
transformers: [
transformSvgWithSvgo({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/_mocks_/figma.files.json
Expand Up @@ -42232,7 +42232,7 @@
},
{
"id": "254:0",
"name": "octicons-by-github",
"name": "icons/octicons-by-github",
"type": "CANVAS",
"children": [
{
Expand Down
55 changes: 53 additions & 2 deletions packages/core/src/lib/export-components.test.ts
Expand Up @@ -88,7 +88,53 @@ describe('export-component', () => {
ids: ['10:8', '8:1', '9:1'],
svg_include_id: true,
});
expect(clientFile).to.have.been.calledOnceWithExactly('fileABCD', { version: 'versionABCD' });

expect(clientFile).to.have.been.calledOnce;
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', {
version: 'versionABCD', depth: undefined, ids: undefined,
});

expect(logger).to.have.been.callCount(6);
expect(logger.getCall(0)).to.have.been.calledWith('fetching document');
expect(logger.getCall(1)).to.have.been.calledWith('preparing components');
expect(logger.getCall(2)).to.have.been.calledWith('fetching components 1/3');
expect(logger.getCall(3)).to.have.been.calledWith('fetching components 2/3');
expect(logger.getCall(4)).to.have.been.calledWith('fetching components 3/3');
expect(logger.getCall(5)).to.have.been.calledWith('exported components from fileABCD');

expect(transformer).to.have.been.calledThrice;
expect(transformer.firstCall).to.have.been.calledWith(figmaDocument.svg.content);
expect(transformer.secondCall).to.have.been.calledWith(figmaDocument.svg.content);
expect(transformer.thirdCall).to.have.been.calledWith(figmaDocument.svg.content);

expect(outputter).to.have.been.calledOnceWithExactly(pagesWithSvg);
});

it('should filter by selected page names when setting onlyFromPages', async () => {
const pagesWithSvg = await exportComponents({
fileId: 'fileABCD',
version: 'versionABCD',
token: 'token1234',
log: logger,
outputters: [outputter],
transformers: [transformer],
onlyFromPages: ['page2'],
});

nockScope.done();

expect(FigmaExport.getClient).to.have.been.calledOnceWithExactly('token1234');
expect(clientFileImages).to.have.been.calledOnceWith('fileABCD', {
format: 'svg',
ids: ['10:8', '8:1', '9:1'],
svg_include_id: true,
});

expect(clientFile).to.have.been.calledTwice;
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: 1, ids: undefined });
expect(clientFile.secondCall).to.have.been.calledWith('fileABCD', {
version: 'versionABCD', depth: undefined, ids: ['10:7'],
});

expect(logger).to.have.been.callCount(6);
expect(logger.getCall(0)).to.have.been.calledWith('fetching document');
Expand Down Expand Up @@ -147,7 +193,12 @@ describe('export-component', () => {
ids: ['10:8'],
svg_include_id: true,
});
expect(clientFile).to.have.been.calledOnceWithExactly('fileABCD', { version: 'versionABCD' });

expect(clientFile).to.have.been.calledOnce;
expect(clientFile.firstCall).to.have.been.calledWith(
'fileABCD',
{ version: 'versionABCD', depth: undefined, ids: undefined },
);

expect(logger).to.have.been.callCount(4);
expect(logger.getCall(0)).to.have.been.calledWith('fetching document');
Expand Down
24 changes: 15 additions & 9 deletions packages/core/src/lib/export-components.ts
@@ -1,6 +1,11 @@
import * as FigmaExport from '@figma-export/types';

import { getClient, getPagesWithComponents, enrichPagesWithSvg } from './figma';
import {
getClient,
enrichPagesWithSvg,
getDocument,
getPagesWithComponents,
} from './figma';

export const components: FigmaExport.ComponentsCommand = async ({
token,
Expand All @@ -20,15 +25,16 @@ export const components: FigmaExport.ComponentsCommand = async ({
const client = getClient(token);

log('fetching document');
const { data: { document = null } = {} } = await client.file(fileId, { version }).catch((error: Error) => {
throw new Error(`while fetching file "${fileId}${version ? `?version=${version}` : ''}": ${error.message}`);
});

if (!document) {
throw new Error('\'document\' is missing.');
}
const figmaDocument = await getDocument(
client,
{
fileId,
version,
onlyFromPages,
},
);

const pages = getPagesWithComponents(document, { only: onlyFromPages, filter: filterComponent });
const pages = getPagesWithComponents(figmaDocument, { filterComponent });

log('preparing components');
const pagesWithSvg = await enrichPagesWithSvg(client, fileId, pages, {
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/lib/export-styles.test.ts
Expand Up @@ -47,7 +47,7 @@ describe('export-styles', () => {
sinon.restore();
});

it('should use outputter to export styles', async () => {
it('should use outputter to export styles without defining the "onlyFromPages" option', async () => {
const pagesWithSvg = await exportStyles({
fileId: 'fileABCD',
version: 'versionABCD',
Expand All @@ -58,8 +58,8 @@ describe('export-styles', () => {

expect(FigmaExport.getClient).to.have.been.calledOnceWithExactly('token1234');
expect(clientFileNodes).to.have.been.calledOnceWith('fileABCD', { ids: fileNodeIds, version: 'versionABCD' });
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: 1 });
expect(clientFile.secondCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', ids: undefined });
expect(clientFile).to.have.been.calledOnce;
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: undefined, ids: undefined });

expect(logger).to.have.been.callCount(4);
expect(logger.getCall(0)).to.have.been.calledWith('fetching document');
Expand All @@ -74,16 +74,17 @@ describe('export-styles', () => {
const pagesWithSvg = await exportStyles({
fileId: 'fileABCD',
version: 'versionABCD',
onlyFromPages: ['octicons-by-github'],
onlyFromPages: ['icons/octicons-by-github'],
token: 'token1234',
log: logger,
outputters: [outputter],
});

expect(FigmaExport.getClient).to.have.been.calledOnceWithExactly('token1234');
expect(clientFileNodes).to.have.been.calledOnceWith('fileABCD', { ids: fileNodeIds, version: 'versionABCD' });
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: 1 });
expect(clientFile.secondCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', ids: ['254:0'] });
expect(clientFile).to.have.been.calledTwice;
expect(clientFile.firstCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: 1, ids: undefined });
expect(clientFile.secondCall).to.have.been.calledWith('fileABCD', { version: 'versionABCD', depth: undefined, ids: ['254:0'] });

expect(logger).to.have.been.callCount(4);
expect(logger.getCall(0)).to.have.been.calledWith('fetching document');
Expand Down Expand Up @@ -118,8 +119,7 @@ describe('export-styles', () => {
});

it('should throw an error when fetching styles fails', async () => {
clientFile.onFirstCall().returns(Promise.resolve({ data: { document: file.document } }));
clientFile.onSecondCall().returns(Promise.reject(new Error('some error')));
clientFile.onFirstCall().returns(Promise.reject(new Error('some error')));

await expect(exportStyles({
fileId: 'fileABCD',
Expand All @@ -133,7 +133,7 @@ describe('export-styles', () => {
await expect(exportStyles({
fileId: 'fileABCD',
token: 'token1234',
})).to.be.rejectedWith(Error, '\'document\' is missing.');
})).to.be.rejectedWith(Error, '\'styles\' are missing.');
});

it('should throw an error if styles property is missing when fetching file', async () => {
Expand Down
22 changes: 10 additions & 12 deletions packages/core/src/lib/export-styles.ts
@@ -1,6 +1,6 @@
import * as FigmaExport from '@figma-export/types';

import { getPages, getClient } from './figma';
import { getClient, getStyles } from './figma';
import { fetchStyles, parseStyles } from './figmaStyles';

export const styles: FigmaExport.StylesCommand = async ({
Expand All @@ -17,19 +17,17 @@ export const styles: FigmaExport.StylesCommand = async ({
const client = getClient(token);

log('fetching document');
const { data: { document = null } = {} } = await client.file(fileId, { version, depth: 1 }).catch((error: Error) => {
throw new Error(`while fetching file "${fileId}${version ? `?version=${version}` : ''}": ${error.message}`);
});

if (!document) {
throw new Error('\'document\' is missing.');
}

const ids = getPages(document, onlyFromPages)
.map((page) => page.id);
const figmaStyles = await getStyles(
client,
{
fileId,
version,
onlyFromPages,
},
);

log('fetching styles');
const styleNodes = await fetchStyles(client, fileId, version, onlyFromPages.length > 0 ? ids : undefined);
const styleNodes = await fetchStyles(client, fileId, figmaStyles, version);

log('parsing styles');
const parsedStyles = parseStyles(styleNodes);
Expand Down
44 changes: 18 additions & 26 deletions packages/core/src/lib/figma.test.ts
Expand Up @@ -18,6 +18,17 @@ describe('figma.', () => {
sinon.restore();
});

describe('', () => {
it('should throw an error if styles are not present', async () => {
const client = {
...({} as Figma.ClientInterface),
file: sinon.stub().resolves({ data: {} }),
};

await expect(figma.getStyles(client, { fileId: 'ABC123' })).to.be.rejectedWith(Error, '\'styles\' are missing.');
});
});

describe('getComponents', () => {
it('should get zero results if no children are provided', () => {
expect(figma.getComponents()).to.eql([]);
Expand All @@ -34,7 +45,7 @@ describe('figma.', () => {
});
});

describe('getPages', () => {
describe('getPagesWithComponents', () => {
const document = figmaDocument.createDocument({ children: [figmaDocument.page1, figmaDocument.page2] });

it('should get all pages by default', () => {
Expand All @@ -43,33 +54,16 @@ describe('figma.', () => {
.to.contain.an.item.with.property('name', 'page2');
});

it('should get all pages if "empty" list is provided', () => {
expect(figma.getPagesWithComponents(document, { only: [''] }))
.to.contain.an.item.with.property('name', 'page1')
.to.contain.an.item.with.property('name', 'page2');

expect(figma.getPagesWithComponents(document, { only: [] }))
.to.contain.an.item.with.property('name', 'page1')
.to.contain.an.item.with.property('name', 'page2');

expect(figma.getPagesWithComponents(document, { only: '' }))
it('should get all the pages from the document', () => {
expect(figma.getPagesWithComponents(document))
.to.contain.an.item.with.property('name', 'page1')
.to.contain.an.item.with.property('name', 'page2');
});

it('should get all requested pages', () => {
expect(figma.getPagesWithComponents(document, { only: 'page2' }))
it('should be able to filter components', () => {
expect(figma.getPagesWithComponents(document, { filterComponent: (component) => ['9:1'].includes(component.id) }))
.to.not.contain.an.item.with.property('name', 'page1')
.to.contain.an.item.with.property('name', 'page2');

expect(figma.getPagesWithComponents(document, { only: ['page1', 'page2'] }))
.to.contain.an.item.with.property('name', 'page1')
.to.contain.an.item.with.property('name', 'page2');
});

it('should get zero results if a non existing page is provided', () => {
expect(figma.getPagesWithComponents(document, { only: 'page20' }))
.to.be.an('array').that.is.empty;
});

it('should excludes pages without components', () => {
Expand All @@ -92,11 +86,9 @@ describe('figma.', () => {
describe('getIdsFromPages', () => {
it('should get component ids from specified pages', () => {
const document = figmaDocument.createDocument({ children: [figmaDocument.page1, figmaDocument.page2] });
const pages = figma.getPagesWithComponents(document, {
only: 'page2',
});
const pages = figma.getPagesWithComponents(document);

expect(figma.getIdsFromPages(pages)).to.eql(['9:1']);
expect(figma.getIdsFromPages(pages)).to.eql(['10:8', '8:1', '9:1']);
});
});

Expand Down

0 comments on commit aeb0c20

Please sign in to comment.