fs-assistant
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -1,2 +0,2 @@ | ||
"use strict";var e=require("fs"),i=require("path");var t=new class{readDir(i){return new Promise((t,r)=>{e.readdir(i,(e,i)=>{e&&r(e),t(i)})})}readFile(i){return new Promise((t,r)=>{e.readFile(i,"utf-8",(e,i)=>e?r(e):t(i))})}writeFile(i,t){return new Promise((r,a)=>{e.writeFile(i,t,e=>e?a(e):r())})}async copyDir(t,r){e.existsSync(i.resolve(r))||await this.makeDir(i.resolve(r));const a=async(t,r)=>{const s=(await this.readDir(t)).map(async s=>{const n=i.resolve(t,s),o=i.resolve(r,s);e.lstatSync(n).isDirectory()?(e.existsSync(o)||await this.makeDir(o),await a(n,o)):await this.copyFile(n,o)});await Promise.all(s)};await a(t,i.resolve(r))}async makeDir(i){return new Promise((t,r)=>{e.mkdir(i,e=>{e&&r(e),t()})})}async copyFile(i,t){return new Promise((r,a)=>{e.copyFile(i,t,e=>{if(e)return a(e);r()})})}async deleteFile(i){return new Promise((t,r)=>{e.unlink(i,e=>{e&&r(e),t()})})}stat(i){return new Promise((t,r)=>{e.stat(i,(e,i)=>{e&&r(e),t(i)})})}async renameFile(e,t){const r=i.join(i.dirname(e),t);await this.copyFile(e,r),await this.deleteFile(e)}isPath(e){return!!/^\.{1,2}$/.test(e)||!/^[\\\/]+$/.test(e)&&(!/\.{3,}/.test(e)&&(!!/^[a-zA-Z1-9 %&]+(\.[a-zA-Z1-9]+)*$/.test(e)||/^([.A-Za-z1-9% ]*[\\\/]){1,}([a-zA-Z 1-9%&]+(\.[a-zA-Z1-9]+)*)?$/.test(e)))}async getAllFilesInDir(e){return(await this.getItemsInDir(e)).filter(e=>"File"===e.type)}async getDirsInDir(e){return(await this.getItemsInDir(e)).filter(e=>"Directory"===e.type).map(e=>({name:e.name,files:e.files||[],location:e.location}))}async getItemsInDir(t){const r=[],a=async t=>{const s=(await this.readDir(t)).map(async s=>{const n=i.resolve(t,s);if(e.lstatSync(n).isDirectory())r.push({type:"Directory",name:s,location:n,files:[]}),await a(n);else{const e={name:s,location:n,type:"File"};r.push(e);const t=r.find(e=>e.location===i.dirname(n));t&&t.files&&t.files.push({name:s,location:n})}});await Promise.all(s)};return await a(t),r}async flattenDir(t,r){const a=await this.getAllFilesInDir(t);e.existsSync(r)||await this.makeDir(r);const s=a.map(e=>this.copyFile(e.location,i.resolve(r,e.name)));await Promise.all(s)}async getFileSizeInBytes(e){return(await this.stat(e)).size}async getDirSizeInBytes(e){const i=await this.getAllFilesInDir(e);return(await Promise.all(i.map(e=>this.getFileSizeInBytes(e.location)))).reduce((e,i)=>e+i,0)}};var r=new class{constructor(){this.replaceStringInFile=async(e,i,r)=>{if(!e)throw new Error("The file location has to be specified");if(!i)throw new Error("The string pattern has to be specified");r=r||"";const a=(await t.readFile(e)).replace(i,r);await t.writeFile(e,a)},this.copyDir=async(e,i)=>{if(!e)throw new Error("Path to the source directory must be specified");if(!i)throw new Error("Path to the output directory must be specified");await t.copyDir(e,i)},this.readFile=async e=>{if(!e)throw new Error("The file location must be specified");return await t.readFile(e)},this.writeFile=async(e,i)=>{if(!e)throw new Error("The new file location must be specified");await t.writeFile(e,i)},this.copyFile=async(e,i)=>{if(!e)throw new Error("The source file location must be specified");if(!i)throw new Error("The output file location must be specified");await t.copyFile(e,i)},this.makeDir=async e=>{if(!e)throw new Error(`The location ${e} is not valid.`);await t.makeDir(e)},this.deleteFile=async e=>{if(!e)throw new Error(`The location ${e} is not valid.`);await t.deleteFile(e)},this.renameFile=async(e,i)=>{if(!e)throw new Error("The file is missing");if(!i)throw new Error("The new file name is missing");await t.renameFile(e,i)},this.isPath=e=>"string"==typeof e&&t.isPath(e),this.getFilesInDir=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getAllFilesInDir(e)},this.flattenDir=async(e,i)=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);if("string"!=typeof i)throw new Error(`The provided path"${i}" is invalid`);await t.flattenDir(e,i)},this.readDir=async e=>{if("string"!=typeof e)throw new Error(`The ${e} argument should be a string`);return t.readDir(e)},this.getDirsInDir=async e=>{if("string"!=typeof e)throw new Error("The dir argument must be string not "+typeof e);return await t.getDirsInDir(e)},this.getItemsInDir=async e=>{if("string"!=typeof e)throw new Error("The dir argument must be string not "+typeof e);return await t.getItemsInDir(e)},this.getFileSizeInBytes=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getFileSizeInBytes(e)},this.getDirSizeInBytes=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getDirSizeInBytes(e)}}};r.default=r,module.exports=r; | ||
"use strict";var e=require("fs"),i=require("path");var t=new class{readDir(i){return new Promise((t,r)=>{e.readdir(i,(e,i)=>e?r(e):t(i))})}readFile(i){return new Promise((t,r)=>{e.readFile(i,"utf-8",(e,i)=>e?r(e):t(i))})}writeFile(i,t){return new Promise((r,s)=>{e.writeFile(i,t,e=>e?s(e):r())})}async copyDir(t,r){e.existsSync(i.resolve(r))||await this.makeDir(i.resolve(r));const s=async(t,r)=>{const a=(await this.readDir(t)).map(async a=>{const n=i.resolve(t,a),o=i.resolve(r,a);e.lstatSync(n).isDirectory()?(e.existsSync(o)||await this.makeDir(o),await s(n,o)):await this.copyFile(n,o)});await Promise.all(a)};await s(t,i.resolve(r))}async makeDir(i){return new Promise((t,r)=>{e.mkdir(i,e=>e?r(e):t())})}async copyFile(i,t){return new Promise((r,s)=>{e.copyFile(i,t,e=>e?s(e):r())})}async deleteFile(i){return new Promise((t,r)=>{e.unlink(i,e=>e?r(e):t())})}stat(i){return new Promise((t,r)=>{e.stat(i,(e,i)=>e?r(e):t(i))})}rmDir(i){return new Promise((t,r)=>{e.rmdir(i,e=>e?r(e):t())})}async renameFile(e,t){const r=i.join(i.dirname(e),t);await this.copyFile(e,r),await this.deleteFile(e)}isPath(e){return!!/^\.{1,2}$/.test(e)||!/^[\\\/]+$/.test(e)&&(!/\.{3,}/.test(e)&&(!!/^[a-zA-Z1-9 %&]+(\.[a-zA-Z1-9]+)*$/.test(e)||/^([.A-Za-z1-9% ]*[\\\/]){1,}([a-zA-Z 1-9%&]+(\.[a-zA-Z1-9]+)*)?$/.test(e)))}async getAllFilesInDir(e){return(await this.getItemsInDir(e)).filter(e=>"File"===e.type)}async getDirsInDir(e){return(await this.getItemsInDir(e)).filter(e=>"Directory"===e.type).map(e=>({name:e.name,files:e.files||[],location:e.location}))}async getItemsInDir(t){const r=[],s=async t=>{const a=(await this.readDir(t)).map(async a=>{const n=i.resolve(t,a);if(e.lstatSync(n).isDirectory())r.push({type:"Directory",name:a,location:n,files:[]}),await s(n);else{const e={name:a,location:n,type:"File"};r.push(e);const t=r.find(e=>e.location===i.dirname(n));t&&t.files&&t.files.push({name:a,location:n})}});await Promise.all(a)};return await s(t),r}async flattenDir(t,r){const s=await this.getAllFilesInDir(t);e.existsSync(r)||await this.makeDir(r);const a=s.map(e=>this.copyFile(e.location,i.resolve(r,e.name)));await Promise.all(a)}async getFileSizeInBytes(e){return(await this.stat(e)).size}async getDirSizeInBytes(e){const i=await this.getAllFilesInDir(e);return(await Promise.all(i.map(e=>this.getFileSizeInBytes(e.location)))).reduce((e,i)=>e+i,0)}async delDir(e){if(!(await this.stat(e)).isDirectory())throw new Error(`delDir expects a directory and the provided location ${e} isn't one`);const i=await this.getAllFilesInDir(e);await Promise.all(i.map(e=>this.deleteFile(e.location))),await this.delDirCore(e)}existsSync(i){return e.existsSync(i)}async delDirCore(e){if(!this.existsSync(e))return;const i=await this.getDirsInDir(e);return await i.reduce((e,i)=>e.then(()=>this.delDirCore(i.location)),Promise.resolve()),this.rmDir(e)}};var r=new class{constructor(){this.replaceStringInFile=async(e,i,r)=>{if(!e)throw new Error("The file location has to be specified");if(!i)throw new Error("The string pattern has to be specified");r=r||"";const s=(await t.readFile(e)).replace(i,r);await t.writeFile(e,s)},this.copyDir=async(e,i)=>{if(!e)throw new Error("Path to the source directory must be specified");if(!i)throw new Error("Path to the output directory must be specified");await t.copyDir(e,i)},this.readFile=async e=>{if(!e)throw new Error("The file location must be specified");return await t.readFile(e)},this.writeFile=async(e,i)=>{if(!e)throw new Error("The new file location must be specified");await t.writeFile(e,i)},this.copyFile=async(e,i)=>{if(!e)throw new Error("The source file location must be specified");if(!i)throw new Error("The output file location must be specified");await t.copyFile(e,i)},this.makeDir=async e=>{if(!e)throw new Error(`The location ${e} is not valid.`);await t.makeDir(e)},this.deleteFile=async e=>{if(!e)throw new Error(`The location ${e} is not valid.`);await t.deleteFile(e)},this.renameFile=async(e,i)=>{if(!e)throw new Error("The file is missing");if(!i)throw new Error("The new file name is missing");await t.renameFile(e,i)},this.isPath=e=>"string"==typeof e&&t.isPath(e),this.getFilesInDir=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getAllFilesInDir(e)},this.flattenDir=async(e,i)=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);if("string"!=typeof i)throw new Error(`The provided path"${i}" is invalid`);await t.flattenDir(e,i)},this.readDir=async e=>{if("string"!=typeof e)throw new Error(`The ${e} argument should be a string`);return t.readDir(e)},this.getDirsInDir=async e=>{if("string"!=typeof e)throw new Error("The dir argument must be string not "+typeof e);return await t.getDirsInDir(e)},this.getItemsInDir=async e=>{if("string"!=typeof e)throw new Error("The dir argument must be string not "+typeof e);return await t.getItemsInDir(e)},this.getFileSizeInBytes=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getFileSizeInBytes(e)},this.getDirSizeInBytes=async e=>{if("string"!=typeof e)throw new Error(`The provided path"${e}" is invalid`);return await t.getDirSizeInBytes(e)},this.delDir=async e=>{if("string"!=typeof e)throw new Error("The location argument must be string not "+typeof e);return await t.delDir(e)},this.existsSync=e=>{if("string"!=typeof e)throw new Error("The location argument must be string not "+typeof e);return t.existsSync(e)}}};r.default=r,module.exports=r; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "fs-assistant", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Making annoying fs operations easier", | ||
@@ -12,3 +12,3 @@ "main": "dist/index.js", | ||
"test:integration": "tsc -p ./tsconfig.test.json && mocha test/**/*.spec.js", | ||
"prepare": "npm run build && npm run lint && npm run test" | ||
"prepare": "npm run build && npm run test" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": { |
@@ -81,2 +81,22 @@ # fs-assistant | ||
``` | ||
Returns an array with all items represented either as FileDetails or as DirectoryDetails. | ||
Returns an array with all items represented either as FileDetails or as DirectoryDetails. | ||
```javascript | ||
getFileSizeInBytes: (location: string) => Promise<number>; | ||
``` | ||
Returns the file size in bytes. | ||
```javascript | ||
getDirSizeInBytes: (location: string) => Promise<number>; | ||
``` | ||
Returns the directory size in bytes. | ||
```javascript | ||
delDir: (location: string) => Promise<void>; | ||
``` | ||
Deletes the directory recursively. | ||
```javascript | ||
existsSync: (location: string) => boolean; | ||
``` | ||
Equivalent to `fs.existsSync()`. |
Sorry, the diff of this file is not supported yet
34596
55
102