figma-metadata-extractor 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +22 -7
- package/dist/index.js +22 -7
- package/dist/lib.d.ts +8 -3
- package/dist/utils/logger.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -124,6 +124,7 @@ function generateImageCSSVariables({
|
|
|
124
124
|
}
|
|
125
125
|
const Logger = {
|
|
126
126
|
isHTTP: false,
|
|
127
|
+
enableLogging: false,
|
|
127
128
|
log: (...args) => {
|
|
128
129
|
if (Logger.isHTTP) {
|
|
129
130
|
console.log("[INFO]", ...args);
|
|
@@ -136,6 +137,7 @@ const Logger = {
|
|
|
136
137
|
}
|
|
137
138
|
};
|
|
138
139
|
function writeLogs(name, value) {
|
|
140
|
+
if (!Logger.enableLogging) return;
|
|
139
141
|
if (process.env.NODE_ENV !== "development") return;
|
|
140
142
|
try {
|
|
141
143
|
const logsDir = "logs";
|
|
@@ -1375,8 +1377,10 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1375
1377
|
localPath,
|
|
1376
1378
|
imageFormat = "png",
|
|
1377
1379
|
pngScale = 2,
|
|
1378
|
-
useRelativePaths = true
|
|
1380
|
+
useRelativePaths = true,
|
|
1381
|
+
enableLogging = false
|
|
1379
1382
|
} = options;
|
|
1383
|
+
Logger.enableLogging = enableLogging;
|
|
1380
1384
|
if (!apiKey && !oauthToken) {
|
|
1381
1385
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1382
1386
|
}
|
|
@@ -1433,7 +1437,7 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1433
1437
|
imageNodes,
|
|
1434
1438
|
{ pngScale: imageFormat === "png" ? pngScale : void 0 }
|
|
1435
1439
|
);
|
|
1436
|
-
result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths);
|
|
1440
|
+
result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths, localPath);
|
|
1437
1441
|
Logger.log(`Successfully downloaded and enriched ${downloadResults.length} images`);
|
|
1438
1442
|
}
|
|
1439
1443
|
}
|
|
@@ -1451,7 +1455,8 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1451
1455
|
}
|
|
1452
1456
|
}
|
|
1453
1457
|
async function downloadFigmaImages(figmaUrl, nodes, options) {
|
|
1454
|
-
const { apiKey, oauthToken, useOAuth = false, pngScale = 2, localPath } = options;
|
|
1458
|
+
const { apiKey, oauthToken, useOAuth = false, pngScale = 2, localPath, enableLogging = false } = options;
|
|
1459
|
+
Logger.enableLogging = enableLogging;
|
|
1455
1460
|
if (!apiKey && !oauthToken) {
|
|
1456
1461
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1457
1462
|
}
|
|
@@ -1487,8 +1492,10 @@ async function downloadFigmaFrameImage(figmaUrl, options) {
|
|
|
1487
1492
|
pngScale = 2,
|
|
1488
1493
|
localPath,
|
|
1489
1494
|
fileName,
|
|
1490
|
-
format = "png"
|
|
1495
|
+
format = "png",
|
|
1496
|
+
enableLogging = false
|
|
1491
1497
|
} = options;
|
|
1498
|
+
Logger.enableLogging = enableLogging;
|
|
1492
1499
|
if (!apiKey && !oauthToken) {
|
|
1493
1500
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1494
1501
|
}
|
|
@@ -1557,7 +1564,7 @@ function hasImageFill(node, globalVars) {
|
|
|
1557
1564
|
}
|
|
1558
1565
|
return fillData.some((fill) => fill?.type === "IMAGE");
|
|
1559
1566
|
}
|
|
1560
|
-
function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true) {
|
|
1567
|
+
function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true, localPath) {
|
|
1561
1568
|
const imageMap = /* @__PURE__ */ new Map();
|
|
1562
1569
|
imageAssets.forEach((asset, index) => {
|
|
1563
1570
|
const result = downloadResults[index];
|
|
@@ -1566,9 +1573,17 @@ function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativeP
|
|
|
1566
1573
|
if (useRelativePaths === false) {
|
|
1567
1574
|
pathForMarkup = result.filePath;
|
|
1568
1575
|
} else if (typeof useRelativePaths === "string") {
|
|
1569
|
-
|
|
1576
|
+
const basePath = useRelativePaths.endsWith("/") ? useRelativePaths : useRelativePaths + "/";
|
|
1577
|
+
const normalizedFilePath = result.filePath.replace(/\\/g, "/");
|
|
1578
|
+
const normalizedBasePath = basePath.replace(/\\/g, "/");
|
|
1579
|
+
if (normalizedFilePath.startsWith(normalizedBasePath)) {
|
|
1580
|
+
pathForMarkup = "./" + normalizedFilePath.substring(normalizedBasePath.length);
|
|
1581
|
+
} else {
|
|
1582
|
+
pathForMarkup = "./" + result.filePath.split(/[/\\]/).pop();
|
|
1583
|
+
}
|
|
1570
1584
|
} else {
|
|
1571
|
-
|
|
1585
|
+
const fileName = result.filePath.split(/[/\\]/).pop();
|
|
1586
|
+
pathForMarkup = "./" + fileName;
|
|
1572
1587
|
}
|
|
1573
1588
|
imageMap.set(asset.id, {
|
|
1574
1589
|
filePath: result.filePath,
|
package/dist/index.js
CHANGED
|
@@ -122,6 +122,7 @@ function generateImageCSSVariables({
|
|
|
122
122
|
}
|
|
123
123
|
const Logger = {
|
|
124
124
|
isHTTP: false,
|
|
125
|
+
enableLogging: false,
|
|
125
126
|
log: (...args) => {
|
|
126
127
|
if (Logger.isHTTP) {
|
|
127
128
|
console.log("[INFO]", ...args);
|
|
@@ -134,6 +135,7 @@ const Logger = {
|
|
|
134
135
|
}
|
|
135
136
|
};
|
|
136
137
|
function writeLogs(name, value) {
|
|
138
|
+
if (!Logger.enableLogging) return;
|
|
137
139
|
if (process.env.NODE_ENV !== "development") return;
|
|
138
140
|
try {
|
|
139
141
|
const logsDir = "logs";
|
|
@@ -1373,8 +1375,10 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1373
1375
|
localPath,
|
|
1374
1376
|
imageFormat = "png",
|
|
1375
1377
|
pngScale = 2,
|
|
1376
|
-
useRelativePaths = true
|
|
1378
|
+
useRelativePaths = true,
|
|
1379
|
+
enableLogging = false
|
|
1377
1380
|
} = options;
|
|
1381
|
+
Logger.enableLogging = enableLogging;
|
|
1378
1382
|
if (!apiKey && !oauthToken) {
|
|
1379
1383
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1380
1384
|
}
|
|
@@ -1431,7 +1435,7 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1431
1435
|
imageNodes,
|
|
1432
1436
|
{ pngScale: imageFormat === "png" ? pngScale : void 0 }
|
|
1433
1437
|
);
|
|
1434
|
-
result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths);
|
|
1438
|
+
result.nodes = enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths, localPath);
|
|
1435
1439
|
Logger.log(`Successfully downloaded and enriched ${downloadResults.length} images`);
|
|
1436
1440
|
}
|
|
1437
1441
|
}
|
|
@@ -1449,7 +1453,8 @@ async function getFigmaMetadata(figmaUrl, options = {}) {
|
|
|
1449
1453
|
}
|
|
1450
1454
|
}
|
|
1451
1455
|
async function downloadFigmaImages(figmaUrl, nodes, options) {
|
|
1452
|
-
const { apiKey, oauthToken, useOAuth = false, pngScale = 2, localPath } = options;
|
|
1456
|
+
const { apiKey, oauthToken, useOAuth = false, pngScale = 2, localPath, enableLogging = false } = options;
|
|
1457
|
+
Logger.enableLogging = enableLogging;
|
|
1453
1458
|
if (!apiKey && !oauthToken) {
|
|
1454
1459
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1455
1460
|
}
|
|
@@ -1485,8 +1490,10 @@ async function downloadFigmaFrameImage(figmaUrl, options) {
|
|
|
1485
1490
|
pngScale = 2,
|
|
1486
1491
|
localPath,
|
|
1487
1492
|
fileName,
|
|
1488
|
-
format = "png"
|
|
1493
|
+
format = "png",
|
|
1494
|
+
enableLogging = false
|
|
1489
1495
|
} = options;
|
|
1496
|
+
Logger.enableLogging = enableLogging;
|
|
1490
1497
|
if (!apiKey && !oauthToken) {
|
|
1491
1498
|
throw new Error("Either apiKey or oauthToken is required");
|
|
1492
1499
|
}
|
|
@@ -1555,7 +1562,7 @@ function hasImageFill(node, globalVars) {
|
|
|
1555
1562
|
}
|
|
1556
1563
|
return fillData.some((fill) => fill?.type === "IMAGE");
|
|
1557
1564
|
}
|
|
1558
|
-
function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true) {
|
|
1565
|
+
function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativePaths = true, localPath) {
|
|
1559
1566
|
const imageMap = /* @__PURE__ */ new Map();
|
|
1560
1567
|
imageAssets.forEach((asset, index) => {
|
|
1561
1568
|
const result = downloadResults[index];
|
|
@@ -1564,9 +1571,17 @@ function enrichNodesWithImages(nodes, imageAssets, downloadResults, useRelativeP
|
|
|
1564
1571
|
if (useRelativePaths === false) {
|
|
1565
1572
|
pathForMarkup = result.filePath;
|
|
1566
1573
|
} else if (typeof useRelativePaths === "string") {
|
|
1567
|
-
|
|
1574
|
+
const basePath = useRelativePaths.endsWith("/") ? useRelativePaths : useRelativePaths + "/";
|
|
1575
|
+
const normalizedFilePath = result.filePath.replace(/\\/g, "/");
|
|
1576
|
+
const normalizedBasePath = basePath.replace(/\\/g, "/");
|
|
1577
|
+
if (normalizedFilePath.startsWith(normalizedBasePath)) {
|
|
1578
|
+
pathForMarkup = "./" + normalizedFilePath.substring(normalizedBasePath.length);
|
|
1579
|
+
} else {
|
|
1580
|
+
pathForMarkup = "./" + result.filePath.split(/[/\\]/).pop();
|
|
1581
|
+
}
|
|
1568
1582
|
} else {
|
|
1569
|
-
|
|
1583
|
+
const fileName = result.filePath.split(/[/\\]/).pop();
|
|
1584
|
+
pathForMarkup = "./" + fileName;
|
|
1570
1585
|
}
|
|
1571
1586
|
imageMap.set(asset.id, {
|
|
1572
1587
|
filePath: result.filePath,
|
package/dist/lib.d.ts
CHANGED
|
@@ -18,12 +18,15 @@ export interface FigmaMetadataOptions {
|
|
|
18
18
|
/** Export scale for PNG images (defaults to 2) */
|
|
19
19
|
pngScale?: number;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* Control how image paths are generated in downloadedImage properties.
|
|
22
|
+
* - true (default): Just the filename (e.g., "./icon.png")
|
|
23
|
+
* - false: Absolute file path (e.g., "/absolute/path/to/images/icon.png")
|
|
24
|
+
* - string: Strip this base path from file path (e.g., "/var/www" → "./images/icon.png")
|
|
24
25
|
* Default: true
|
|
25
26
|
*/
|
|
26
27
|
useRelativePaths?: boolean | string;
|
|
28
|
+
/** Enable JSON debug log files (defaults to false) */
|
|
29
|
+
enableLogging?: boolean;
|
|
27
30
|
}
|
|
28
31
|
export interface FigmaImageOptions {
|
|
29
32
|
/** Export scale for PNG images (defaults to 2) */
|
|
@@ -76,6 +79,8 @@ export interface FigmaFrameImageOptions {
|
|
|
76
79
|
fileName: string;
|
|
77
80
|
/** Image format to download (defaults to 'png') */
|
|
78
81
|
format?: 'png' | 'svg';
|
|
82
|
+
/** Enable JSON debug log files (defaults to false) */
|
|
83
|
+
enableLogging?: boolean;
|
|
79
84
|
}
|
|
80
85
|
/**
|
|
81
86
|
* Extract metadata from a Figma file or specific nodes
|
package/dist/utils/logger.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "figma-metadata-extractor",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Extract metadata and download images from Figma files. A standalone library for accessing Figma design data and downloading frame images programmatically.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|