mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Lotsa typescript bullshit
This commit is contained in:
parent
b7ac485689
commit
b927988017
65 changed files with 801 additions and 8447 deletions
|
@ -1,21 +1,33 @@
|
|||
module.exports = {
|
||||
ReadFile: ReadFile,
|
||||
ReadJSON: ReadJSON,
|
||||
WriteFile: WriteFile,
|
||||
writeFileAsync: WriteFileAsync,
|
||||
AppendToFile: AppendToFile,
|
||||
FileExists: FileExists,
|
||||
CreatePath: CreatePath,
|
||||
WatchFile: WatchFile,
|
||||
ReadDir: ReadDir,
|
||||
CopyFile: CopyFile,
|
||||
GetDateString: GetDateString,
|
||||
export default {
|
||||
ReadFile,
|
||||
ReadJSON,
|
||||
WriteFile,
|
||||
writeFileAsync,
|
||||
AppendToFile,
|
||||
FileExists,
|
||||
CreatePath,
|
||||
WatchFile,
|
||||
ReadDir,
|
||||
CopyFile,
|
||||
GetDateString,
|
||||
formatUrl,
|
||||
}
|
||||
|
||||
import fs from 'fs'
|
||||
import logger from '../utils/logger.js'
|
||||
import logger from '../utils/logger'
|
||||
|
||||
function GetDateString(noTime) {
|
||||
interface URLFormatOptions {
|
||||
query: any
|
||||
pathname?: string
|
||||
}
|
||||
|
||||
// TODO
|
||||
function formatUrl(options: URLFormatOptions): string {
|
||||
console.log(options.pathname, options.query)
|
||||
return options.pathname + JSON.stringify(options.query)
|
||||
}
|
||||
|
||||
function GetDateString(noTime?: boolean): string {
|
||||
const date = new Date()
|
||||
|
||||
if (noTime) {
|
||||
|
@ -43,16 +55,16 @@ function GetDateString(noTime) {
|
|||
}
|
||||
}
|
||||
|
||||
function CopyFile(from, to) {
|
||||
function CopyFile(from: string, to: string): void {
|
||||
CreatePath(to)
|
||||
fs.copyFileSync(from, to)
|
||||
}
|
||||
|
||||
function ReadDir(path) {
|
||||
function ReadDir(path: string): Array<string> {
|
||||
return fs.readdirSync(path)
|
||||
}
|
||||
|
||||
function ReadJSON(name) {
|
||||
function ReadJSON(name: string): any {
|
||||
try {
|
||||
return JSON.parse(ReadFile(name))
|
||||
} catch (err) {
|
||||
|
@ -61,18 +73,18 @@ function ReadJSON(name) {
|
|||
}
|
||||
}
|
||||
|
||||
function ReadFile(name) {
|
||||
function ReadFile(name: string): string {
|
||||
if (!FileExists(name)) {
|
||||
throw new Error('No such file: ' + name)
|
||||
}
|
||||
return fs.readFileSync(name, 'utf8')
|
||||
}
|
||||
|
||||
function FileExists(path) {
|
||||
function FileExists(path: string): boolean {
|
||||
return fs.existsSync(path)
|
||||
}
|
||||
|
||||
function WatchFile(file, callback) {
|
||||
function WatchFile(file: string, callback: Function): void {
|
||||
if (FileExists(file)) {
|
||||
fs.watchFile(file, () => {
|
||||
fs.readFile(file, 'utf8', (err, data) => {
|
||||
|
@ -84,14 +96,11 @@ function WatchFile(file, callback) {
|
|||
})
|
||||
})
|
||||
} else {
|
||||
console.log(file + ' does not eadjsalék')
|
||||
setTimeout(() => {
|
||||
WatchFile(file)
|
||||
}, 1000)
|
||||
throw new Error(`${file} does not exits to watch`)
|
||||
}
|
||||
}
|
||||
|
||||
function CreatePath(path, onlyPath) {
|
||||
function CreatePath(path: string, onlyPath?: boolean): void {
|
||||
if (FileExists(path)) {
|
||||
return
|
||||
}
|
||||
|
@ -114,12 +123,12 @@ function CreatePath(path, onlyPath) {
|
|||
}
|
||||
}
|
||||
|
||||
function WriteFile(content, path) {
|
||||
function WriteFile(content: string, path: string): void {
|
||||
CreatePath(path)
|
||||
fs.writeFileSync(path, content)
|
||||
}
|
||||
|
||||
function WriteFileAsync(content, path) {
|
||||
function writeFileAsync(content: string, path: string): void {
|
||||
CreatePath(path)
|
||||
fs.writeFile(path, content, function(err) {
|
||||
if (err) {
|
||||
|
@ -131,7 +140,7 @@ function WriteFileAsync(content, path) {
|
|||
})
|
||||
}
|
||||
|
||||
function AppendToFile(data, file) {
|
||||
function AppendToFile(data: string, file: string): void {
|
||||
CreatePath(file)
|
||||
try {
|
||||
fs.appendFileSync(file, '\n' + data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue