-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,618 additions
and
2,708 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
node_modules | ||
.env | ||
src/svg | ||
src/svg | ||
|
||
built/ | ||
# TS -incremental file | ||
tsconfig.tsbuildinfo |
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,11 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
roots: ['<rootDir>/src'], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest', | ||
'^.+\\.(js|jsx)$': 'ts-jest', | ||
}, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['node_modules/'], | ||
} |
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,18 +1,26 @@ | ||
{ | ||
"name": "export-figma-svg", | ||
"version": "0.0.1", | ||
"version": "2.0.0", | ||
"description": "Export SVGs from Figma", | ||
"main": "index.js", | ||
"main": "index.ts", | ||
"scripts": { | ||
"test": "TZ=Asia/Singapore jest" | ||
}, | ||
"author": "Jacob Tan <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^0.21.1", | ||
"dotenv": "^10.0.0", | ||
"axios": "^0.27.2", | ||
"dotenv": "^16.0.1", | ||
"fs": "^0.0.1-security", | ||
"jest": "^26.6.3", | ||
"pg": "^8.7.1" | ||
"pg": "^8.7.1", | ||
"ts-node": "^10.9.1", | ||
"tsc": "^2.0.4", | ||
"typescript": "^4.8.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.7.14", | ||
"ts-jest": "^28.0.8", | ||
"jest": "^29.0.1" | ||
} | ||
} |
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,4 @@ | ||
export const OUTPUT_FOLDER = "./src/svg/"; | ||
export const RATE_LIMIT = 20; | ||
export const WAIT_TIME_IN_SECONDS = 45; | ||
export const ENV_OUTPUT_FILE = '.env' |
This file was deleted.
Oops, something went wrong.
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 'dotenv/config' | ||
import axios from 'axios' | ||
import figmaRestApi from './api/' | ||
import { | ||
writeToFile, | ||
findAllByValue, | ||
filterPrivateComponents, | ||
camelCaseToDash, | ||
createFolder, | ||
} from './utils' | ||
import { OUTPUT_FOLDER, RATE_LIMIT, WAIT_TIME_IN_SECONDS } from './constants' | ||
|
||
const getProjectNode = async () => { | ||
return await figmaRestApi.get( | ||
'files/' + | ||
process.env.FIGMA_PROJECT_ID + | ||
'/nodes?ids=' + | ||
process.env.FIGMA_PROJECT_NODE_ID | ||
) | ||
} | ||
|
||
const getSVGURL = async (id: string) => { | ||
return await figmaRestApi.get( | ||
'images/' + process.env.FIGMA_PROJECT_ID + '/?ids=' + id + '&format=svg' | ||
) | ||
} | ||
|
||
const svgExporter = async () => { | ||
try { | ||
const response = await getProjectNode() | ||
const children = await response.data.nodes[ | ||
process.env.FIGMA_PROJECT_NODE_ID | ||
].document.children | ||
|
||
// If ignoring private components | ||
let svgs | ||
if (process.env.FILTER_PRIVATE_COMPONENTS === 'false') { | ||
svgs = findAllByValue(children, 'COMPONENT') | ||
} else { | ||
svgs = filterPrivateComponents(findAllByValue(children, 'COMPONENT')) | ||
} | ||
|
||
const numOfSvgs = svgs.length | ||
|
||
console.log('Number of SVGs', numOfSvgs) | ||
|
||
createFolder(OUTPUT_FOLDER) | ||
|
||
for (let i = 0; i < numOfSvgs; i += RATE_LIMIT) { | ||
const requests = svgs.slice(i, i + RATE_LIMIT).map(async (svg) => { | ||
// Get URL of each SVG | ||
let svgName = await svg.name | ||
|
||
if (svgName.includes('/')) { | ||
const nameArr = svg.name.split('/').join('-') | ||
svgName = nameArr | ||
} | ||
|
||
const svgURL = await getSVGURL(svg.id) | ||
|
||
// Get SVG DOM | ||
const svgDOM = await axios.get(svgURL.data.images[svg.id]) | ||
writeToFile( | ||
OUTPUT_FOLDER + `${camelCaseToDash(svgName)}.svg`, | ||
svgDOM.data | ||
) | ||
}) | ||
|
||
await Promise.all(requests) | ||
.then(() => { | ||
console.log(`Wait for ${WAIT_TIME_IN_SECONDS} seconds`) | ||
return new Promise<void>(function (resolve) { | ||
setTimeout(() => { | ||
console.log(`${WAIT_TIME_IN_SECONDS} seconds!`) | ||
resolve() | ||
}, WAIT_TIME_IN_SECONDS * 1000) | ||
}) | ||
}) | ||
.catch((err) => console.error(`Error proccessing ${i} - Error ${err}`)) | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
|
||
svgExporter() |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.