Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
feerglas committed Aug 19, 2020
1 parent 8db95fe commit 89c3134
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 112 deletions.
28 changes: 28 additions & 0 deletions figmaExtractor/figmaApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const axios = require('axios');

module.exports = async (config) => {
try {

const {
token,
file
} = config;

const requestHeaders = {
'X-Figma-Token': token
};

const requestConfig = {
method: 'GET',
headers: requestHeaders,
url: file
};

const figmaJson = await axios.request(requestConfig);

return figmaJson.data;

} catch (e) {
throw new Error('figmaApi.js: ' + e.message);
}
}
51 changes: 51 additions & 0 deletions figmaExtractor/figmaExtractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const shell = require('shelljs');
// const fs = require('fs');
const figmaApi = require('./figmaApi');
const getFigmaFrames = require('./figmaFrames');

require('dotenv').config();

// general configuration
// const config = {
// destinationFileName: './figma_extractor/figma.json'
// };

(async () => {

try {

const apiConfig = {
token: process.env.FIGMA_ACCESS_TOKEN,
file: process.env.FIGMA_FILE_URL
};

const figmaData = await figmaApi(apiConfig);
const figmaFrames = getFigmaFrames(figmaData);

console.log(figmaFrames);
// const figmaJson = await getFigmaJSON();

// const figmaStyles = getFigmaStyles(figmaJson);
// const fillStyleOfWhiteBox = figmaFrames[0].children[1].styles.fill;
// console.log(getFigmaStyleNameForId(fillStyleOfWhiteBox, figmaStyles));

// console.log(figmaFrames[0].children[1].fills[0]);

/*
// write .json file
fs.mkdirSync(deploymentsDir);
fs.writeFileSync(`./${deploymentsDir}/${config.deploymentsJsonName}`, JSON.stringify(json));
// write index file
fs.writeFileSync(`./${deploymentsDir}/index.html`, '<html><body><p>This space is empty. It is only serving deployments.json for lyne-design-system. <a href="/deployments.json">deployments.json</a></p></body></html>');
console.log(`-->> NETLIFY DEPLOYMENTS: Successfully created ${config.deploymentsJsonName}`);
shell.exit(0);
*/
} catch (error) {
console.log(error);
shell.exit(0);
}
})();
39 changes: 39 additions & 0 deletions figmaExtractor/figmaFrames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Get frames from 1st page of the Figma file
module.exports = (figmaData) => {

// make sure that file is not empty
const figmaDocument = figmaData.document;

if (!figmaDocument) {
throw new Error('ERROR: Figma file seems to be empty');
}

// make sure that file has pages
const figmaPages = figmaDocument.children;

if (!figmaPages || figmaPages.length < 1) {
throw new Error('ERROR: Figma file seems not to have any pages');
}

// get the first page. By convention, we put all definitions on
// the first page
const figmaPage = figmaPages[0];

// make sure that 1st page has regions
let figmaChildren = figmaPage.children;

if (!figmaChildren || figmaChildren.length < 1) {
throw new Error('ERROR: 1st page of the Figma file does not have any children');
}

const figmaFrames = figmaChildren.filter((frame) => {
return frame.type === 'FRAME';
});

if (figmaFrames.length < 1) {
throw new Error('ERROR: 1st page of the Figma file does not have any frames');
}

return figmaFrames;

}
Empty file added figmaExtractor/figmaJson.js
Empty file.
111 changes: 0 additions & 111 deletions figma_extractor/figma_extractor.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
],
"scripts": {
"build": "node ./build.js",
"figma:extract": "node figma_extractor/figma_extractor.js",
"figma:extract": "node figmaExtractor/figmaExtractor.js",
"lint": "npm run lint:json && npm run lint:yml",
"lint:json": "eslint ./ --ext .json",
"lint:yml": "eslint ./ --ext .yml,.yaml",
Expand Down

0 comments on commit 89c3134

Please sign in to comment.