添加 io 与 path 工具模块

This commit is contained in:
2020-05-17 22:25:25 +08:00
parent 8a10db1037
commit c7f9bfbd61
2 changed files with 223 additions and 0 deletions

169
src/tiny/io.ts Normal file
View File

@@ -0,0 +1,169 @@
export enum AccessType {
ACCESS_RESOURCES,
ACCESS_USERDATA,
ACCESS_FILESYSTEM,
ACCESS_MAX
};
export enum ModeFlags {
READ = 1,
WRITE = 2,
READ_WRITE = 3,
WRITE_READ = 7,
};
export class FileAccess {
protected path: string;
/** close a file */
close(): void {};
/** true when file is open */
is_open(): boolean { return false; }
/** returns the path for the current open file */
get_path(): string { return ""; }
/** returns the absolute path for the current open file */
get_path_absolute(): string { return ""; }
/** seek to a given position */
seek(p_position: number) {}
/** seek from the end of file */
seek_end(p_position: number) {}
/* get position in the file */
get_position(): number { return -1; }
/* get size of the file */
get_len(): number { return -1; }
/** reading passed EOF */
eof_reached(): boolean { return false; }
get_as_utf8_string(): string { return ""; }
save_as_utf8_string(text: string) {}
get_as_array(): Uint8Array { return null; }
save_as_array(buffer: Uint8Array) {}
store_string(text: string) {}
static create(path: string): FileAccess {
return null;
}
static open(path: string, mode_flags: ModeFlags): FileAccess {
switch (get_runtime()) {
case JavaScriptRuntime.NodeJS:
return NodeJSFileAccess.open(path, mode_flags);
default:
return null;
}
}
static exists(path: string): boolean {
switch (get_runtime()) {
case JavaScriptRuntime.NodeJS:
return NodeJSFileAccess.exists(path);
default:
return false;
}
}
}
export class DirAccess {
static exists(path: string): boolean {
switch (get_runtime()) {
case JavaScriptRuntime.NodeJS:
return NodeJSFileAccess.exists(path);
default:
return false;
}
}
static make_dir(p_dir: string, recursive: boolean = false) {
switch (get_runtime()) {
case JavaScriptRuntime.NodeJS:
return NodeJSDirAccess.make_dir(p_dir, recursive);
default:
return false;
}
};
}
import * as fs from "fs";
import {O_RDONLY, O_WRONLY, O_RDWR, O_CREAT } from 'constants';
import { get_runtime, JavaScriptRuntime } from "./env";
class NodeJSFileAccess extends FileAccess {
protected fd: number = -1;
protected pos: number = 0;
constructor(path, flags: number) {
super();
this.fd = fs.openSync(path, flags);
this.path = path;
}
close(): void {
fs.closeSync(this.fd);
};
static exists(path: string): boolean {
return fs.existsSync(path);
}
static open(path: string, mode_flags: ModeFlags): NodeJSFileAccess {
let flags = 0;
switch (mode_flags) {
case ModeFlags.READ:
flags = O_RDONLY;
break;
case ModeFlags.WRITE:
flags = O_CREAT | O_WRONLY;
break;
case ModeFlags.READ_WRITE:
flags = O_CREAT | O_RDWR;
break;
case ModeFlags.WRITE_READ:
flags = O_CREAT | O_RDWR;
break;
default:
break;
}
return new NodeJSFileAccess(path, flags);
}
get_len(): number {
return fs.statSync(this.path).size;
}
get_as_utf8_string(): string {
return fs.readFileSync(this.path, {encoding: 'utf-8'});
}
save_as_utf8_string(text: string) {
return fs.writeFileSync(this.path, text, {encoding: 'utf-8'});
}
get_as_array(): Uint8Array {
let buff = fs.readFileSync(this.path);
return buff;
}
save_as_array(buffer: Uint8Array) {
fs.writeFileSync(this.path, buffer);
}
}
export class NodeJSDirAccess extends DirAccess {
static exists(path: string): boolean {
return fs.statSync(path).isDirectory();
}
static make_dir(p_dir: string, recursive: boolean = false) {
fs.mkdirSync(p_dir, {recursive: true});
}
}

54
src/tiny/path.ts Normal file
View File

@@ -0,0 +1,54 @@
/**
* 对文件路径的一些操作,针对的是 C:/A/B/C/D/example.ts这种格式
*/
export const path = {
/**
* 格式化文件路径,"C:/A/B//C//D//example.ts"=>"C:/A/B/C/D/example.ts"
* @param filename 传入的文件路径
*/
normalize: (filename: string): string => {
const arr = filename.split("/");
return arr.filter((value, index) => !!value || index == arr.length - 1).join("/");
},
/**
* 根据文件路径得到文件名字,"C:/A/B/example.ts"=>"example.ts"
* @param filename 传入的文件路径
* @return 文件的名字
*/
basename: (filename: string): string => {
return filename.substr(filename.lastIndexOf("/") + 1);
},
/**
* 文件所在文件夹路径,"C:/A/B/example.ts"=>"C:/A/B"
* @param path 传入的文件路径
* @return 文件所在文件夹的地址
*/
dirname: (path: string): string => {
return path.substr(0, path.lastIndexOf("/"));
},
/**
* 获得文件拓展名 " text.txt" => "txt"
* @param path 传入的文件路径
*/
extension: (filename: string): string => {
let pos = filename.lastIndexOf('.');
if (pos >= 0 && pos < filename.length - 1) {
return filename.substring(pos + 1, filename.length);
}
return "";
},
join(dir: string, ...pathes: string[]) {
let ret = dir;
for (const p of pathes) {
if (p) {
ret += '/' + p;
}
}
return ret;
}
}