-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(muti-rooms): diff rooms now has diff memo
- Loading branch information
Showing
7 changed files
with
162 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,81 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import * as crypto from 'crypto'; | ||
|
||
export class FileSystemService { | ||
private static configDir: string = path.join(os.homedir(), '.job-wx-bot'); | ||
private static baseDir: string = path.join(os.homedir(), '.job-wx-bot'); | ||
private static roomsDir: string = path.join(FileSystemService.baseDir, 'rooms'); | ||
|
||
static initialize(): void { | ||
if (!fs.existsSync(this.configDir)) { | ||
fs.mkdirSync(this.configDir, { recursive: true }); | ||
if (!fs.existsSync(this.baseDir)) { | ||
fs.mkdirSync(this.baseDir, { recursive: true }); | ||
} | ||
if (!fs.existsSync(this.roomsDir)) { | ||
fs.mkdirSync(this.roomsDir, { recursive: true }); | ||
} | ||
} | ||
|
||
private static getRoomHash(roomTopic: string): string { | ||
return crypto.createHash('md5').update(roomTopic).digest('hex'); | ||
} | ||
|
||
private static getRoomDir(roomTopic: string): string { | ||
const roomHash = this.getRoomHash(roomTopic); | ||
const roomDir = path.join(this.roomsDir, roomHash); | ||
if (!fs.existsSync(roomDir)) { | ||
fs.mkdirSync(roomDir, { recursive: true }); | ||
} | ||
return roomDir; | ||
} | ||
|
||
static readFile(fileName: string): string { | ||
const filePath = path.join(this.configDir, fileName); | ||
static readFile(roomTopic: string, fileName: string): string { | ||
const filePath = path.join(this.getRoomDir(roomTopic), fileName); | ||
return fs.readFileSync(filePath, 'utf8'); | ||
} | ||
|
||
static writeFile(fileName: string, data: string): void { | ||
const filePath = path.join(this.configDir, fileName); | ||
static writeFile(roomTopic: string, fileName: string, data: string): void { | ||
const filePath = path.join(this.getRoomDir(roomTopic), fileName); | ||
fs.writeFileSync(filePath, data); | ||
} | ||
|
||
static fileExists(fileName: string): boolean { | ||
const filePath = path.join(this.configDir, fileName); | ||
static fileExists(roomTopic: string, fileName: string): boolean { | ||
const filePath = path.join(this.getRoomDir(roomTopic), fileName); | ||
return fs.existsSync(filePath); | ||
} | ||
|
||
static readJSON<T>(fileName: string): T { | ||
const data = this.readFile(fileName); | ||
static readJSON<T>(roomTopic: string, fileName: string): T { | ||
const data = this.readFile(roomTopic, fileName); | ||
return JSON.parse(data) as T; | ||
} | ||
|
||
static writeJSON(fileName: string, data: any): void { | ||
static writeJSON(roomTopic: string, fileName: string, data: any): void { | ||
const jsonData = JSON.stringify(data, null, 2); | ||
this.writeFile(fileName, jsonData); | ||
this.writeFile(roomTopic, fileName, jsonData); | ||
} | ||
|
||
static writeGlobalFile(fileName: string, data: string): void { | ||
const filePath = path.join(this.baseDir, fileName); | ||
fs.writeFileSync(filePath, data); | ||
} | ||
|
||
static readGlobalFile(fileName: string): string { | ||
const filePath = path.join(this.baseDir, fileName); | ||
return fs.readFileSync(filePath, 'utf8'); | ||
} | ||
|
||
static globalFileExists(fileName: string): boolean { | ||
const filePath = path.join(this.baseDir, fileName); | ||
return fs.existsSync(filePath); | ||
} | ||
|
||
static writeGlobalJSON(fileName: string, data: any): void { | ||
const jsonData = JSON.stringify(data, null, 2); | ||
this.writeGlobalFile(fileName, jsonData); | ||
} | ||
|
||
static readGlobalJSON<T>(fileName: string): T { | ||
const data = this.readGlobalFile(fileName); | ||
return JSON.parse(data) as T; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.