Commit 9a995b61 authored by rockyl's avatar rockyl

资源去重

parent 6fecdea7
...@@ -233,6 +233,7 @@ async function execute$1(psdFile, options) { ...@@ -233,6 +233,7 @@ async function execute$1(psdFile, options) {
}; };
const assets = []; const assets = [];
const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -296,7 +297,7 @@ async function execute$1(psdFile, options) { ...@@ -296,7 +297,7 @@ async function execute$1(psdFile, options) {
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); let uuid = generateUUID();
const ext = '.png'; const ext = '.png';
const imageFilePath = path.join(imagesPath, uuid + ext); const imageFilePath = path.join(imagesPath, uuid + ext);
...@@ -307,19 +308,24 @@ async function execute$1(psdFile, options) { ...@@ -307,19 +308,24 @@ async function execute$1(psdFile, options) {
//await node.origin.saveAsPng(imageFilePath); //await node.origin.saveAsPng(imageFilePath);
if (buffer) { if (buffer) {
properties.source = 'asset://' + uuid; const fileNameHash = hash(buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) {
const hashFileName = hash(buffer); uuid = imageHashMap[fileNameHash];
const hashFilePath = path.join(imagesPath, hashFileName + ext); } else {
await fs.rename(imageFilePath, hashFilePath); imageHashMap[fileNameHash] = uuid;
assets.push({ assets.push({
name, name,
ext, ext,
uuid, uuid,
hash: hashFileName, hash: fileNameHash,
}); });
} }
const hashFilePath = path.join(imagesPath, fileNameHash + ext);
await fs.rename(imageFilePath, hashFilePath);
properties.source = 'asset://' + uuid;
}
} }
} }
......
This diff is collapsed.
...@@ -239,6 +239,7 @@ async function execute$1(psdFile, options) { ...@@ -239,6 +239,7 @@ async function execute$1(psdFile, options) {
}; };
const assets = []; const assets = [];
const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -302,7 +303,7 @@ async function execute$1(psdFile, options) { ...@@ -302,7 +303,7 @@ async function execute$1(psdFile, options) {
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); let uuid = generateUUID();
const ext = '.png'; const ext = '.png';
const imageFilePath = path.join(imagesPath, uuid + ext); const imageFilePath = path.join(imagesPath, uuid + ext);
...@@ -313,19 +314,24 @@ async function execute$1(psdFile, options) { ...@@ -313,19 +314,24 @@ async function execute$1(psdFile, options) {
//await node.origin.saveAsPng(imageFilePath); //await node.origin.saveAsPng(imageFilePath);
if (buffer) { if (buffer) {
properties.source = 'asset://' + uuid; const fileNameHash = hash(buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) {
const hashFileName = hash(buffer); uuid = imageHashMap[fileNameHash];
const hashFilePath = path.join(imagesPath, hashFileName + ext); } else {
await fs.rename(imageFilePath, hashFilePath); imageHashMap[fileNameHash] = uuid;
assets.push({ assets.push({
name, name,
ext, ext,
uuid, uuid,
hash: hashFileName, hash: fileNameHash,
}); });
} }
const hashFilePath = path.join(imagesPath, fileNameHash + ext);
await fs.rename(imageFilePath, hashFilePath);
properties.source = 'asset://' + uuid;
}
} }
} }
......
This diff is collapsed.
...@@ -239,6 +239,7 @@ ...@@ -239,6 +239,7 @@
}; };
const assets = []; const assets = [];
const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -302,7 +303,7 @@ ...@@ -302,7 +303,7 @@
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); let uuid = generateUUID();
const ext = '.png'; const ext = '.png';
const imageFilePath = path.join(imagesPath, uuid + ext); const imageFilePath = path.join(imagesPath, uuid + ext);
...@@ -313,19 +314,24 @@ ...@@ -313,19 +314,24 @@
//await node.origin.saveAsPng(imageFilePath); //await node.origin.saveAsPng(imageFilePath);
if (buffer) { if (buffer) {
properties.source = 'asset://' + uuid; const fileNameHash = hash(buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) {
const hashFileName = hash(buffer); uuid = imageHashMap[fileNameHash];
const hashFilePath = path.join(imagesPath, hashFileName + ext); } else {
await fs.rename(imageFilePath, hashFilePath); imageHashMap[fileNameHash] = uuid;
assets.push({ assets.push({
name, name,
ext, ext,
uuid, uuid,
hash: hashFileName, hash: fileNameHash,
}); });
} }
const hashFilePath = path.join(imagesPath, fileNameHash + ext);
await fs.rename(imageFilePath, hashFilePath);
properties.source = 'asset://' + uuid;
}
} }
} }
......
This diff is collapsed.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"egret": "rollup -i egret-demo/generator.js -o egret-demo/generator.cjs.js -f cjs -m&&node egret-demo/generator.cjs.js", "egret": "rollup -i egret-demo/generator.js -o egret-demo/generator.cjs.js -f cjs -m && node egret-demo/generator.cjs.js",
"zeroing": "rollup -i zeroing-demo/generator.js -o zeroing-demo/generator.cjs.js -f cjs -m&&node zeroing-demo/generator.cjs.js" "zeroing": "rollup -i zeroing-demo/generator.js -o zeroing-demo/generator.cjs.js -f cjs -m && node zeroing-demo/generator.cjs.js"
} }
} }
...@@ -211,7 +211,7 @@ function savePng(png, output) { ...@@ -211,7 +211,7 @@ function savePng(png, output) {
(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('psd/test2.psd', { const {view, assets} = await execute('psd/test3.psd', {
imagesPath, imagesPath,
}); });
......
This diff is collapsed.
...@@ -7,7 +7,7 @@ import {toZeroing} from "../../src/index"; ...@@ -7,7 +7,7 @@ import {toZeroing} from "../../src/index";
(async function generate() { (async function generate() {
const imagesPath = 'zeroing-demo/images_' + Date.now(); const imagesPath = 'zeroing-demo/images_' + Date.now();
const {view, assets} = await toZeroing('psd/test2.psd', { const {view, assets} = await toZeroing('psd/test3.psd', {
imagesPath, imagesPath,
}); });
......
...@@ -26,6 +26,7 @@ export async function execute(psdFile, options) { ...@@ -26,6 +26,7 @@ export async function execute(psdFile, options) {
}; };
const assets = []; const assets = [];
const imageHashMap = {};
await walkNode(tree, async function (node, parent) { await walkNode(tree, async function (node, parent) {
const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node; const {name, x, y, width, height, alpha, visible, origin: {layer, layer: {typeTool, solidColor}}} = node;
...@@ -89,7 +90,7 @@ export async function execute(psdFile, options) { ...@@ -89,7 +90,7 @@ export async function execute(psdFile, options) {
} else { } else {
viewNode.type = 'image'; viewNode.type = 'image';
const uuid = generateUUID(); let uuid = generateUUID();
const ext = '.png'; const ext = '.png';
const imageFilePath = path.join(imagesPath, uuid + ext); const imageFilePath = path.join(imagesPath, uuid + ext);
...@@ -100,19 +101,24 @@ export async function execute(psdFile, options) { ...@@ -100,19 +101,24 @@ export async function execute(psdFile, options) {
//await node.origin.saveAsPng(imageFilePath); //await node.origin.saveAsPng(imageFilePath);
if (buffer) { if (buffer) {
properties.source = 'asset://' + uuid; const fileNameHash = hash(buffer);
if (imageHashMap.hasOwnProperty(fileNameHash)) {
const hashFileName = hash(buffer); uuid = imageHashMap[fileNameHash];
const hashFilePath = path.join(imagesPath, hashFileName + ext); } else {
await fs.rename(imageFilePath, hashFilePath); imageHashMap[fileNameHash] = uuid;
assets.push({ assets.push({
name, name,
ext, ext,
uuid, uuid,
hash: hashFileName, hash: fileNameHash,
}); });
} }
const hashFilePath = path.join(imagesPath, fileNameHash + ext);
await fs.rename(imageFilePath, hashFilePath);
properties.source = 'asset://' + uuid;
}
} }
} }
......
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