File size: 1,121 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs-extra');

import { ISessionAuthRepository } from './ISessionAuthRepository';
import { LocalStore } from './LocalStore';
// Keep all waha related files, ".waha.session.*"
const KEEP_FILES = /^\.waha\.session\..*$/;

export class LocalSessionAuthRepository extends ISessionAuthRepository {
  private store: LocalStore;

  constructor(store: LocalStore) {
    super();
    this.store = store;
  }

  async init(sessionName?: string) {
    await this.store.init(sessionName);
  }

  async clean(sessionName: string) {
    // Remove all files and directories recursively, but keep waha files
    const sessionDirectory = this.store.getSessionDirectory(sessionName);
    // Check it exists and it's directory
    const exists = await fs.pathExists(sessionDirectory);
    if (!exists) {
      return;
    }
    const files = await fs.readdir(sessionDirectory);
    const filesToRemove = files.filter((file) => !file.match(KEEP_FILES));
    for (const file of filesToRemove) {
      await fs.remove(`${sessionDirectory}/${file}`);
    }
  }
}