mirror of
https://github.com/iptv-org/iptv.git
synced 2025-05-12 10:00:05 -04:00
Update scripts
This commit is contained in:
parent
8a83f23243
commit
f1d2add19a
98 changed files with 2423 additions and 1499 deletions
82
scripts/core/storage.ts
Normal file
82
scripts/core/storage.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { File, Collection } from './'
|
||||
import * as path from 'path'
|
||||
import fs from 'fs-extra'
|
||||
import { glob } from 'glob'
|
||||
|
||||
export class Storage {
|
||||
rootDir: string
|
||||
|
||||
constructor(rootDir?: string) {
|
||||
this.rootDir = rootDir || './'
|
||||
}
|
||||
|
||||
list(pattern: string): Promise<string[]> {
|
||||
return glob(pattern, {
|
||||
cwd: this.rootDir
|
||||
})
|
||||
}
|
||||
|
||||
async createDir(dir: string): Promise<void> {
|
||||
if (await fs.exists(dir)) return
|
||||
|
||||
await fs.mkdir(dir, { recursive: true }).catch(console.error)
|
||||
}
|
||||
|
||||
async load(filepath: string): Promise<any> {
|
||||
return this.read(filepath)
|
||||
}
|
||||
|
||||
async read(filepath: string): Promise<any> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
return await fs.readFile(absFilepath, { encoding: 'utf8' })
|
||||
}
|
||||
|
||||
async json(filepath: string): Promise<any> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const content = await fs.readFile(absFilepath, { encoding: 'utf8' })
|
||||
|
||||
return JSON.parse(content)
|
||||
}
|
||||
|
||||
async exists(filepath: string): Promise<boolean> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
return await fs.exists(absFilepath)
|
||||
}
|
||||
|
||||
async write(filepath: string, data: string = ''): Promise<void> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const dir = path.dirname(absFilepath)
|
||||
|
||||
await this.createDir(dir)
|
||||
await fs.writeFile(absFilepath, data, { encoding: 'utf8', flag: 'w' })
|
||||
}
|
||||
|
||||
async append(filepath: string, data: string = ''): Promise<void> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
|
||||
await fs.appendFile(absFilepath, data, { encoding: 'utf8', flag: 'w' })
|
||||
}
|
||||
|
||||
async clear(filepath: string): Promise<void> {
|
||||
await this.write(filepath)
|
||||
}
|
||||
|
||||
async createStream(filepath: string): Promise<NodeJS.WriteStream> {
|
||||
const absFilepath = path.join(this.rootDir, filepath)
|
||||
const dir = path.dirname(absFilepath)
|
||||
|
||||
await this.createDir(dir)
|
||||
|
||||
return fs.createWriteStream(absFilepath) as unknown as NodeJS.WriteStream
|
||||
}
|
||||
|
||||
async save(filepath: string, content: string): Promise<void> {
|
||||
await this.write(filepath, content)
|
||||
}
|
||||
|
||||
async saveFile(file: File): Promise<void> {
|
||||
await this.write(file.filepath, file.content)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue