ktfile 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let t=[];function s(s,e=t){s.startsWith("/")?(e=[],s=s.slice(1)):e=[...e];const i=s.split("/");for(const t of i)".."===t?e.pop():"."!==t&&""!==t&&e.push(t);return e}"undefined"!=typeof process&&"cwd"in process&&"function"==typeof process.cwd&&(t=s(process.cwd()));const e=new Map;function i(t,s,e){const i=t.listeners(s);t.removeAllListeners(s);for(const n of i)n!==e&&t.on(s,n);t.on(s,e)}function n(){for(const[t,s]of e.entries())new d(t).delete(s);e.clear(),"undefined"!=typeof process?(process.off("SIGINT",r),process.off("SIGTERM",l),process.off("exit",n)):"undefined"!=typeof window&&(window.removeEventListener("beforeunload",n),window.removeEventListener("pagehide",n))}function r(){n(),process.exit(130)}function l(){n(),process.exit(143)}function a(){"undefined"!=typeof process?(i(process,"exit",n),i(process,"SIGINT",r),i(process,"SIGTERM",l)):"undefined"!=typeof window&&(window.addEventListener("beforeunload",n),window.addEventListener("pagehide",n))}class h{split;constructor(t){this.split="string"==typeof t?s(t):[...t]}get fullPath(){return 0===this.split.length?this.separator:this.split.join(this.separator)}createWriteStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),t);throw new Error("File system does not support createWriteStream")}createReadStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),t);throw new Error("File system does not support createReadStream")}createAppendStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),{flags:"a",encoding:t});throw new Error("File system does not support createAppendStream")}createInputStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),{flags:"r",encoding:t});throw new Error("File system does not support createInputStream")}}async function u(t){try{return t().then(()=>!0).catch(()=>!1)}catch(t){return!1}}async function f(t){try{return t().then(t=>t).catch(()=>null)}catch(t){return null}}class c extends h{static fs;static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTemp"in c.fs))throw new Error("mkdTemp is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtemp(`${i}/${s}-`);return new c(`${n}${e}`)}get fs(){return c.fs}canExecute(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.X_OK??1))}async setExecutable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?493:420))?this:null}canRead(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.R_OK??4))}async setReadable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?420:0))?this:null}canWrite(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.W_OK??2))}async setWritable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?420:292))?this:null}creationTime(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.birthtime).catch(()=>null))}async setCreationTime(t){return await u(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async lastModified(){return await f(()=>this.fs.stat(this.fullPath).then(t=>t.mtime).catch(()=>null))}async setLastModified(t){return await u(()=>this.fs.utimes(this.fullPath,t,t))?this:null}exists(){return f(()=>this.fs.exists(this.fullPath))}lastAccess(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.atime).catch(()=>null))}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new c(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return"sep"in this.fs&&"string"==typeof this.fs.sep?this.fs.sep:"/"}isDirectory(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.isDirectory()).catch(()=>null))}isFile(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.isFile()).catch(()=>null))}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}isSymbolicLink(){return f(()=>this.fs.lstat(this.fullPath).then(t=>t.isSymbolicLink()).catch(()=>null))}size(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.size).catch(()=>null))}async sizeKB(){const t=await this.size();return null!==t?t/1024:null}async sizeMB(){const t=await this.size();return null!==t?t/1048576:null}async sizeGB(){const t=await this.size();return null!==t?t/1073741824:null}to(...t){return new c(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}async createFile(){return await this.exists()?await this.isFile()?this:null:await this.write("")?this:null}async delete(t,s=t){if(t){if("rmSync"in this.fs)return await u(()=>this.fs.rm(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return await u(()=>this.fs.rmdir(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of await this.listFiles()||[])await s.delete(!0)||(t=!0);if(t)return null}return await u(()=>this.fs.unlink(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}async clear(t){if(t){const t=await this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])await e.delete(!0)||(s=!0);return s?null:this}return await this.write(""),null}async listFiles(){if(!this.isDirectory)return null;const t=await f(()=>this.fs.readdir(this.fullPath));return t?t.map(t=>new c(`${this.fullPath}/${t}`)):null}listFilenames(){return f(()=>this.fs.readdir(this.fullPath))}async mkdir(t=!1){return await u(()=>this.fs.mkdir(this.fullPath,{recursive:t}))?this:null}async mkdirs(){return await this.mkdir(!0)?this:null}async renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||await t.delete(e)?(e&&await(this.parent?.mkdirs()),await u(()=>this.fs.rename(this.fullPath,t.fullPath))?this:null):null}async copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!await t.delete(e))return null;if(this.isFile){const s=await this.read();return null===s?null:await t.write(s)?this:null}if(!await t.mkdir(e))return null;const i=await this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);await r.copyTo(i,s,e)||(n=!0)}return n?null:this}async*walk(){if(this.isDirectory){yield this;for(const t of await this.listFiles()||[])yield*t.walk()}else yield this}read(t){return f(()=>this.fs.readFile(this.fullPath,t))}async readLines(t="utf8"){const s=await this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}async readJSON(){const t=await this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}async readlink(){if(!this.isSymbolicLink)return null;const t=await f(()=>this.fs.readlink(this.fullPath));return t?new c(t):null}async write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),await u(()=>this.fs.writeFile(this.fullPath,t,s))?this:null}async writeJSON(t){return await u(()=>this.write(JSON.stringify(t,null,2)))?this:null}async append(t,s){return await u(()=>this.fs.appendFile(this.fullPath,t,s))?this:null}get sync(){return new d(this.split)}}function o(t){try{return t(),!0}catch(t){return!1}}function p(t){try{return t()}catch(t){return null}}class d extends h{static fs;static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTempSync"in d.fs))throw new Error("mkdTempSync is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtempSync(`${i}/${s}-`);return new d(`${n}${e}`)}get fs(){return d.fs}get canExecute(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.X_OK??1))}set canExecute(t){o(()=>this.fs.chmodSync(this.fullPath,t?493:420))}get canRead(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.R_OK??4))}set canRead(t){o(()=>this.fs.chmodSync(this.fullPath,t?420:0))}get canWrite(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.W_OK??2))}set canWrite(t){o(()=>this.fs.chmodSync(this.fullPath,t?420:292))}get creationTime(){return p(()=>this.fs.statSync(this.fullPath).birthtime)}set creationTime(t){o(()=>this.fs.utimesSync(this.fullPath,t,t))}get lastModified(){return p(()=>this.fs.statSync(this.fullPath).mtime)}set lastModified(t){o(()=>this.fs.utimesSync(this.fullPath,t,t))}get exists(){return p(()=>this.fs.existsSync(this.fullPath))}get lastAccess(){return p(()=>this.fs.statSync(this.fullPath).atime)}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new d(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return"sep"in this.fs&&"string"==typeof this.fs.sep?this.fs.sep:"/"}get isDirectory(){return p(()=>this.fs.statSync(this.fullPath).isDirectory())}get isFile(){return p(()=>this.fs.statSync(this.fullPath).isFile())}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}get isSymbolicLink(){return p(()=>this.fs.lstatSync(this.fullPath).isSymbolicLink())}get size(){return p(()=>this.fs.statSync(this.fullPath).size)}get sizeKB(){const t=this.size;return null!==t?t/1024:null}get sizeMB(){const t=this.size;return null!==t?t/1048576:null}get sizeGB(){const t=this.size;return null!==t?t/1073741824:null}to(...t){return new d(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}createFile(){return this.exists?this.isFile?this:null:this.write("")?this:null}delete(t,s=t){if(t){if("rmSync"in this.fs)return o(()=>this.fs.rmSync(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return o(()=>this.fs.rmdirSync(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of this.listFiles()||[])s.delete(!0)||(t=!0);if(t)return null}return o(()=>this.fs.unlinkSync(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}clear(t){if(t){const t=this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])e.delete(!0)||(s=!0);return s?null:this}return this.write(""),null}listFiles(){if(!this.isDirectory)return null;const t=p(()=>this.fs.readdirSync(this.fullPath));return t?t.map(t=>new d(`${this.fullPath}/${t}`)):null}listFilenames(){return p(()=>this.fs.readdirSync(this.fullPath))}mkdir(t=!1){return o(()=>this.fs.mkdirSync(this.fullPath,{recursive:t}))?this:null}mkdirs(){return this.mkdir(!0)?this:null}renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||t.delete(e)?(e&&this.parent?.mkdirs(),o(()=>this.fs.renameSync(this.fullPath,t.fullPath))?this:null):null}copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!t.delete(e))return null;if(this.isFile){const s=this.read();return null===s?null:t.write(s)?this:null}if(!t.mkdir(e))return null;const i=this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);r.copyTo(i,s,e)||(n=!0)}return n?null:this}*walk(){if(this.isDirectory){yield this;for(const t of this.listFiles()||[])yield*t.walk()}else yield this}read(t){return p(()=>this.fs.readFileSync(this.fullPath,t))}readLines(t="utf8"){const s=this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}readJSON(){const t=this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}readlink(){if(!this.isSymbolicLink)return null;const t=p(()=>this.fs.readlinkSync(this.fullPath));return t?new d(t):null}write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),o(()=>this.fs.writeFileSync(this.fullPath,t,s))?this:null}writeJSON(t){return o(()=>this.write(JSON.stringify(t,null,2)))?this:null}append(t,s){return o(()=>this.fs.appendFileSync(this.fullPath,t,s))?this:null}get async(){return new c(this.split)}}function y(t){d.fs=t,c.fs=t.promises}function m(t){return new d(t)}function w(t){return new c(t)}if("undefined"!=typeof process)try{y(await import("fs"))}catch{}export{d as File,c as FileAsync,d as FileSync,w as fileAsync,m as fileSync,y as initFS};
|
|
1
|
+
let t=[];function s(s,e=t){"/"===s[0]||"\\"===s[0]?(e=[],s=s.slice(1)):e=[...e];const i=s.split(/[\\/]/g);for(const t of i)".."===t?e.pop():"."!==t&&""!==t&&e.push(t);return e}"undefined"!=typeof process&&"cwd"in process&&"function"==typeof process.cwd&&(t=s(process.cwd()));const e=new Map;function i(t,s,e){const i=t.listeners(s);t.removeAllListeners(s);for(const n of i)n!==e&&t.on(s,n);t.on(s,e)}function n(){for(const[t,s]of e.entries())new d(t).delete(s);e.clear(),"undefined"!=typeof process?(process.off("SIGINT",r),process.off("SIGTERM",l),process.off("exit",n)):"undefined"!=typeof window&&(window.removeEventListener("beforeunload",n),window.removeEventListener("pagehide",n))}function r(){n(),process.exit(130)}function l(){n(),process.exit(143)}function a(){"undefined"!=typeof process?(i(process,"exit",n),i(process,"SIGINT",r),i(process,"SIGTERM",l)):"undefined"!=typeof window&&(window.addEventListener("beforeunload",n),window.addEventListener("pagehide",n))}class h{split;constructor(t){this.split="string"==typeof t?s(t):[...t]}get fullPath(){return 0===this.split.length?this.separator:this.split.join(this.separator)}createWriteStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),t);throw new Error("File system does not support createWriteStream")}createReadStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),t);throw new Error("File system does not support createReadStream")}createAppendStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),{flags:"a",encoding:t});throw new Error("File system does not support createAppendStream")}createInputStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),{flags:"r",encoding:t});throw new Error("File system does not support createInputStream")}}async function u(t){try{return t().then(()=>!0).catch(()=>!1)}catch(t){return!1}}async function f(t){try{return t().then(t=>t).catch(()=>null)}catch(t){return null}}class c extends h{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTemp"in c.fs))throw new Error("mkdTemp is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtemp(`${i}/${s}-`);return new c(`${n}${e}`)}get fs(){return c.fs}canExecute(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.X_OK??1))}async setExecutable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?493:420))?this:null}canRead(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.R_OK??4))}async setReadable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?420:0))?this:null}canWrite(){return u(()=>this.fs.access(this.fullPath,this.fs?.constants?.W_OK??2))}async setWritable(t=!0){return await u(()=>this.fs.chmod(this.fullPath,t?420:292))?this:null}creationTime(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.birthtime).catch(()=>null))}async setCreationTime(t){return await u(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async lastModified(){return await f(()=>this.fs.stat(this.fullPath).then(t=>t.mtime).catch(()=>null))}async setLastModified(t){return await u(()=>this.fs.utimes(this.fullPath,t,t))?this:null}exists(){return f(()=>this.fs.exists(this.fullPath))}lastAccess(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.atime).catch(()=>null))}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new c(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return c.sep}isDirectory(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.isDirectory()).catch(()=>null))}isFile(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.isFile()).catch(()=>null))}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}isSymbolicLink(){return f(()=>this.fs.lstat(this.fullPath).then(t=>t.isSymbolicLink()).catch(()=>null))}size(){return f(()=>this.fs.stat(this.fullPath).then(t=>t.size).catch(()=>null))}async sizeKB(){const t=await this.size();return null!==t?t/1024:null}async sizeMB(){const t=await this.size();return null!==t?t/1048576:null}async sizeGB(){const t=await this.size();return null!==t?t/1073741824:null}to(...t){return new c(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}async createFile(){return await this.exists()?await this.isFile()?this:null:await this.write("")?this:null}async delete(t,s=t){if(t){if("rmSync"in this.fs)return await u(()=>this.fs.rm(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return await u(()=>this.fs.rmdir(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of await this.listFiles()||[])await s.delete(!0)||(t=!0);if(t)return null}return await u(()=>this.fs.unlink(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}async clear(t){if(t){const t=await this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])await e.delete(!0)||(s=!0);return s?null:this}return await this.write(""),null}async listFiles(){if(!this.isDirectory)return null;const t=await f(()=>this.fs.readdir(this.fullPath));return t?t.map(t=>new c(`${this.fullPath}/${t}`)):null}listFilenames(){return f(()=>this.fs.readdir(this.fullPath))}async mkdir(t=!1){return await u(()=>this.fs.mkdir(this.fullPath,{recursive:t}))?this:null}async mkdirs(){return await this.mkdir(!0)?this:null}async renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||await t.delete(e)?(e&&await(this.parent?.mkdirs()),await u(()=>this.fs.rename(this.fullPath,t.fullPath))?this:null):null}async copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!await t.delete(e))return null;if(this.isFile){const s=await this.read();return null===s?null:await t.write(s)?this:null}if(!await t.mkdir(e))return null;const i=await this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);await r.copyTo(i,s,e)||(n=!0)}return n?null:this}async*walk(){if(this.isDirectory){yield this;for(const t of await this.listFiles()||[])yield*t.walk()}else yield this}read(t){return f(()=>this.fs.readFile(this.fullPath,t))}async readLines(t="utf8"){const s=await this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}async readJSON(){const t=await this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}async readlink(){if(!this.isSymbolicLink)return null;const t=await f(()=>this.fs.readlink(this.fullPath));return t?new c(t):null}async write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),await u(()=>this.fs.writeFile(this.fullPath,t,s))?this:null}async writeJSON(t){return await u(()=>this.write(JSON.stringify(t,null,2)))?this:null}async append(t,s){return await u(()=>this.fs.appendFile(this.fullPath,t,s))?this:null}get sync(){return new d(this.split)}}function o(t){try{return t(),!0}catch(t){return!1}}function p(t){try{return t()}catch(t){return null}}class d extends h{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTempSync"in d.fs))throw new Error("mkdTempSync is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtempSync(`${i}/${s}-`);return new d(`${n}${e}`)}get fs(){return d.fs}get canExecute(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.X_OK??1))}set canExecute(t){o(()=>this.fs.chmodSync(this.fullPath,t?493:420))}get canRead(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.R_OK??4))}set canRead(t){o(()=>this.fs.chmodSync(this.fullPath,t?420:0))}get canWrite(){return o(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.W_OK??2))}set canWrite(t){o(()=>this.fs.chmodSync(this.fullPath,t?420:292))}get creationTime(){return p(()=>this.fs.statSync(this.fullPath).birthtime)}set creationTime(t){o(()=>this.fs.utimesSync(this.fullPath,t,t))}get lastModified(){return p(()=>this.fs.statSync(this.fullPath).mtime)}set lastModified(t){o(()=>this.fs.utimesSync(this.fullPath,t,t))}get exists(){return p(()=>this.fs.existsSync(this.fullPath))}get lastAccess(){return p(()=>this.fs.statSync(this.fullPath).atime)}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new d(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return d.sep}get isDirectory(){return p(()=>this.fs.statSync(this.fullPath).isDirectory())}get isFile(){return p(()=>this.fs.statSync(this.fullPath).isFile())}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}get isSymbolicLink(){return p(()=>this.fs.lstatSync(this.fullPath).isSymbolicLink())}get size(){return p(()=>this.fs.statSync(this.fullPath).size)}get sizeKB(){const t=this.size;return null!==t?t/1024:null}get sizeMB(){const t=this.size;return null!==t?t/1048576:null}get sizeGB(){const t=this.size;return null!==t?t/1073741824:null}to(...t){return new d(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}createFile(){return this.exists?this.isFile?this:null:this.write("")?this:null}delete(t,s=t){if(t){if("rmSync"in this.fs)return o(()=>this.fs.rmSync(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return o(()=>this.fs.rmdirSync(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of this.listFiles()||[])s.delete(!0)||(t=!0);if(t)return null}return o(()=>this.fs.unlinkSync(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}clear(t){if(t){const t=this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])e.delete(!0)||(s=!0);return s?null:this}return this.write(""),null}listFiles(){if(!this.isDirectory)return null;const t=p(()=>this.fs.readdirSync(this.fullPath));return t?t.map(t=>new d(`${this.fullPath}/${t}`)):null}listFilenames(){return p(()=>this.fs.readdirSync(this.fullPath))}mkdir(t=!1){return o(()=>this.fs.mkdirSync(this.fullPath,{recursive:t}))?this:null}mkdirs(){return this.mkdir(!0)?this:null}renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||t.delete(e)?(e&&this.parent?.mkdirs(),o(()=>this.fs.renameSync(this.fullPath,t.fullPath))?this:null):null}copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!t.delete(e))return null;if(this.isFile){const s=this.read();return null===s?null:t.write(s)?this:null}if(!t.mkdir(e))return null;const i=this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);r.copyTo(i,s,e)||(n=!0)}return n?null:this}*walk(){if(this.isDirectory){yield this;for(const t of this.listFiles()||[])yield*t.walk()}else yield this}read(t){return p(()=>this.fs.readFileSync(this.fullPath,t))}readLines(t="utf8"){const s=this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}readJSON(){const t=this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}readlink(){if(!this.isSymbolicLink)return null;const t=p(()=>this.fs.readlinkSync(this.fullPath));return t?new d(t):null}write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),o(()=>this.fs.writeFileSync(this.fullPath,t,s))?this:null}writeJSON(t){return o(()=>this.write(JSON.stringify(t,null,2)))?this:null}append(t,s){return o(()=>this.fs.appendFileSync(this.fullPath,t,s))?this:null}get async(){return new c(this.split)}}function y(t){d.fs=t,c.fs=t.promises}function m(t){return new d(t)}function w(t){return new c(t)}if("undefined"!=typeof process)try{y(await import("fs")),d.sep=c.sep="win32"===process.platform?"\\":"/"}catch{}export{d as File,c as FileAsync,d as FileSync,w as fileAsync,m as fileSync,y as initFS};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ktfile",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "A powerful, cross-platform file system library for JavaScript/TypeScript that provides both synchronous and asynchronous APIs with a clean, object-oriented interface.",
|
|
5
5
|
"main": "index.min.js",
|
|
6
6
|
"types": "types/ktfile.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -5,6 +5,7 @@ export declare function pass(x: () => Promise<unknown>): Promise<boolean>;
|
|
|
5
5
|
export declare function ret<T>(x: () => Promise<T>): Promise<T | null>;
|
|
6
6
|
export declare class FileAsync extends IFile<IAsyncFS> {
|
|
7
7
|
static fs: IAsyncFS;
|
|
8
|
+
static sep: string;
|
|
8
9
|
static createTempFile(directory: FileAsync, prefix?: string, suffix?: string): FileAsync;
|
|
9
10
|
get fs(): IAsyncFS;
|
|
10
11
|
/**
|
|
@@ -536,7 +537,7 @@ export declare class FileAsync extends IFile<IAsyncFS> {
|
|
|
536
537
|
* or as a Buffer if encoding is not specified.
|
|
537
538
|
* If reading fails, it will return null.
|
|
538
539
|
*/
|
|
539
|
-
read(encoding
|
|
540
|
+
read(encoding: BufferEncoding): Promise<string | null>;
|
|
540
541
|
/**
|
|
541
542
|
* @description Reads the contents of the file as a Buffer.
|
|
542
543
|
* If the file is a binary file, it will return the contents as a Buffer.
|
package/types/sync/FileSync.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare function pass(x: () => unknown): boolean;
|
|
|
5
5
|
export declare function ret<T>(x: () => T): T | null;
|
|
6
6
|
export declare class FileSync extends IFile<ISyncFS> {
|
|
7
7
|
static fs: ISyncFS;
|
|
8
|
+
static sep: string;
|
|
8
9
|
static createTempFile(directory: FileSync, prefix?: string, suffix?: string): FileSync;
|
|
9
10
|
get fs(): ISyncFS;
|
|
10
11
|
/**
|
|
@@ -529,7 +530,7 @@ export declare class FileSync extends IFile<ISyncFS> {
|
|
|
529
530
|
* or as a Buffer if encoding is not specified.
|
|
530
531
|
* If reading fails, it will return null.
|
|
531
532
|
*/
|
|
532
|
-
read(encoding
|
|
533
|
+
read(encoding: BufferEncoding): string | null;
|
|
533
534
|
/**
|
|
534
535
|
* @description Reads the contents of the file as a Buffer.
|
|
535
536
|
* If the file is a binary file, it will return the contents as a Buffer.
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: Bug report
|
|
3
|
-
about: Create a report to help us improve
|
|
4
|
-
title: "[BUG]"
|
|
5
|
-
labels: ''
|
|
6
|
-
assignees: OguzhanUmutlu
|
|
7
|
-
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
**Environment:**
|
|
11
|
-
|
|
12
|
-
- OS: [e.g. Windows 10, macOS 12, Ubuntu 20.04]
|
|
13
|
-
- Node.js: [e.g. 16.14.0]
|
|
14
|
-
- KTFile: [e.g. 1.0.0]
|
|
15
|
-
|
|
16
|
-
**Description:**
|
|
17
|
-
Clear description of the bug
|
|
18
|
-
|
|
19
|
-
**Reproduction:**
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
// Minimal code example
|
|
23
|
-
const file = fileSync('./example.txt');
|
|
24
|
-
// Steps to reproduce...
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
**Expected Behavior:**
|
|
28
|
-
What should happen
|
|
29
|
-
|
|
30
|
-
**Actual Behavior:**
|
|
31
|
-
What actually happens
|
|
32
|
-
|
|
33
|
-
**Additional Context:**
|
|
34
|
-
Any other relevant information
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: Feature request
|
|
3
|
-
about: Suggest an idea for this project
|
|
4
|
-
title: "[FEATURE]"
|
|
5
|
-
labels: enhancement
|
|
6
|
-
assignees: OguzhanUmutlu
|
|
7
|
-
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
**Feature Description:**
|
|
11
|
-
Clear description of the proposed feature
|
|
12
|
-
|
|
13
|
-
**Use Case:**
|
|
14
|
-
Why is this feature needed? What problem does it solve?
|
|
15
|
-
|
|
16
|
-
**Proposed API:**
|
|
17
|
-
|
|
18
|
-
```typescript
|
|
19
|
-
// Example of how the feature might work
|
|
20
|
-
file.newMethod(parameters);
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**Alternatives Considered:**
|
|
24
|
-
What other approaches did you consider?
|
|
25
|
-
|
|
26
|
-
**Additional Context:**
|
|
27
|
-
Any other relevant information
|