Commit 30e0cbff authored by rockyl's avatar rockyl

节点增加uuid字段

parent 4a712d00
......@@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs-extra';
import Color from 'color';
import generateUUID from 'uuid/v4';
import hash from 'object-hash';
/**
* Created by rockyl on 2019-08-09.
......@@ -228,18 +229,20 @@ async function execute$1(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;
......@@ -248,8 +251,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;
......@@ -259,16 +262,16 @@ 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();
......@@ -278,12 +281,19 @@ async function execute$1(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,
hash: hashFileName,
});
}
}
......@@ -303,5 +313,21 @@ 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);
});
});
}
export { getTree, execute as toEgret, execute$1 as toZeroing };
//# sourceMappingURL=index.es.js.map
This diff is collapsed.
......@@ -9,6 +9,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.
......@@ -234,18 +235,20 @@ async function execute$1(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;
......@@ -254,8 +257,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;
......@@ -265,16 +268,16 @@ 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();
......@@ -284,12 +287,19 @@ async function execute$1(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,
hash: hashFileName,
});
}
}
......@@ -309,6 +319,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);
});
});
}
exports.getTree = getTree;
exports.toEgret = execute;
exports.toZeroing = execute$1;
......
This diff is collapsed.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('xml'), require('path'), require('fs-extra'), require('color'), require('uuid/v4')) :
typeof define === 'function' && define.amd ? define(['exports', 'xml', 'path', 'fs-extra', 'color', 'uuid/v4'], factory) :
(global = global || self, factory(global['psd-parse'] = {}, global.xml, global.path, global.fs, global.Color, global.generateUUID));
}(this, function (exports, xml, path, fs, Color, generateUUID) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('xml'), require('path'), require('fs-extra'), require('color'), require('uuid/v4'), require('object-hash')) :
typeof define === 'function' && define.amd ? define(['exports', 'xml', 'path', 'fs-extra', 'color', 'uuid/v4', 'object-hash'], factory) :
(global = global || self, factory(global['psd-parse'] = {}, global.xml, global.path, global.fs, global.Color, global.generateUUID, global.hash));
}(this, function (exports, xml, path, fs, Color, generateUUID, hash) { 'use strict';
xml = xml && xml.hasOwnProperty('default') ? xml['default'] : xml;
path = path && path.hasOwnProperty('default') ? path['default'] : path;
fs = fs && fs.hasOwnProperty('default') ? fs['default'] : fs;
Color = Color && Color.hasOwnProperty('default') ? Color['default'] : Color;
generateUUID = generateUUID && generateUUID.hasOwnProperty('default') ? generateUUID['default'] : generateUUID;
hash = hash && hash.hasOwnProperty('default') ? hash['default'] : hash;
/**
* Created by rockyl on 2019-08-09.
......@@ -234,18 +235,20 @@
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;
......@@ -254,8 +257,8 @@
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;
......@@ -265,16 +268,16 @@
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();
......@@ -284,12 +287,19 @@
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,
hash: hashFileName,
});
}
}
......@@ -309,6 +319,22 @@
}
}
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);
});
});
}
exports.getTree = getTree;
exports.toEgret = execute;
exports.toZeroing = execute$1;
......
This diff is collapsed.
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