diff --git a/.gitignore b/.gitignore index cb4e535..ba3e916 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,7 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules +.idea +package-lock.json diff --git a/capture/bin/arm/scrot b/capture/bin/arm/scrot new file mode 100755 index 0000000..df4f6fe Binary files /dev/null and b/capture/bin/arm/scrot differ diff --git a/capture/darwin.js b/capture/darwin.js index 7b64886..aafe4de 100644 --- a/capture/darwin.js +++ b/capture/darwin.js @@ -3,15 +3,10 @@ module.exports = function(options, callback) { var fs = require('fs'); var childProcess = require('child_process'); - // due to bug in jpgjs processing OSX jpg images https://github.com/notmasteryet/jpgjs/issues/34 + // due to bug in jpgjs processing OSX jpg images https://github.com/notmasteryet/jpgjs/issues/34 // when requesting JPG capture as PNG, so JIMP can read it - var ext = extension(options.output); - if(ext === "jpeg" || ext === "jpg") { - options.intermediate = path.resolve(path.join(__dirname, uniqueId() + ".png")); // create an intermediate file that can be processed, then deleted - capture(options.intermediate, callbackReturn); - } - else - capture(options.output, callbackReturn); // when jpegjs bug fixed, only need this line + /* the previuos error seems already corrected */ + capture(options.output, callbackReturn); // when jpegjs bug fixed, only need this line function callbackReturn(error, success) { // called from capture @@ -19,26 +14,12 @@ module.exports = function(options, callback) { callback(error, options); } - function uniqueId() { - function s4() { - return Math.floor((1 + Math.random()) * 0x10000) - .toString(16) - .substring(1); - } - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + - s4() + '-' + s4() + s4() + s4(); - } - - function extension(file) { - return path.extname(file).toLowerCase().substring(1); - } - function capture(output, callback) { var cmd = "screencapture"; var args = [ // will create PNG by default "-t", - path.extname(output).toLowerCase().substring(1), + path.extname(file).toLowerCase().substring(1), "-x", output ]; diff --git a/capture/index.js b/capture/index.js new file mode 100644 index 0000000..eab42ef --- /dev/null +++ b/capture/index.js @@ -0,0 +1,6 @@ +'use strict' +exports = module.exports = { + darwin: require('./darwin'), + linux: require('./linux'), + win32: require('./win32') +} \ No newline at end of file diff --git a/capture/linux.js b/capture/linux.js index b4a9b67..8c40795 100755 --- a/capture/linux.js +++ b/capture/linux.js @@ -4,7 +4,7 @@ module.exports = function(options, callback) { var childProcess = require('child_process'); var path = require('path'); - var scrot = childProcess.spawn(path.join(__dirname, "bin", "scrot", "scrot"), [options.output]); + var scrot = childProcess.spawn(path.join(__dirname, "bin", process.arch !== "arm" ? "scrot" : "arm", "scrot"), [options.output]); scrot.on('close', function(code, signal) { try { fs.statSync(options.output); diff --git a/module.js b/module.js index fdaee33..27e419a 100755 --- a/module.js +++ b/module.js @@ -2,33 +2,26 @@ module.exports = function() { return new Screenshot(arguments); }; -var path = require('flavored-path'); +var path = require('path'); var jimp = require('jimp'); var fs = require('fs'); +var capture = require('./capture') function Screenshot(args) { var config = this.parseArgs(args); var self = this; - try { - require("./capture/" + process.platform + ".js")(config.options, function(error, options) { - // TODO add option for string, rather than file - if(error && typeof config.callback === "function") - config.callback(error, null); - else if(!error) { - if (typeof options.intermediate === "string") { - self.processImage(options.intermediate, options.output, options, function (error, success) { - fs.unlink(options.intermediate, handleCallback); // delete intermediate - }); - } - else - self.processImage(options.output, options.output, options, handleCallback); - } - }); - } - catch(error) { - if(typeof error == "object" && typeof error.code === "string" && error.code === "MODULE_NOT_FOUND") - handleCallback("unsupported_platform"); + if(capture[process.platform]){ + capture[process.platform](config.options, function(error, options) { + // TODO add option for string, rather than file + if(error && typeof config.callback === "function") + config.callback(error, null); + else if(!error) { + self.processImage(options.output, options.output, options, handleCallback); + } + }); + } else { + handleCallback("unsupported_platform"); } function handleCallback(error, success) { @@ -61,7 +54,18 @@ Screenshot.prototype.processImage = function(input, output, options, callback) { if(typeof options.quality === "number" && options.quality >= 0 && options.quality <= 100) image.quality(Math.floor(options.quality)); // only works with JPEGs - image.write(output, callback); + if(options.buffered){ + image.getBuffer(jimp.AUTO, function(error, buffer) { + if(error) { + callback(error); + } + fs.unlink(input, function(errorUnlink) { + callback(errorUnlink, buffer); + }) + }); + } else { + image.write(output, callback); + } } catch(error) { callback(error); @@ -83,7 +87,7 @@ Screenshot.prototype.parseArgs = function(args) { config.callback = args[property]; break; case "object": - if(args[property] != null) + if(args[property] !== null) config.options = args[property]; break; } diff --git a/package.json b/package.json index 8606fab..4b3c589 100755 --- a/package.json +++ b/package.json @@ -12,8 +12,7 @@ "url": "https://github.com/johnvmt/node-desktop-screenshot" }, "dependencies": { - "jimp": "0.2.x", - "flavored-path": "0.0.x" + "jimp": "0.2.x" }, "main": "module.js" }