Commit f112f438 authored by rockyl's avatar rockyl

文件名使用hash字符串

节点增加uuid字段
parent a79d745a
{ {
"name": "psd-parse", "name": "psd-parse",
"version": "1.0.0", "version": "1.0.1",
"main": "dist/index.js", "main": "dist/index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"color": "^3.1.2", "color": "^3.1.2",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"object-hash": "^1.3.1",
"psd": "^3.2.0", "psd": "^3.2.0",
"uuid": "^3.3.3", "uuid": "^3.3.3",
"xml": "^1.0.1" "xml": "^1.0.1"
......
...@@ -7,6 +7,7 @@ var path = _interopDefault(require('path')); ...@@ -7,6 +7,7 @@ var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs-extra')); var fs = _interopDefault(require('fs-extra'));
var Color = _interopDefault(require('color')); var Color = _interopDefault(require('color'));
var generateUUID = _interopDefault(require('uuid/v4')); var generateUUID = _interopDefault(require('uuid/v4'));
var hash = _interopDefault(require('object-hash'));
/** /**
* Created by rockyl on 2019-08-09. * Created by rockyl on 2019-08-09.
...@@ -68,7 +69,7 @@ async function walkNode(node, callback, includeSelf = false) { ...@@ -68,7 +69,7 @@ async function walkNode(node, callback, includeSelf = false) {
* 导出zeroing的视图 * 导出zeroing的视图
*/ */
async function execute$1(psdFile, options) { async function execute(psdFile, options) {
const { const {
imagesPath, imagesPath,
} = options; } = options;
...@@ -83,13 +84,14 @@ async function execute$1(psdFile, options) { ...@@ -83,13 +84,14 @@ async function execute$1(psdFile, options) {
const assets = []; const assets = [];
await walkNode(tree, async function (node, parent) { 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 = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
let viewNode = { let viewNode = {
name: node.name, name,
properties, properties,
uuid: generateUUID(),
}; };
if (x !== 0) { if (x !== 0) {
properties.x = x; properties.x = x;
...@@ -98,8 +100,8 @@ async function execute$1(psdFile, options) { ...@@ -98,8 +100,8 @@ async function execute$1(psdFile, options) {
properties.y = y; properties.y = y;
} }
if(typeTool){ if (typeTool) {
let fontInfo= typeTool(); let fontInfo = typeTool();
const fonts = fontInfo.fonts(); const fonts = fontInfo.fonts();
const styles = fontInfo.styles(); const styles = fontInfo.styles();
const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun; const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
...@@ -109,30 +111,35 @@ async function execute$1(psdFile, options) { ...@@ -109,30 +111,35 @@ async function execute$1(psdFile, options) {
fonts, styles, RunLengthArray, fonts, styles, RunLengthArray,
}; };
viewNode.type = 'label'; viewNode.type = 'label';
}else if(solidColor){ } else if (solidColor) {
const {r, g, b} = solidColor(); const {r, g, b} = solidColor();
let color = Color({r, g, b}); let color = Color({r, g, b});
viewNode.type = 'rect'; viewNode.type = 'rect';
properties.fillColor = '#' + color.rgbNumber().toString(16); properties.fillColor = '#' + color.rgbNumber().toString(16);
}else{ } else {
if(node.hasOwnProperty('children')){ if (node.hasOwnProperty('children')) {
viewNode.type = 'node'; viewNode.type = 'node';
}else{ } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); const uuid = generateUUID();
const fileName = Date.now().valueOf();
const ext = '.png'; const ext = '.png';
properties.source = 'asset|' + uuid; 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 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({ assets.push({
name: fileName, name,
ext, ext,
uuid, uuid,
}); });
...@@ -154,6 +161,22 @@ async function execute$1(psdFile, options) { ...@@ -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. * Created by rockyl on 2019-08-10.
*/ */
...@@ -161,7 +184,7 @@ async function execute$1(psdFile, options) { ...@@ -161,7 +184,7 @@ async function execute$1(psdFile, options) {
(async function generate() { (async function generate() {
const imagesPath = 'zeroing-demo/images_' + Date.now(); 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, imagesPath,
}); });
......
This diff is collapsed.
...@@ -10,6 +10,7 @@ import path from 'path' ...@@ -10,6 +10,7 @@ import path from 'path'
import Color from 'color' import Color from 'color'
import generateUUID from 'uuid/v4' import generateUUID from 'uuid/v4'
import fs from "fs-extra"; import fs from "fs-extra";
import hash from 'object-hash';
export async function execute(psdFile, options) { export async function execute(psdFile, options) {
const { const {
...@@ -21,18 +22,20 @@ export async function execute(psdFile, options) { ...@@ -21,18 +22,20 @@ export async function execute(psdFile, options) {
let viewRoot = { let viewRoot = {
name: path.basename(psdFile, '.psd'), name: path.basename(psdFile, '.psd'),
type: 'node', type: 'node',
uuid: generateUUID(),
}; };
const assets = []; const assets = [];
await walkNode(tree, async function (node, parent) { 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 = { let properties = {
width, height, alpha, visible, width, height, alpha, visible,
}; };
let viewNode = { let viewNode = {
name: node.name, name,
properties, properties,
uuid: generateUUID(),
}; };
if (x !== 0) { if (x !== 0) {
properties.x = x; properties.x = x;
...@@ -41,8 +44,8 @@ export async function execute(psdFile, options) { ...@@ -41,8 +44,8 @@ export async function execute(psdFile, options) {
properties.y = y; properties.y = y;
} }
if(typeTool){ if (typeTool) {
let fontInfo= typeTool(); let fontInfo = typeTool();
const fonts = fontInfo.fonts(); const fonts = fontInfo.fonts();
const styles = fontInfo.styles(); const styles = fontInfo.styles();
const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun; const {RunLengthArray} = fontInfo.engineData.EngineDict.StyleRun;
...@@ -52,16 +55,16 @@ export async function execute(psdFile, options) { ...@@ -52,16 +55,16 @@ export async function execute(psdFile, options) {
fonts, styles, RunLengthArray, fonts, styles, RunLengthArray,
}; };
viewNode.type = 'label'; viewNode.type = 'label';
}else if(solidColor){ } else if (solidColor) {
const {r, g, b} = solidColor(); const {r, g, b} = solidColor();
let color = Color({r, g, b}); let color = Color({r, g, b});
viewNode.type = 'rect'; viewNode.type = 'rect';
properties.fillColor = '#' + color.rgbNumber().toString(16); properties.fillColor = '#' + color.rgbNumber().toString(16);
}else{ } else {
if(node.hasOwnProperty('children')){ if (node.hasOwnProperty('children')) {
viewNode.type = 'node'; viewNode.type = 'node';
}else{ } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); const uuid = generateUUID();
...@@ -71,10 +74,16 @@ export async function execute(psdFile, options) { ...@@ -71,10 +74,16 @@ export async function execute(psdFile, options) {
const imageFilePath = path.join(imagesPath, uuid + ext); const imageFilePath = path.join(imagesPath, uuid + ext);
await fs.ensureDir(path.dirname(imageFilePath)); 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({ assets.push({
name: uuid, name,
ext, ext,
uuid, uuid,
}); });
...@@ -95,3 +104,19 @@ export async function execute(psdFile, options) { ...@@ -95,3 +104,19 @@ export async function execute(psdFile, options) {
assets, 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: ...@@ -95,6 +95,11 @@ mkdirp@~0.3.5:
resolved "https://registry.npm.taobao.org/mkdirp/download/mkdirp-0.3.5.tgz#de3e5f8961c88c787ee1368df849ac4413eca8d7" resolved "https://registry.npm.taobao.org/mkdirp/download/mkdirp-0.3.5.tgz#de3e5f8961c88c787ee1368df849ac4413eca8d7"
integrity sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc= 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": "parse-engine-data@~ 0.1":
version "0.1.2" version "0.1.2"
resolved "https://registry.npm.taobao.org/parse-engine-data/download/parse-engine-data-0.1.2.tgz#5161f6133c9888f52155ecced42716575dfea052" 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