On-demand loading of JSDOM library

yargs
gardenapple 4 years ago
parent a97b1ee269
commit 366b405ad6
No known key found for this signature in database
GPG Key ID: CAF17E9ABE789268

@ -19,9 +19,8 @@ Firefox Reader Mode in your terminal! CLI tool for Mozilla's Readability library
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
const JSDOM = require("jsdom").JSDOM;
const parseArgs = require("minimist"); const parseArgs = require("minimist");
//fs, Readability, and Readability-readerable are loaded on-demand. //JSDOM, fs, Readability, and Readability-readerable are loaded on-demand.
//To-do: lazy loading? //To-do: lazy loading?
const ExitCodes = { const ExitCodes = {
@ -231,6 +230,7 @@ async function read(stream) {
return Buffer.concat(chunks).toString('utf8'); return Buffer.concat(chunks).toString('utf8');
} }
if (inputIsFromStdin) { if (inputIsFromStdin) {
if (!args["quiet"]) { if (!args["quiet"]) {
console.error("Reading..."); console.error("Reading...");
@ -240,9 +240,11 @@ if (inputIsFromStdin) {
"be broken. Supply the --url parameter to fix.") "be broken. Supply the --url parameter to fix.")
} }
read(process.stdin).then(result => { read(process.stdin).then(result => {
const JSDOM = require("jsdom").JSDOM;
onLoadDOM(new JSDOM(result, { url: documentURL })); onLoadDOM(new JSDOM(result, { url: documentURL }));
}); });
} else { } else {
const JSDOM = require("jsdom").JSDOM;
if (!args["quiet"]) if (!args["quiet"])
console.error("Retrieving..."); console.error("Retrieving...");
let promiseGetHTML; let promiseGetHTML;
@ -284,12 +286,12 @@ function onLoadDOM(dom) {
if (!shouldParseArticle) { if (!shouldParseArticle) {
if (args["low-confidence"] == LowConfidenceMode.exit) { if (args["low-confidence"] == LowConfidenceMode.exit) {
console.error("Not sure if this document should be parsed, exiting"); console.error("Not sure if this document should be processed, exiting");
setErrored(ExitCodes.dataError); setErrored(ExitCodes.dataError);
return; return;
} else { } else {
if (!args["quiet"]) if (!args["quiet"])
console.error("Not sure if this document should be parsed. Not parsing"); console.error("Not sure if this document should be processed. Not processing");
if (!justOutputHtml) { if (!justOutputHtml) {
console.error("Can't output properties"); console.error("Can't output properties");
setErrored(ExitCodes.dataError); setErrored(ExitCodes.dataError);
@ -312,12 +314,12 @@ function onLoadDOM(dom) {
const Readability = require("readability"); const Readability = require("readability");
if (!args["quiet"]) if (!args["quiet"])
console.error("Parsing..."); console.error("Processing...");
const reader = new Readability(document); const reader = new Readability(document);
const article = reader.parse(); const article = reader.parse();
if (!article) { if (!article) {
console.error("Couldn't parse document. This error usually means that the input document is empty."); console.error("Couldn't process document. This error usually means that the input document is empty.");
setErrored(ExitCodes.dataError); setErrored(ExitCodes.dataError);
return; return;
} }

@ -1,6 +1,6 @@
{ {
"name": "@gardenapple/readability-cli", "name": "@gardenapple/readability-cli",
"version": "1.1.0", "version": "1.1.1",
"description": "Firefox Reader Mode in your terminal - get useful text from a web page using Mozilla's Readability library", "description": "Firefox Reader Mode in your terminal - get useful text from a web page using Mozilla's Readability library",
"main": "index.js", "main": "index.js",
"bin": { "bin": {

Loading…
Cancel
Save