Skip to content

Commit

Permalink
Replace 'variablePrefix' and 'variableSuffix' with 'getVariableName' …
Browse files Browse the repository at this point in the history
…(output-components-as-es6)
  • Loading branch information
marcomontalbano committed Jan 28, 2020
1 parent b46db83 commit a84402c
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 48 deletions.
26 changes: 18 additions & 8 deletions packages/output-components-as-es6/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ const fs = require('fs');
const path = require('path');
const makeDir = require('make-dir');
const svgToMiniDataURI = require('mini-svg-data-uri');

const { getVariableName } = require('./utils');
const { camelCase } = require('@figma-export/output-components-utils');

module.exports = ({
output,
variablePrefix = '',
variableSuffix = '',
getVariableName = (options) => camelCase(options.componentName.trim()),
useBase64 = false,
useDataUrl = false,
}) => {
Expand All @@ -17,18 +15,30 @@ module.exports = ({
pages.forEach(({ name: pageName, components }) => {
let code = '';

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

const variableName = getVariableName(options);
let variableValue;

if (/^[\d]+/.test(variableName)) {
throw new Error(`"${componentName.trim()}" thrown an error: component names cannot start with a number.`);
}

// eslint-disable-next-line default-case
switch (true) {
case useBase64:
variableValue = Buffer.from(svg).toString('base64');
break;
case useDataUrl:
variableValue = svgToMiniDataURI(svg);
break;
default:
variableValue = svg;
break;
}

if (code.includes(`export const ${variableName} =`)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/output-components-as-es6/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { expect } = chai;

const fs = require('fs');
const { camelCase } = require('@figma-export/output-components-utils');

const figmaDocument = require('../core/lib/_config.test');
const figma = require('../core/lib/figma');
Expand Down Expand Up @@ -36,8 +37,7 @@ describe('outputter as es6', () => {

await outputter({
output: 'output',
variablePrefix: 'i',
variableSuffix: 'my ico',
getVariableName: (options) => camelCase(`i ${options.componentName} my ico`),
})(pages);

expect(writeFileSync).to.be.calledOnce;
Expand Down
1 change: 1 addition & 0 deletions packages/output-components-as-es6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"access": "public"
},
"dependencies": {
"@figma-export/output-components-utils": "^1.2.0",
"make-dir": "~3.0.0",
"mini-svg-data-uri": "~1.1.3"
}
Expand Down
21 changes: 0 additions & 21 deletions packages/output-components-as-es6/utils.js

This file was deleted.

17 changes: 0 additions & 17 deletions packages/output-components-as-es6/utils.test.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/output-components-as-svg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = ({
componentName,
...figmaExport,
};

const filePath = makeDir.sync(path.resolve(output, getDirname(options)));
fs.writeFileSync(path.resolve(filePath, getBasename(options)), svg);
});
Expand Down
17 changes: 17 additions & 0 deletions packages/output-components-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @figma-export/output-components-utils

> Utils for [@figma-export](https://github.com/marcomontalbano/figma-export) outputters.
## Install

Using npm:

```sh
npm install --save-dev @figma-export/output-components-utils
```

or using yarn:

```sh
yarn add @figma-export/output-components-utils --dev
```
11 changes: 11 additions & 0 deletions packages/output-components-utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const camelCase = (str) => str.replace(/[\])}]+/g, '').replace(/^([A-Z])|[\s-_([/\\{]+(\w)/g, (match, p1, p2) => {
if (p2) {
return p2.toUpperCase();
}

return p1.toLowerCase();
});

module.exports = {
camelCase,
};
17 changes: 17 additions & 0 deletions packages/output-components-utils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { expect } = chai;

const utils = require('./index');

describe('utils', () => {
it('getVariableName', async () => {
expect(utils.camelCase('a')).to.eql('a');
expect(utils.camelCase('a word')).to.eql('aWord');
expect(utils.camelCase('a-word')).to.eql('aWord');
expect(utils.camelCase('a/word')).to.eql('aWord');
expect(utils.camelCase('a\\word')).to.eql('aWord');
expect(utils.camelCase('a[wor]d')).to.eql('aWord');
expect(utils.camelCase('a [word]')).to.eql('aWord');
expect(utils.camelCase('a (word)')).to.eql('aWord');
expect(utils.camelCase('a {word}')).to.eql('aWord');
});
});
26 changes: 26 additions & 0 deletions packages/output-components-utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions packages/output-components-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@figma-export/output-components-utils",
"version": "1.2.0",
"description": "Utils for @figma-export outputters",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/marcomontalbano/figma-exporter.git",
"directory": "packages/output-components-utils"
},
"author": "Marco Montalbano <me@marcomontalbano.com>",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {

}
}

0 comments on commit a84402c

Please sign in to comment.