mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 20:49:14 +00:00
feat(dev): Interactive shell for quick testing/exploration
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
"typings": "./index.d.ts",
|
"typings": "./index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"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"
|
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
39
shell.js
Normal file
39
shell.js
Normal file
@@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user