Skip to content

Commit

Permalink
Remove spinnies for more sane logs
Browse files Browse the repository at this point in the history
  • Loading branch information
9cb14c1ec0 committed Nov 10, 2024
1 parent 8f846c9 commit 52a7a1b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 597 deletions.
117 changes: 0 additions & 117 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
"qrcode-terminal": "^0.12.0",
"sanitize-filename": "^1.6.3",
"sharp": "^0.33.1",
"spinnies": "^0.5.1",
"unzipper": "^0.11.4",
"ws": "^8.8.0",
"yoo-hoo": "^1.2.0"
Expand Down
19 changes: 5 additions & 14 deletions src/api/helpers/delete-file.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import path from 'path';
import { existsSync, unlink } from 'fs';
import * as Spinnies from 'spinnies';
export async function deleteFiles(
mergedOptions: any,
Session: String,
spinnies: Spinnies
) {
export async function deleteFiles(mergedOptions: any, Session: String) {
try {
spinnies.add(`removeFile`, { text: '....' });
const pathTokens: string = path.join(
path.resolve(
process.cwd() + mergedOptions.mkdirFolderToken,
Expand All @@ -18,16 +12,13 @@ export async function deleteFiles(
if (existsSync(pathTokens)) {
unlink(pathTokens, (err) => {
if (err) {
spinnies.fail(`removeFile`, {
text: `Not removed file: ${pathTokens}`
});
console.error(`Not removed file: ${pathTokens}`);
} else {
console.info(`Removed file: ${pathTokens}`);
}
spinnies.succeed(`removeFile`, {
text: `Removed file: ${pathTokens}`
});
});
} else {
spinnies.fail(`removeFile`, { text: `Not Files: ${pathTokens}` });
console.error(`Not Files: ${pathTokens}`);
}
} catch (e) {}
}
79 changes: 14 additions & 65 deletions src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import {
retrieveQR
} from '../../controllers/auth';
import { sleep } from '../../utils/sleep';
import { getSpinnies } from '../../utils/spinnies';
import * as Spinnies from 'spinnies';

export class HostLayer {
readonly session: string;
readonly options: CreateConfig;

protected spinnies: Spinnies = getSpinnies();
protected spinStatus = {
apiInject: '',
autoCloseRemain: 0,
Expand All @@ -39,54 +36,8 @@ export class HostLayer {
) {
this.session = session;
this.options = { ...defaultOptions, ...options };

// this.spin('Initializing...', 'spinning');
//this._initialize(this.page);
}

protected spin(text?: string, status?: Spinnies.SpinnerStatus) {
const name = `session-${this.session}`;

text = text || this.spinStatus.previousText;
this.spinStatus.previousText = text;

status =
status || (this.spinStatus.previousStatus as Spinnies.SpinnerStatus);
this.spinStatus.previousStatus = status;

let fullText = `[instance: ${this.session}`;
// if (this.spinStatus.state) {
// fullText += `, ${this.spinStatus.state}`;
// }
fullText += `]: ${text}`;

let prevText = '';

try {
prevText = this.spinnies.pick(name).text;
} catch (error) {
this.spinnies.add(name, { text: fullText, status });
prevText = fullText;
}
if (prevText !== fullText) {
this.spinnies.update(name, {
text: fullText,
status
});
}
}

// public async _initialize(page: Page) {
// this.spinStatus.apiInject = 'injecting';
// await injectApi(page)
// .then(() => {
// this.spinStatus.apiInject = 'injected';
// })
// .catch(() => {
// this.spinStatus.apiInject = 'failed';
// });
// }

protected tryAutoClose() {
if (
this.options.autoClose > 0 &&
Expand Down Expand Up @@ -169,7 +120,7 @@ export class HostLayer {
if (this.options.logQR) {
console.log(qr);
} else {
this.spin(`Waiting for QRCode Scan: Attempt ${attempt}`);
console.info(`Waiting for QRCode Scan: Attempt ${attempt}`);
}

if (catchQR) {
Expand Down Expand Up @@ -201,13 +152,11 @@ export class HostLayer {
) {
this.statusFind = statusFind;

this.spin('Waiting page load', 'spinning');

this.spin('Checking is logged...');
console.info('Waiting page load');
let authenticated = await isAuthenticated(this.page).catch(() => null);

if (typeof authenticated === 'object' && authenticated.type) {
this.spin(`Error http: ${authenticated.type}`, 'fail');
console.error(`Error http: ${authenticated.type}`);
this.page.close().catch(() => {});
this.browser.close().catch(() => {});
throw `Error http: ${authenticated.type}`;
Expand All @@ -216,32 +165,32 @@ export class HostLayer {
this.startAutoClose();

if (authenticated === false) {
this.spin('Waiting for QRCode Scan...');
console.info('Waiting for QRCode Scan...');
statusFind && statusFind('notLogged', this.session);

await this.waitForQrCodeScan(catchQR).catch(() => undefined);

this.spin('Checking QRCode status...');
console.info('Checking QRCode status...');

// Wait for interface update
await sleep(200);
authenticated = await isAuthenticated(this.page).catch(() => null);

if (authenticated === null || JSON.stringify(authenticated) === '{}') {
this.spin('Failed to authenticate');
console.error('Failed to authenticate');
statusFind && statusFind('qrReadFail', this.session);
} else if (authenticated) {
this.spin('QRCode Success');
console.info('QRCode Success');
statusFind && statusFind('qrReadSuccess', this.session);
} else {
this.spin('QRCode Fail', 'fail');
console.error('QRCode Fail', 'fail');
statusFind && statusFind('qrReadFail', this.session);
this.cancelAutoClose();
this.tryAutoClose();
throw 'Failed to read the QRCode';
}
} else if (authenticated === true) {
this.spin('Authenticated');
console.info('Authenticated');
statusFind && statusFind('isLogged', this.session);
}

Expand All @@ -251,32 +200,32 @@ export class HostLayer {
this.startAutoClose();
// Wait for interface update
await sleep(200);
this.spin('Checking phone is connected...');
console.info('Checking phone is connected...');
const inChat = await this.waitForInChat();

if (!inChat) {
this.spin('Phone not connected', 'fail');
console.error('Phone not connected');
statusFind && statusFind('phoneNotConnected', this.session);
this.cancelAutoClose();
this.tryAutoClose();
throw new Error('Phone not connected');
}
this.cancelAutoClose();
this.spin('Connected', 'succeed');
console.info('Connected');
// statusFind && statusFind('inChat', this.session);
return true;
}

if (authenticated === false) {
this.cancelAutoClose();
this.tryAutoClose();
this.spin('Not logged', 'fail');
console.error('Not logged');
throw new Error('Not logged');
}

this.cancelAutoClose();
this.tryAutoClose();
this.spin('Unknow error', 'fail');
console.error('Unknown error');
}

//Pro
Expand Down
6 changes: 3 additions & 3 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ListenerLayer extends ProfileLayer {

this.page.on('close', () => {
this.cancelAutoClose();
this.spin('Page Closed', 'fail');
console.error('Page Closed');
});
}

Expand Down Expand Up @@ -441,9 +441,9 @@ export class ListenerLayer extends ProfileLayer {

/**
* @event Listens to participants changed
* @param to group id: [email protected]
* @param to callback
* @returns Stream of ParticipantEvent
* @param groupId
* @param fn
*/
public async onParticipantsChanged(
groupId: string,
Expand Down
Loading

0 comments on commit 52a7a1b

Please sign in to comment.