Skip to content

Commit

Permalink
Merge 2f65def into 4c1b892
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Nov 29, 2019
2 parents 4c1b892 + 2f65def commit ab74f6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/output-components-as-es6/index.js
Expand Up @@ -9,10 +9,11 @@ const camelCase = (str) => str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2)
});

const getVariableName = (componentName) => {
const variableName = camelCase(componentName);
const trimmedComponentName = componentName.trim();
const variableName = camelCase(trimmedComponentName);

if (/^[\d]+/.test(variableName)) {
throw new Error(`"${componentName}" - Component names cannot start with a number.`);
throw new Error(`"${trimmedComponentName}" - Component names cannot start with a number.`);
}

return variableName;
Expand All @@ -31,7 +32,7 @@ module.exports = ({
let code = '';

components.forEach(({ name: componentName, svg }) => {
const variableName = getVariableName(componentName);
const variableName = getVariableName(`${variablePrefix} ${componentName} ${variableSuffix}`);
let variableValue = svg;

// eslint-disable-next-line default-case
Expand All @@ -44,7 +45,7 @@ module.exports = ({
break;
}

code += `export const ${variablePrefix}${variableName}${variableSuffix} = \`${variableValue}\`;\n`;
code += `export const ${variableName} = \`${variableValue}\`;\n`;
});

const filePath = path.resolve(output, `${pageName}.js`);
Expand Down
17 changes: 17 additions & 0 deletions packages/output-components-as-es6/index.test.js
Expand Up @@ -30,6 +30,23 @@ describe('outputter as es6', () => {
);
});

it('should use "variablePrefix" and "variableSuffix" options to prepend or append a text to the variable name', async () => {
const writeFileSync = sinon.stub(fs, 'writeFileSync');
const pages = figma.getPages({ children: [figmaDocument.page1] });

await outputter({
output: 'output',
variablePrefix: 'i',
variableSuffix: 'my ico',
})(pages);

expect(writeFileSync).to.be.calledOnce;
expect(writeFileSync).to.be.calledWithMatch(
'output/page1.js',
'export const iFigmaLogoMyIco = `<svg width="40" height="60" viewBox="0 0 40 60" fill="none" xmlns="http://www.w3.org/2000/svg"></svg>`;',
);
});

it('should export all components into an es6 file using base64 encoding if set', async () => {
const writeFileSync = sinon.stub(fs, 'writeFileSync');
const pages = figma.getPages({ children: [figmaDocument.page1] });
Expand Down

0 comments on commit ab74f6d

Please sign in to comment.