From 382259488ba92be9e071634033a07def87d66fcd Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Mon, 17 Aug 2020 23:43:50 -0400 Subject: [PATCH] feat(dev): Interactive shell for quick testing/exploration --- package.json | 1 + shell.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 shell.js diff --git a/package.json b/package.json index 91a1ea5..ddab8fb 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "typings": "./index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", + "shell": "node --experimental-repl-await ./shell.js", "generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose" }, "repository": { diff --git a/shell.js b/shell.js new file mode 100644 index 0000000..10e44d0 --- /dev/null +++ b/shell.js @@ -0,0 +1,39 @@ +/** + * ==== wwebjs-shell ==== + * Used for quickly testing library features + * + * Running `npm run shell` will start WhatsApp Web in headless mode + * and then drop you into Node REPL with `client` in its context. + */ + +const repl = require('repl'); +const fs = require('fs'); + +const { Client } = require('./index'); + +const SESSION_FILE_PATH = './session.json'; +let sessionCfg; +if (fs.existsSync(SESSION_FILE_PATH)) { + sessionCfg = require(SESSION_FILE_PATH); +} + +const client = new Client({ + puppeteer: { headless: false }, + session: sessionCfg +}); + +console.log('Initializing...'); + +client.initialize(); + +client.on('qr', () => { + console.log('Please scan the QR code on the browser.'); +}); + +client.on('ready', () => { + const shell = repl.start('wwebjs> '); + shell.context.client = client; + shell.on('exit', async () => { + await client.destroy(); + }); +});