feat: update articles or snippets

This commit is contained in:
wx-chevalier
2020-06-23 15:53:00 +08:00
parent d3cc4cf24d
commit 39d39248c8
3 changed files with 96 additions and 68 deletions

View File

@@ -1,59 +1,72 @@
module.exports = function(options, callback) {
var path = require('path');
var fs = require('fs');
var childProcess = require('child_process');
module.exports = function (options, callback) {
var path = require("path");
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
// 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.replace('app.asar', 'app.asar.unpacked'), 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
// 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.replace("app.asar", "app.asar.unpacked"),
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
function callbackReturn(error, success) {
// called from capture
// callback with options, in case options added
callback(error, options);
}
function callbackReturn(error, success) {
// called from capture
// callback with options, in case options added
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 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 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),
"-x",
output
];
function capture(output, callback) {
var cmd = "screencapture";
var args = [
// will create PNG by default
"-t",
path.extname(output).toLowerCase().substring(1),
"-x",
output,
];
var captureChild = childProcess.spawn(cmd, args);
var captureChild = childProcess.spawn(cmd, args);
captureChild.on('close', function(error) {
if (error)
callback(error.toString());
else
callback();
});
captureChild.on("close", function (error) {
if (error) callback(error.toString());
else callback();
});
captureChild.stderr.on('data', function(data) {
callback(data.toString());
});
}
};
captureChild.stderr.on("data", function (data) {
callback(data.toString());
});
}
};

View File

@@ -1,18 +1,33 @@
module.exports = function(options, callback) {
module.exports = function (options, callback) {
var fs = require("fs");
var childProcess = require("child_process");
var path = require("path");
var fs = require('fs');
var childProcess = require('child_process');
var path = require('path');
var nircmd = childProcess.spawn(path.join(__dirname.replace('app.asar', 'app.asar.unpacked'), "bin", "nircmd.exe"), ["savescreenshot", options.output]);
const cmdPath1 = path.join(
__dirname.replace("app.asar", "app.asar.unpacked"),
"bin",
"nircmd.exe"
);
nircmd.on('close', function(code, signal) {
try {
fs.statSync(options.output);
callback(null, options); // callback with options, in case options added
}
catch(error) {
callback("file_not_found", null);
}
});
};
// 有时候会在 node_modules 路径下
const cmdPath2 = path.join(
__dirname.replace("app.asar", "app.asar.unpacked"),
"node_modules/node-desktop-capturer/capture",
"bin",
"nircmd.exe"
);
var nircmd = childProcess.spawn(
fs.existsSync(cmdPath1) ? cmdPath1 : cmdPath2,
["savescreenshot", options.output]
);
nircmd.on("close", function (code, signal) {
try {
fs.statSync(options.output);
callback(null, options); // callback with options, in case options added
} catch (error) {
callback("file_not_found", null);
}
});
};

View File

@@ -1,6 +1,6 @@
{
"name": "node-desktop-capturer",
"version": "0.0.1",
"version": "0.0.3",
"description": "Cross-platform screenshot module, using external tools",
"license": "MIT",
"author": {