-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up and code readability improvements (#57)
- Loading branch information
1 parent
fcd2b02
commit bc109c2
Showing
32 changed files
with
668 additions
and
497 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ lerna-debug.log | |
*.tsbuildinfo | ||
|
||
/.figmaexportrc.js | ||
/.figmaexportrc.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
'!(*.d).{js,ts}': () => ['yarn lint --fix', 'yarn coverage'], | ||
'!(*.d).{js,ts}': () => ['yarn lint --fix', 'yarn coverage', 'git add .'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
|
||
import * as Figma from 'figma-js'; | ||
|
||
import * as FigmaExport from './figma'; | ||
|
||
import { styles as exportStyles } from './export-styles'; | ||
|
||
import file from './_mocks_/figma.files.json'; | ||
import fileNodes from './_mocks_/figma.fileNodes.json'; | ||
|
||
const nodeIds = Object.keys(fileNodes.nodes); | ||
|
||
describe('export-styles', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let logger: sinon.SinonSpy<any[], any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let outputter: sinon.SinonSpy<any[], any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let clientFileNodes: sinon.SinonStub<any[], any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let clientFile: sinon.SinonStub<any[], any>; | ||
|
||
let client: Figma.ClientInterface; | ||
|
||
beforeEach(() => { | ||
logger = sinon.spy(); | ||
outputter = sinon.spy(); | ||
|
||
clientFile = sinon.stub().resolves({ data: file }); | ||
clientFileNodes = sinon.stub().resolves({ data: fileNodes }); | ||
|
||
client = { | ||
...({} as Figma.ClientInterface), | ||
file: clientFile, | ||
fileNodes: clientFileNodes, | ||
}; | ||
|
||
sinon.stub(FigmaExport, 'getClient').returns(client); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should use outputter to export styles', async () => { | ||
const pagesWithSvg = await exportStyles({ | ||
fileId: 'fileABCD', | ||
token: 'token1234', | ||
log: logger, | ||
outputters: [outputter], | ||
}); | ||
|
||
expect(FigmaExport.getClient).to.have.been.calledOnceWithExactly('token1234'); | ||
expect(clientFileNodes).to.have.been.calledOnceWith('fileABCD', { ids: nodeIds }); | ||
expect(clientFile).to.have.been.calledOnceWithExactly('fileABCD'); | ||
|
||
expect(logger).to.have.been.calledTwice; | ||
expect(logger.firstCall).to.have.been.calledWith('fetching styles'); | ||
expect(logger.secondCall).to.have.been.calledWith('parsing styles'); | ||
|
||
expect(outputter).to.have.been.calledOnceWithExactly(pagesWithSvg); | ||
}); | ||
|
||
it('should use default "logger" if not defined', async () => { | ||
await exportStyles({ | ||
fileId: 'fileABCD', | ||
token: 'token1234', | ||
}); | ||
|
||
/* eslint-disable no-console */ | ||
expect(console.log).to.have.been.calledTwice; | ||
expect((console.log as sinon.SinonSpy<unknown[], unknown>).firstCall).to.have.been.calledWith('fetching styles'); | ||
expect((console.log as sinon.SinonSpy<unknown[], unknown>).secondCall).to.have.been.calledWith('parsing styles'); | ||
}); | ||
|
||
it('should throw an error if fetching styles fails', async () => { | ||
clientFile.returns({}); | ||
|
||
await expect(exportStyles({ | ||
fileId: 'fileABCD', | ||
token: 'token1234', | ||
})).to.be.rejectedWith(Error, '\'styles\' are missing.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,36 @@ | ||
import { FigmaExport } from '@figma-export/types'; | ||
import * as FigmaExport from '@figma-export/types'; | ||
|
||
import { getClient } from './figma'; | ||
import { fetchStyles, parseFigmaStyles } from './figmaStyles'; | ||
import { fetchStyles, parseStyles } from './figmaStyles'; | ||
|
||
type Options = { | ||
token: string; | ||
fileId: string; | ||
onlyFromPages?: string[]; | ||
transformers?: FigmaExport.StringTransformer[]; | ||
outputters?: FigmaExport.Outputter[]; | ||
// transformers?: FigmaExport.StringTransformer[]; | ||
outputters?: FigmaExport.StyleOutputter[]; | ||
log?: (msg: string) => void; | ||
} | ||
|
||
export const styles = async ({ | ||
token, | ||
fileId, | ||
// transformers = [], | ||
// outputters = [], | ||
outputters = [], | ||
log = (msg): void => { | ||
// eslint-disable-next-line no-console | ||
console.log(msg); | ||
}, | ||
}: Options): Promise<number[]> => { | ||
}: Options): Promise<FigmaExport.Style[]> => { | ||
const client = getClient(token); | ||
|
||
log('fetching styles'); | ||
const styleNodes = await fetchStyles(client, fileId); | ||
|
||
log('parsing styles'); | ||
// TODO: convert figma Styles to CSS Like | ||
const parsedStyles = await parseFigmaStyles(styleNodes); | ||
console.log(JSON.stringify(parsedStyles, undefined, 4)); | ||
const parsedStyles = await parseStyles(styleNodes); | ||
|
||
// TODO: send the parsed style to outputter | ||
// await Promise.all(outputters.map((outputter) => outputter(parsedStyles))); | ||
await Promise.all(outputters.map((outputter) => outputter(parsedStyles))); | ||
|
||
return []; | ||
return parsedStyles; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.