Commit f112f438 authored by rockyl's avatar rockyl

文件名使用hash字符串

节点增加uuid字段
parent a79d745a
{
"name": "psd-parse",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/index.js",
"license": "MIT",
"dependencies": {
"color": "^3.1.2",
"fs-extra": "^8.1.0",
"object-hash": "^1.3.1",
"psd": "^3.2.0",
"uuid": "^3.3.3",
"xml": "^1.0.1"
......
......@@ -7,6 +7,7 @@ var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs-extra'));
var Color = _interopDefault(require('color'));
var generateUUID = _interopDefault(require('uuid/v4'));
var hash = _interopDefault(require('object-hash'));
/**
* Created by rockyl on 2019-08-09.
......@@ -68,7 +69,7 @@ async function walkNode(node, callback, includeSelf = false) {
* 导出zeroing的视图
*/
async function execute$1(psdFile, options) {
async function execute(psdFile, options) {
const {
imagesPath,
} = options;
......@@ -83,13 +84,14 @@ async function execute$1(psdFile, options) {
const assets = [];
await walkNode(tree, async function (node, parent) {
const {x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;
const {name, x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;
let properties = {
width, height, alpha, visible,
};
let viewNode = {
name: node.name,
name,
properties,
uuid: generateUUID(),
};
if (x !== 0) {
properties.x = x;
......@@ -98,8 +100,8 @@ async function execute$1(psdFile, options) {
properties.y = y;
}
if(typeTool){
let fontInfo= typeTool();
if (typeTool) {
let fontInfo = typeTool();
const fonts = fontInfo.fonts();
const styles = fontInfo.styles();
const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
......@@ -109,30 +111,35 @@ async function execute$1(psdFile, options) {
fonts, styles, RunLengthArray,
};
viewNode.type = 'label';
}else if(solidColor){
} else if (solidColor) {
const {r, g, b} = solidColor();
let color = Color({r, g, b});
viewNode.type = 'rect';
properties.fillColor = '#' + color.rgbNumber().toString(16);
}else{
if(node.hasOwnProperty('children')){
} else {
if (node.hasOwnProperty('children')) {
viewNode.type = 'node';
}else{
} else {
viewNode.type = 'image';
const uuid = generateUUID();
const fileName = Date.now().valueOf();
const ext = '.png';
properties.source = 'asset|' + uuid;
const imageFilePath = path.join(imagesPath, fileName + ext);
const imageFilePath = path.join(imagesPath, uuid + ext);
await fs.ensureDir(path.dirname(imageFilePath));
await node.origin.saveAsPng(imageFilePath);
let png = node.origin.toPng();
let buffer = await savePng(png, imageFilePath);
//await node.origin.saveAsPng(imageFilePath);
const hashFileName = hash(buffer);
const hashFilePath = path.join(imagesPath, hashFileName + ext);
await fs.rename(imageFilePath, hashFilePath);
assets.push({
name: fileName,
name,
ext,
uuid,
});
......@@ -154,6 +161,22 @@ async function execute$1(psdFile, options) {
}
}
function savePng(png, output) {
return new Promise((resolve, reject) => {
let buffer, buffers = [];
png.pack()
.on('error', reject)
.on('data', (data) => buffers.push(data))
.on('end', () => {
buffer = Buffer.concat(buffers);
})
.pipe(fs.createWriteStream(output))
.on('finish', () => {
resolve(buffer);
});
});
}
/**
* Created by rockyl on 2019-08-10.
*/
......@@ -161,7 +184,7 @@ async function execute$1(psdFile, options) {
(async function generate() {
const imagesPath = 'zeroing-demo/images_' + Date.now();
const {view, assets} = await execute$1('psd/test.psd', {
const {view, assets} = await execute('psd/test.psd', {
imagesPath,
});
......
{"version":3,"file":"generator.cjs.js","sources":["../../dist/index.js","generator.js"],"sourcesContent":["import xml from 'xml';\nimport path from 'path';\nimport fs from 'fs-extra';\nimport Color from 'color';\nimport generateUUID from 'uuid/v4';\n\n/**\n * Created by rockyl on 2019-08-09.\n */\n\nconst PSD = require('psd');\n\nasync function getTree(psdFilePath) {\n\tconst psd = await PSD.open(psdFilePath);\n\tconst root = {};\n\twalk(psd.tree(), root);\n\n\treturn root;\n}\n\nfunction walk(psNode, dataNode) {\n\tconst {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};\n\tconst {left, top, width, height, name, layer: {opacity, visible}} = psNode;\n\tconst x = left - pLeft;\n\tconst y = top - pTop;\n\n\tObject.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});\n\tif (psNode.children() && psNode.children().length > 0){\n\t\tdataNode.children = [];\n\t}\n\n\tlet children = psNode.children();\n\tfor (let i = children.length - 1; i >= 0; i--) {\n\t\tconst childPsNode = children[i];\n\n\t\tconst childDataNode = {};\n\t\tdataNode.children.push(childDataNode);\n\t\twalk(childPsNode, childDataNode);\n\t}\n}\n\n/**\n * Created by rockyl on 2019-08-10.\n */\n\nasync function walkNode(node, callback, includeSelf = false) {\n\tif (includeSelf) {\n\t\tawait callback(node, null);\n\t}\n\tif (node.children && node.children.length > 0) {\n\t\tfor (let childNode of node.children) {\n\t\t\tawait callback(childNode, node);\n\t\t\tconst result = await walkNode(childNode, callback);\n\t\t\tif (result === true) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n\nasync function walkObject(obj, callback) {\n\tif(typeof obj === \"object\"){\n\t\tfor (let key of Object.keys(obj)) {\n\t\t\tconst value = obj[key];\n\t\t\tawait callback(key, value, obj);\n\t\t\tconst result = await walkObject(value, callback);\n\t\t\tif (result === true) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Created by rockyl on 2019-08-10.\n *\n * 导出exml\n */\n\nconst elementTpls = {\n\t'e:Group': [],\n\t'e:Image': {_attr: {source: '{res}'}},\n\t'e:Button': [\n\t\t{_attr: {label: '{1}',}},\n\t\t{\n\t\t\t'e:skinName': [\n\t\t\t\t{\n\t\t\t\t\t'e:Skin': [\n\t\t\t\t\t\t{_attr: {states: 'up,down,disabled'}},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'e:Image': {_attr: {width: '100%', height: '100%', source: '{res}'}},\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'e:Label': {_attr: {id: 'labelDisplay', horizontalCenter: '0', verticalCenter: '0'}},\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t]\n\t\t}\n\t]\n};\n\nasync function execute(psdFile, options) {\n\tconst {skinFilePath, skinClassName, resPath, resGroupName} = options;\n\n\tconst tree = await getTree(psdFile);\n\n\tconst exmlRoot = [\n\t\t{\n\t\t\t_attr: {\n\t\t\t\tclass: skinClassName,\n\t\t\t\twidth: tree.width,\n\t\t\t\theight: tree.height,\n\t\t\t\t'xmlns:e': \"http://ns.egret.com/eui\",\n\t\t\t\t'xmlns:w': \"http://ns.egret.com/wing\",\n\t\t\t},\n\t\t},\n\t];\n\tconst exmlData = {\n\t\t'e:Skin': exmlRoot\n\t};\n\n\tawait walkNode(tree, async function (node, parent) {\n\t\tconst {x, y, width, height, alpha, visible} = node;\n\t\tlet attributes = {width, height, alpha, visible};\n\t\tif (x !== 0) {\n\t\t\tattributes.x = x;\n\t\t}\n\t\tif (y !== 0) {\n\t\t\tattributes.y = y;\n\t\t}\n\t\tlet element;\n\t\tlet tagName;\n\t\tlet imageResName;\n\t\tlet params;\n\n\t\tlet hasChild = node.hasOwnProperty('children');\n\t\tif (hasChild) {\n\t\t\ttagName = 'e:Group';\n\t\t} else {\n\t\t\tconst nameParams = node.name.split('|');\n\t\t\tconst nodeName = nameParams[0];\n\n\t\t\tattributes.name = nodeName;\n\t\t\timageResName = resGroupName + '_' + nodeName;\n\t\t\tconst imageFilePath = path.join(resPath, resGroupName + '_p', nodeName + '.png');\n\t\t\tawait fs.ensureDir(path.dirname(imageFilePath));\n\t\t\tawait node.origin.saveAsPng(imageFilePath);\n\n\t\t\tif (nameParams.length === 1) {\n\t\t\t\ttagName = 'e:Image';\n\t\t\t} else {\n\t\t\t\tparams = nameParams[1].split(',');\n\t\t\t\ttagName = 'e:' + params[0];\n\t\t\t}\n\n\t\t\t//element[tagName] = {_attr: attributes};\n\t\t}\n\n\t\tlet elementTpl = elementTpls[tagName];\n\t\tlet elementContent;\n\t\tif (elementTpl) {\n\t\t\telementContent = JSON.parse(JSON.stringify(elementTpl));\n\t\t} else {\n\t\t\telementContent = {};\n\t\t}\n\t\telement = {\n\t\t\t[tagName]: elementContent,\n\t\t};\n\n\t\tlet attr;\n\t\tif (Array.isArray(elementContent)) {\n\t\t\tattr = elementContent.find(item => item._attr);\n\t\t\tif (!attr) {\n\t\t\t\tattr = {_attr: {}};\n\t\t\t\telementContent.unshift(attr);\n\t\t\t}\n\t\t} else {\n\t\t\tattr = elementContent;\n\t\t}\n\n\t\tObject.assign(attr._attr, attributes);\n\n\t\tif (imageResName) {\n\t\t\tawait walkObject(element, function (key, value, obj) {\n\t\t\t\tif (value === '{res}') {\n\t\t\t\t\tobj[key] = imageResName;\n\t\t\t\t} else if (typeof value === 'string') {\n\t\t\t\t\tconst result = value.match(/{(\\d+)}/g);\n\t\t\t\t\tif(result){\n\t\t\t\t\t\tfor(let item of result){\n\t\t\t\t\t\t\tconst pi = parseInt(item.match(/{(\\d+)}/)[1]);\n\t\t\t\t\t\t\tvalue = value.replace(item, params[pi]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tobj[key] = value;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\n\t\tif (hasChild) {\n\t\t\tnode.exmlNode = elementContent;\n\t\t}\n\n\t\tconst exmlNode = parent.exmlNode || exmlRoot;\n\t\texmlNode.push(element);\n\t});\n\n\tlet exmlStr = xml(exmlData, {declaration: true, indent: true});\n\tawait fs.ensureDir(path.dirname(skinFilePath));\n\tawait fs.writeFile(skinFilePath, exmlStr);\n\n}\n\n/**\n * Created by rockyl on 2019-09-26.\n *\n * 导出zeroing的视图\n */\n\nasync function execute$1(psdFile, options) {\n\tconst {\n\t\timagesPath,\n\t} = options;\n\n\tconst tree = await getTree(psdFile);\n\n\tlet viewRoot = {\n\t\tname: path.basename(psdFile, '.psd'),\n\t\ttype: 'node',\n\t};\n\n\tconst assets = [];\n\n\tawait walkNode(tree, async function (node, parent) {\n\t\tconst {x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;\n\t\tlet properties = {\n\t\t\twidth, height, alpha, visible,\n\t\t};\n\t\tlet viewNode = {\n\t\t\tname: node.name,\n\t\t\tproperties,\n\t\t};\n\t\tif (x !== 0) {\n\t\t\tproperties.x = x;\n\t\t}\n\t\tif (y !== 0) {\n\t\t\tproperties.y = y;\n\t\t}\n\n\t\tif(typeTool){\n\t\t\tlet fontInfo= typeTool();\n\t\t\tconst fonts = fontInfo.fonts();\n\t\t\tconst styles = fontInfo.styles();\n\t\t\tconst {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;\n\n\t\t\tproperties.text = fontInfo.textValue;\n\t\t\tproperties.textflow = {\n\t\t\t\tfonts, styles, RunLengthArray,\n\t\t\t};\n\t\t\tviewNode.type = 'label';\n\t\t}else if(solidColor){\n\t\t\tconst {r, g, b} = solidColor();\n\t\t\tlet color = Color({r, g, b});\n\n\t\t\tviewNode.type = 'rect';\n\t\t\tproperties.fillColor = '#' + color.rgbNumber().toString(16);\n\t\t}else{\n\t\t\tif(node.hasOwnProperty('children')){\n\t\t\t\tviewNode.type = 'node';\n\t\t\t}else{\n\t\t\t\tviewNode.type = 'image';\n\n\t\t\t\tconst uuid = generateUUID();\n\t\t\t\tconst fileName = Date.now().valueOf();\n\t\t\t\tconst ext = '.png';\n\n\t\t\t\tproperties.source = 'asset|' + uuid;\n\n\t\t\t\tconst imageFilePath = path.join(imagesPath, fileName + ext);\n\t\t\t\tawait fs.ensureDir(path.dirname(imageFilePath));\n\t\t\t\tawait node.origin.saveAsPng(imageFilePath);\n\n\t\t\t\tassets.push({\n\t\t\t\t\tname: fileName,\n\t\t\t\t\text,\n\t\t\t\t\tuuid,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tlet viewParent = parent.view || viewRoot;\n\t\tif (!viewParent.hasOwnProperty('children')) {\n\t\t\tviewParent.children = [];\n\t\t}\n\t\tviewParent.children.push(viewNode);\n\n\t\tnode.view = viewNode;\n\t});\n\n\treturn {\n\t\tview: viewRoot,\n\t\tassets,\n\t}\n}\n\nexport { getTree, execute as toEgret, execute$1 as toZeroing };\n//# sourceMappingURL=index.js.map\n","/**\n * Created by rockyl on 2019-08-10.\n */\n\nimport {toZeroing} from \"../../dist/index\";\n\n(async function generate() {\n\tconst imagesPath = 'zeroing-demo/images_' + Date.now();\n\n\tconst {view, assets} = await toZeroing('psd/test.psd', {\n\t\timagesPath,\n\t});\n\n\tconsole.log(assets);\n\tconsole.log(view);\n})();\n"],"names":["toZeroing"],"mappings":";;;;;;;;;;AAMA;;;;AAIA,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;;AAE3B,eAAe,OAAO,CAAC,WAAW,EAAE;CACnC,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;CACxC,MAAM,IAAI,GAAG,EAAE,CAAC;CAChB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAEvB,OAAO,IAAI,CAAC;CACZ;;AAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAC/B,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;CAC9D,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC;CAC3E,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;CACvB,MAAM,CAAC,GAAG,GAAG,GAAG,IAAI,CAAC;;CAErB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE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;EACzC,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE;GAC3C,UAAU,CAAC,QAAQ,GAAG,EAAE,CAAC;GACzB;EACD,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;;EAEnC,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC;EACrB,CAAC,CAAC;;CAEH,OAAO;EACN,IAAI,EAAE,QAAQ;EACd,MAAM;EACN;CACD;;AChTD;;;AAGA,AAEA;AACA,CAAC,eAAe,QAAQ,GAAG;CAC1B,MAAM,UAAU,GAAG,sBAAsB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;;CAEvD,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,MAAMA,SAAS,CAAC,cAAc,EAAE;EACtD,UAAU;EACV,CAAC,CAAC;;CAEH,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;CACpB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;CAClB,GAAG,CAAC"}
\ No newline at end of file
{"version":3,"file":"generator.cjs.js","sources":["../../src/psd-tree.js","../../src/utils.js","../../src/zeroing.js","generator.js"],"sourcesContent":["/**\n * Created by rockyl on 2019-08-09.\n */\n\nconst PSD = require('psd');\n\nexport async function getTree(psdFilePath) {\n\tconst psd = await PSD.open(psdFilePath);\n\tconst root = {};\n\twalk(psd.tree(), root);\n\n\treturn root;\n}\n\nfunction walk(psNode, dataNode) {\n\tconst {left: pLeft = 0, top: pTop = 0,} = psNode.parent || {};\n\tconst {left, top, width, height, name, layer: {opacity, visible}} = psNode;\n\tconst x = left - pLeft;\n\tconst y = top - pTop;\n\n\tObject.assign(dataNode, {x, y, width, height, alpha: opacity / 255, visible, name, origin: psNode, label: `${name} > [${x}, ${y}, ${width}, ${height}]`});\n\tif (psNode.children() && psNode.children().length > 0){\n\t\tdataNode.children = [];\n\t}\n\n\tlet children = psNode.children();\n\tfor (let i = children.length - 1; i >= 0; i--) {\n\t\tconst childPsNode = children[i];\n\n\t\tconst childDataNode = {};\n\t\tdataNode.children.push(childDataNode);\n\t\twalk(childPsNode, childDataNode)\n\t}\n}\n","/**\n * Created by rockyl on 2019-08-10.\n */\n\nexport async function walkNode(node, callback, includeSelf = false) {\n\tif (includeSelf) {\n\t\tawait callback(node, null);\n\t}\n\tif (node.children && node.children.length > 0) {\n\t\tfor (let childNode of node.children) {\n\t\t\tawait callback(childNode, node);\n\t\t\tconst result = await walkNode(childNode, callback);\n\t\t\tif (result === true) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n\nexport async function walkObject(obj, callback) {\n\tif(typeof obj === \"object\"){\n\t\tfor (let key of Object.keys(obj)) {\n\t\t\tconst value = obj[key];\n\t\t\tawait callback(key, value, obj);\n\t\t\tconst result = await walkObject(value, callback);\n\t\t\tif (result === true) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n}\n","/**\n * Created by rockyl on 2019-09-26.\n *\n * 导出zeroing的视图\n */\n\nimport {getTree} from \"./psd-tree\";\nimport {walkNode} from \"./utils\";\nimport path from 'path'\nimport Color from 'color'\nimport generateUUID from 'uuid/v4'\nimport fs from \"fs-extra\";\nimport hash from 'object-hash';\n\nexport async function execute(psdFile, options) {\n\tconst {\n\t\timagesPath,\n\t} = options;\n\n\tconst tree = await getTree(psdFile);\n\n\tlet viewRoot = {\n\t\tname: path.basename(psdFile, '.psd'),\n\t\ttype: 'node',\n\t};\n\n\tconst assets = [];\n\n\tawait walkNode(tree, async function (node, parent) {\n\t\tconst {name, x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;\n\t\tlet properties = {\n\t\t\twidth, height, alpha, visible,\n\t\t};\n\t\tlet viewNode = {\n\t\t\tname,\n\t\t\tproperties,\n\t\t\tuuid: generateUUID(),\n\t\t};\n\t\tif (x !== 0) {\n\t\t\tproperties.x = x;\n\t\t}\n\t\tif (y !== 0) {\n\t\t\tproperties.y = y;\n\t\t}\n\n\t\tif (typeTool) {\n\t\t\tlet fontInfo = typeTool();\n\t\t\tconst fonts = fontInfo.fonts();\n\t\t\tconst styles = fontInfo.styles();\n\t\t\tconst {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;\n\n\t\t\tproperties.text = fontInfo.textValue;\n\t\t\tproperties.textflow = {\n\t\t\t\tfonts, styles, RunLengthArray,\n\t\t\t};\n\t\t\tviewNode.type = 'label';\n\t\t} else if (solidColor) {\n\t\t\tconst {r, g, b} = solidColor();\n\t\t\tlet color = Color({r, g, b});\n\n\t\t\tviewNode.type = 'rect';\n\t\t\tproperties.fillColor = '#' + color.rgbNumber().toString(16);\n\t\t} else {\n\t\t\tif (node.hasOwnProperty('children')) {\n\t\t\t\tviewNode.type = 'node';\n\t\t\t} else {\n\t\t\t\tviewNode.type = 'image';\n\n\t\t\t\tconst uuid = generateUUID();\n\t\t\t\tconst ext = '.png';\n\n\t\t\t\tproperties.source = 'asset|' + uuid;\n\n\t\t\t\tconst imageFilePath = path.join(imagesPath, uuid + ext);\n\t\t\t\tawait fs.ensureDir(path.dirname(imageFilePath));\n\t\t\t\tlet png = node.origin.toPng();\n\t\t\t\tlet buffer = await savePng(png, imageFilePath);\n\t\t\t\t//await node.origin.saveAsPng(imageFilePath);\n\n\t\t\t\tconst hashFileName = hash(buffer);\n\t\t\t\tconst hashFilePath = path.join(imagesPath, hashFileName + ext);\n\t\t\t\tawait fs.rename(imageFilePath, hashFilePath);\n\n\t\t\t\tassets.push({\n\t\t\t\t\tname,\n\t\t\t\t\text,\n\t\t\t\t\tuuid,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tlet viewParent = parent.view || viewRoot;\n\t\tif (!viewParent.hasOwnProperty('children')) {\n\t\t\tviewParent.children = [];\n\t\t}\n\t\tviewParent.children.push(viewNode);\n\n\t\tnode.view = viewNode;\n\t});\n\n\treturn {\n\t\tview: viewRoot,\n\t\tassets,\n\t}\n}\n\nfunction savePng(png, output) {\n\treturn new Promise((resolve, reject) => {\n\t\tlet buffer, buffers = [];\n\t\tpng.pack()\n\t\t\t.on('error', reject)\n\t\t\t.on('data', (data) => buffers.push(data))\n\t\t\t.on('end', () => {\n\t\t\t\tbuffer = Buffer.concat(buffers)\n\t\t\t})\n\t\t\t.pipe(fs.createWriteStream(output))\n\t\t\t.on('finish', () => {\n\t\t\t\tresolve(buffer);\n\t\t\t});\n\t});\n}\n","/**\n * Created by rockyl on 2019-08-10.\n */\n\nimport {toZeroing} from \"../../src/index\";\n\n(async function generate() {\n\tconst imagesPath = 'zeroing-demo/images_' + Date.now();\n\n\tconst {view, assets} = await toZeroing('psd/test.psd', {\n\t\timagesPath,\n\t});\n\n\tconsole.log(assets);\n\tconsole.log(view);\n})();\n"],"names":["toZeroing"],"mappings":";;;;;;;;;;;AAAA;;;;AAIA,MAAM,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;;AAE3B,AAAO,eAAe,OAAO,CAAC,WAAW,EAAE;CAC1C,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;CACxC,MAAM,IAAI,GAAG,EAAE,CAAC;CAChB,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;;CAEvB,OAAO,IAAI,CAAC;CACZ;;AAED,SAAS,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;CAC/B,MAAM,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;CAC9D,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC;CAC3E,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;CACvB,MAAM,CAAC,GAAG,GAAG,GAAG,IAAI,CAAC;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AC,QAAQ,EAAE,MAAM;IACnB,OAAO,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC,CAAC;EACJ,CAAC,CAAC;CACH;;ACxHD;;;AAGA,AAEA;AACA,CAAC,eAAe,QAAQ,GAAG;CAC1B,MAAM,UAAU,GAAG,sBAAsB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;;CAEvD,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,MAAMA,OAAS,CAAC,cAAc,EAAE;EACtD,UAAU;EACV,CAAC,CAAC;;CAEH,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;CACpB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;CAClB,GAAG,CAAC"}
\ No newline at end of file
......@@ -10,6 +10,7 @@ import path from 'path'
import Color from 'color'
import generateUUID from 'uuid/v4'
import fs from "fs-extra";
import hash from 'object-hash';
export async function execute(psdFile, options) {
const {
......@@ -21,18 +22,20 @@ export async function execute(psdFile, options) {
let viewRoot = {
name: path.basename(psdFile, '.psd'),
type: 'node',
uuid: generateUUID(),
};
const assets = [];
await walkNode(tree, async function (node, parent) {
const {x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;
const {name, x, y, width, height, alpha, visible, origin: {layer: {typeTool, solidColor}}} = node;
let properties = {
width, height, alpha, visible,
};
let viewNode = {
name: node.name,
name,
properties,
uuid: generateUUID(),
};
if (x !== 0) {
properties.x = x;
......@@ -41,8 +44,8 @@ export async function execute(psdFile, options) {
properties.y = y;
}
if(typeTool){
let fontInfo= typeTool();
if (typeTool) {
let fontInfo = typeTool();
const fonts = fontInfo.fonts();
const styles = fontInfo.styles();
const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
......@@ -52,16 +55,16 @@ export async function execute(psdFile, options) {
fonts, styles, RunLengthArray,
};
viewNode.type = 'label';
}else if(solidColor){
} else if (solidColor) {
const {r, g, b} = solidColor();
let color = Color({r, g, b});
viewNode.type = 'rect';
properties.fillColor = '#' + color.rgbNumber().toString(16);
}else{
if(node.hasOwnProperty('children')){
} else {
if (node.hasOwnProperty('children')) {
viewNode.type = 'node';
}else{
} else {
viewNode.type = 'image';
const uuid = generateUUID();
......@@ -71,10 +74,16 @@ export async function execute(psdFile, options) {
const imageFilePath = path.join(imagesPath, uuid + ext);
await fs.ensureDir(path.dirname(imageFilePath));
await node.origin.saveAsPng(imageFilePath);
let png = node.origin.toPng();
let buffer = await savePng(png, imageFilePath);
//await node.origin.saveAsPng(imageFilePath);
const hashFileName = hash(buffer);
const hashFilePath = path.join(imagesPath, hashFileName + ext);
await fs.rename(imageFilePath, hashFilePath);
assets.push({
name: uuid,
name,
ext,
uuid,
});
......@@ -95,3 +104,19 @@ export async function execute(psdFile, options) {
assets,
}
}
function savePng(png, output) {
return new Promise((resolve, reject) => {
let buffer, buffers = [];
png.pack()
.on('error', reject)
.on('data', (data) => buffers.push(data))
.on('end', () => {
buffer = Buffer.concat(buffers)
})
.pipe(fs.createWriteStream(output))
.on('finish', () => {
resolve(buffer);
});
});
}
......@@ -95,6 +95,11 @@ mkdirp@~0.3.5:
resolved "https://registry.npm.taobao.org/mkdirp/download/mkdirp-0.3.5.tgz#de3e5f8961c88c787ee1368df849ac4413eca8d7"
integrity sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc=
object-hash@^1.3.1:
version "1.3.1"
resolved "https://registry.npm.taobao.org/object-hash/download/object-hash-1.3.1.tgz#fde452098a951cb145f039bb7d455449ddc126df"
integrity sha1-/eRSCYqVHLFF8Dm7fUVUSd3BJt8=
"parse-engine-data@~ 0.1":
version "0.1.2"
resolved "https://registry.npm.taobao.org/parse-engine-data/download/parse-engine-data-0.1.2.tgz#5161f6133c9888f52155ecced42716575dfea052"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment