-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
117 lines (104 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const fs = require('fs');
const path = require('path');
const psd = require('psd');
const mkdirp = require('mkdirp');
/**
* Output PSD layout to JSON
* @param {string} psdFile Relative path or absolute path of PSD file
* @param {string|Object} [options] directory path or options
* @param {string} [options.outJsonDir] Set to output files
* @param {string} [options.outImgDir] Set to output files
*/
function psd2json(psdFile, options = {}) {
const psdFilePath = path.resolve(psdFile);
const psdFileName = path.basename(psdFilePath, path.extname(psdFilePath));
let outImgDir = '';
let outJsonDir = '';
if (typeof options === 'string') {
outImgDir = options;
outJsonDir = options;
} else {
if (options.outImgDir) {
outImgDir = options.outImgDir;
}
if (options.outJsonDir) {
outJsonDir = options.outJsonDir;
}
}
// get root node.
const psdData = psd.fromFile(psdFilePath);
psdData.parse();
const rootNode = psdData.tree();
const queueNodes = [];
const queueNodesIndex = [];
const queueNodesName = [];
const queueNodesStructure = [];
queueNodes.push(rootNode._children);
queueNodesIndex.push(0);
queueNodesName.push(undefined);
const psdStructure = {
'group' : []
};
queueNodesStructure.push(psdStructure);
queueLoop: while (0 < queueNodes.length) {
const queueIndex = queueNodes.length - 1;
const nodes = queueNodes[queueIndex];
const nodesStructure = queueNodesStructure[queueIndex];
let nodesIndex = queueNodesIndex[queueIndex];
let nodesName = queueNodesName[queueIndex];
if (nodesName === undefined) {
nodesName = '';
} else {
nodesName += path.sep;
}
while (nodesIndex < nodes.length) {
const node = nodes[nodesIndex];
nodesIndex++;
if (node.layer.visible === false) continue;
if (node.type === 'group') {
queueNodes.push(node._children);
queueNodesIndex[queueIndex] = nodesIndex;
queueNodesIndex.push(0);
queueNodesName.push(nodesName + node.name);
const structure = {
'name' : node.name,
'group' : []
};
nodesStructure.group.push(structure);
queueNodesStructure.push(structure);
continue queueLoop;
} else {
if (outImgDir) {
const outImgDirPath = path.resolve(outImgDir, psdFileName, nodesName);
mkdirp.sync(outImgDirPath);
node.layer.image.saveAsPng(path.join(outImgDirPath, node.name + '.png'));
}
const structure = {
'name' : node.name,
'x' : node.layer.left,
'y' : node.layer.top,
'width' : node.layer.width,
'height' : node.layer.height
};
nodesStructure.group.push(structure);
}
}
queueNodes.pop();
queueNodesIndex.pop();
queueNodesName.pop();
queueNodesStructure.pop();
}
const outJsonData = JSON.stringify(psdStructure.group);
if (outJsonDir) {
const outJsonDirPath = path.resolve(outJsonDir);
const outJsonPath = path.join(outJsonDirPath, psdFileName + '.json');
// make output directory.
if (!fs.existsSync(outJsonDirPath)) {
fs.mkdirSync(outJsonDirPath);
}
// output file.
fs.writeFileSync(outJsonPath, outJsonData);
}
return outJsonData;
}
module.exports = psd2json;