From 3f5f731700d479389541faad26296ccc37afb42a Mon Sep 17 00:00:00 2001 From: BearingMe Date: Mon, 4 Jan 2021 22:06:18 -0300 Subject: [PATCH] [REFACTOR] changed from es6 module to commonjs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pt: para permitir a utilização de bibliotecas antigas foi feita a alteração na forma que os modulos eram importados eng: to allow the use of old libraries was made in change in the way that modules were imported --- auth.mjs => auth.js | 13 ++++++++----- index.js | 12 +++++------- package.json | 1 - parse.mjs => parse.js | 9 ++++++--- ping.mjs => ping.js | 11 +++++++---- 5 files changed, 26 insertions(+), 20 deletions(-) rename auth.mjs => auth.js (75%) rename parse.mjs => parse.js (91%) rename ping.mjs => ping.js (65%) diff --git a/auth.mjs b/auth.js similarity index 75% rename from auth.mjs rename to auth.js index 4145b9f..f0be2bd 100644 --- a/auth.mjs +++ b/auth.js @@ -1,11 +1,11 @@ -import qrcode from 'qrcode-terminal' -import fs from 'fs' +const qrcode = require('qrcode-terminal') +const fs = require('fs') // path where the session data will be stored const SESSION_FILE_PATH = './session.json'; // load the session data if it has been previously saved -export function loadPreviousSession() { +function loadPreviousSession() { if (fs.existsSync(SESSION_FILE_PATH)) { let raw = fs.readFileSync('./session.json') let data = JSON.parse(raw) @@ -15,7 +15,7 @@ export function loadPreviousSession() { } // save session values to the file upon successful auth -export function startCurrentSession(client, sessionData) { +function startCurrentSession(client, sessionData) { // generate and scan this code with your phone client.on('qr', (qr) => { qrcode.generate(qr, { small: true }); @@ -33,4 +33,7 @@ export function startCurrentSession(client, sessionData) { }); client.initialize(); -} \ No newline at end of file +} + +exports.loadPreviousSession = loadPreviousSession +exports.startCurrentSession = startCurrentSession \ No newline at end of file diff --git a/index.js b/index.js index d2a2910..73bd99d 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,8 @@ -import axios from 'axios' -import fs from 'fs' -import whatsapp from 'whatsapp-web.js' -import express from 'express' -import { loadPreviousSession, startCurrentSession } from './auth.mjs' -import { parseBody } from './parse.mjs' -import { startServer, pingServer } from './ping.mjs' +const axios = require('axios') +const whatsapp = require('whatsapp-web.js') +const { loadPreviousSession, startCurrentSession } = require('./auth.js') +const { parseBody } = require('./parse.js') +const { startServer, pingServer } = require('./ping.js') // constants const sessionData = loadPreviousSession() diff --git a/package.json b/package.json index 89871c3..7ea3dca 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "0.0.8", "description": "", "main": "index.js", - "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" diff --git a/parse.mjs b/parse.js similarity index 91% rename from parse.mjs rename to parse.js index dea81ae..7da7b8e 100644 --- a/parse.mjs +++ b/parse.js @@ -4,7 +4,7 @@ function remove(substring) { String.prototype.remove = remove; -export function parseArgs(params) { +function parseArgs(params) { const pattern = /([^=]+)[\s]*=[\s]*\'([^']+)\'[\s]*(,)?/ let matches = params.match(pattern) @@ -33,7 +33,7 @@ export function parseArgs(params) { } -export async function parseBody(messageObject, callback) { +async function parseBody(messageObject, callback) { const pattern = /(#|!)([^(\s]+)(\([^)]+\))?/; let lowerMessageBody = messageObject.body.toLowerCase(); let matches = lowerMessageBody.match(pattern); @@ -68,4 +68,7 @@ export async function parseBody(messageObject, callback) { await callback(parsedData); } -} \ No newline at end of file +} + +exports.parseArgs = parseArgs +exports.parseBody = parseBody diff --git a/ping.mjs b/ping.js similarity index 65% rename from ping.mjs rename to ping.js index 010172f..d281126 100644 --- a/ping.mjs +++ b/ping.js @@ -1,7 +1,7 @@ -import express from 'express' -import axios from 'axios' +const express = require('express') +const axios = require('axios') -export function startServer() { +function startServer() { const app = express() const port = process.env.PORT || 3000 @@ -12,10 +12,13 @@ export function startServer() { } -export function pingServer() { +function pingServer() { setInterval(() => { axios.get('http://aliceclient.herokuapp.com/') .then(res => console.log(res)) .catch(err => console.log(err)) }, 30*1000); } + +exports.startServer = startServer +exports.pingServer = pingServer