Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function build(filename, argv) {
filename = path.resolve('', filename || '');
if (!fs.existsSync(filename)) {
console.warn(
colors.blue('File "%s" not exists.'), filename
);
return;
}
// 处理默认值
argv = argv || {};
var output = argv.output || path.dirname(filename);
var images = argv.images || 'images';
return psd.open(filename).then(function (context) {
mkdirp.sync(path.join(output, images)); // 确保输出目录存在
var tree = context.tree();
var treeInfo = tree.export();
var h5 = {
name: path.basename(filename),
width: treeInfo.document.width,
height: treeInfo.document.height,
layers: []
};
// var promise = [];
var layers = h5.layers;
var md5dict = {};
var promises = [];
console.log("\nProcessing %s ...", filepath);
try {
var buffer = readChunk.sync(filepath, 0, 262);
var type = fileType(buffer).ext;
if (type != 'psd') {
console.log(chalk.red.bold("%s is not a PSD file, type detected : %s"), filepath, type);
return cb();
}
} catch (e) {
console.log(chalk.red.bold("%s could not be opened with PSD library"), filepath);
return cb();
}
var psdPromise = PSD.open(filepath);
var asyncTasks = [];
// convert file to PNG
if (program.convert || program.open) {
asyncTasks.push(function(cb) {
convertFile(filepath, psdPromise, cb);
});
}
// extract text data
if (program.text) {
asyncTasks.push(function(cb) {
extractTextFromFile(filepath, psdPromise, cb);
});
}
async.series(asyncTasks, cb);
colors.blue('PSD file "%s" not exists.'), filename
);
return;
}
template = argv.template || path.join(__dirname, '../tpl/page.html');
if (!fs.existsSync(template)) {
console.warn(
colors.blue('Template file "%s" not exists.'), template
);
return;
}
// 处理默认值
argv = argv || {};
var output = argv.output || path.dirname(filename);
var images = argv.images || 'images';
return psd.open(filename).then(function (context) {
mkdirp.sync(path.join(output, images)); // 确保输出目录存在
var tree = context.tree();
var treeInfo = tree.export();
var h5 = {
name: path.basename(filename),
width: treeInfo.document.width,
height: treeInfo.document.height,
layers: []
};
// var promise = [];
var layers = h5.layers;
var md5dict = {};
var promises = [];
var descendants = tree.descendants();
descendants.forEach(function (node, index) {
if (node.isGroup() || node.hidden()) {
let outImgDir = '';
let outJsonDir = '';
if (typeof options === 'string') {
outImgDir = options;
outJsonDir = options;
} else {
if (options.outImgDir) {
outImgDir = options.outImgDir;
}
if (options.outJsonDir) {
outJsonDir = options.outJsonDir;
}
}
// get root node.
const psdData = psd.fromFile(psdFilePath);
psdData.parse();
const rootNode = psdData.tree();
const queueNodes = [];
const queueNodesIndex = [];
const queueNodesName = [];
const queueNodesStructure = [];
queueNodes.push(rootNode._children);
queueNodesIndex.push(0);
queueNodesName.push(undefined);
const psdStructure = {
'group' : []
};
queueNodesStructure.push(psdStructure);
var LayersfromPathes = R.reduce(function(acc,path){
var psd = PSD.fromFile(path);
psd.parse();
var psdChildren = psd.tree().children()
return R.concat(acc, psdChildren.map(metaAdder(path)));
}, []);
async dropFile(event) {
event.preventDefault();
this.setState({ loading:true, psd:null });
let psd = await PSD.fromEvent(event);
this.setState({ loading:false, psd });
},