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,
});
......
This diff is collapsed.
......@@ -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