Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
sample
.eslintrc.json
96 changes: 95 additions & 1 deletion bin/psd.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ var fileType = require('file-type');
var readChunk = require('read-chunk');
var chalk = require('chalk');
var fs = require('fs');
var Path = require('path');
var filenamify = require('filenamify');
var mkdirp = require('mkdirp');

var filesProcessed = [];
var outputDir = null;

// setup Commander program
program
.version(require('../package.json').version)
.arguments('<file...>')
.option('-c, --convert', 'Convert to PNG file named <FILENAME>.png')
.option('-l, --layers', 'Convert layers to PNG files named <LAYER-NAME>.png')
.option('-t, --text', 'Extract text content to <FILENAME>.txt')
.option('-o, --open', 'Preview file after conversion (triggers -c option)')
.option('-d, --dir <DIR>', 'Write output file(s) to <DIR>')
.action(processFiles)
.parse(process.argv);

// save PNG
function convertFile(filepath, psdPromise, cb) {
var filePng = filepath.replace(/\.psd$/, '.png');

if (outputDir) {
filePng = Path.join(outputDir, Path.basename(filePng));
}

psdPromise.then(function(psd) {
return psd.image.saveAsPng(filePng);
}).then(function(err) {
Expand All @@ -39,11 +49,39 @@ function convertFile(filepath, psdPromise, cb) {
});
}

// save layers to PNG
function convertLayers(filepath, psdPromise, cb) {
var fileDir = Path.dirname(filepath);

if (outputDir) {
fileDir = outputDir;
}

try {
psdPromise.then(function(psd) {
psd.tree().descendants().forEach(function(node) {
saveLayer(node, fileDir, cb);
});
});
} catch (err) {
console.log(
chalk.red.bold('Error while extracting layers from %s'), filepath);
return cb(err);
}

filesProcessed.push(filepath);
cb(null, filepath);
}

// extract text from PSD file
function extractTextFromFile(filepath, psdPromise, cb) {
var fileText = filepath.replace(/\.psd$/, '.txt');
var fileString = '';

if (outputDir) {
fileText = Path.join(outputDir, Path.basename(fileText));
}

psdPromise.then(function(psd) {

psd.tree().export().children.forEach(function(child) {
Expand Down Expand Up @@ -101,14 +139,51 @@ function processFiles(files, env) {
convertFile(filepath, psdPromise, cb);
});
}
if (program.layers) {
asyncTasks.push(function(cb) {
convertLayers(filepath, psdPromise, cb);
});
}
// extract text data
if (program.text) {
asyncTasks.push(function(cb) {
extractTextFromFile(filepath, psdPromise, cb);
});
}
// set output directory
if (program.dir) {
outputDir = Path.resolve(program.dir);
}

if (program.dir) {
// check that directory exists and is a directory
try {
if (fs.statSync(outputDir).isDirectory()) {
async.series(asyncTasks, cb);
} else {
console.log(chalk.red.bold('%s is not a directory'), outputDir);
return cb();
}
} catch (err) {
if (err.code === 'ENOENT') {
// make directory
mkdirp(outputDir, function(error) {
if (error) {
console.log(chalk.red.bold('Error while creating %s'), outputDir);
return cb();
}

async.series(asyncTasks, cb);
console.log(chalk.gray('Created directory %s'), outputDir);
async.series(asyncTasks, cb);
});
} else {
console.log(chalk.red.bold('Error accessing %s'), outputDir);
return cb();
}
}
} else {
async.series(asyncTasks, cb);
}

}, processDone);
}
Expand Down Expand Up @@ -180,3 +255,22 @@ function PSDLayer(path, element) {
}
}
}

function saveLayer(node, fileDir, cb) {

if (node && node.hasChildren()) {
node.children().forEach(function(child) {
saveLayer(child, fileDir, cb);
});
} else if (node) {
var filepath = Path.join(
fileDir, filenamify(node.layer.name, {replacement: '-'}) + '.png');
node.layer.image.saveAsPng(filepath).then(function(err) {
if (err) {
console.log(chalk.red.bold("Error while saving %s"), filepath);
}

console.log(chalk.gray("PNG saved to %s"), filepath);
});
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"commander": "2.9.0",
"file-type": "3.3.0",
"psd": "3.1.0",
"read-chunk": "1.0.1"
"read-chunk": "1.0.1",
"filenamify": "2.1.0",
"mkdirp": "0.5.1"
},
"devDependencies": {},
"preferGlobal": true,
Expand Down