Comparing version 0.0.1 to 0.0.2
80
index.js
@@ -5,2 +5,3 @@ const p = require('path') | ||
class Hotfile { | ||
constructor(path){ | ||
@@ -12,79 +13,24 @@ const stats = fs.statSync(path) | ||
this.path = p.resolve(path) | ||
this.size = stats.size | ||
this.name = p.basename(path).replace(p.extname(path), '') | ||
this.basename = p.basename(path) | ||
this.size = stats.size | ||
this.isDirectory ? this.children = [] : this.ext = p.extname(path) | ||
} | ||
filename(name, ext){ | ||
if(ext) this.ext = ext | ||
return [ | ||
name || this.name, | ||
this.ext && this.ext.replace('.','') || null | ||
].filter(e => e != null).join('.') | ||
static async map(path, options = {}){ | ||
const items = await this.readdir(path, options) | ||
for(let i = 0; i < items.length; i++){ | ||
if(items[i].isDirectory) items[i].children = await this.map(items[i].path, options) | ||
} | ||
return items | ||
} | ||
static async mkdir(path){ | ||
return fs.promises.mkdir(path, { recursive: true }) | ||
.then(() => new Hotfile(path)).catch(() => false) | ||
static async readdir(path, options = {}){ | ||
const { model, exclude } = options | ||
let items = await fs.promises.readdir(path) | ||
if(exclude) items = items.filter(item => !(exclude).test(item)) | ||
return items.map(i => model ? new model(p.resolve(path,i)) : new Hotfile(p.resolve(path,i))) | ||
} | ||
static async readdir(path){ | ||
const items = await fs.promises.readdir(path) | ||
return items.filter(item => !(/(^|\/)\.[^\/\.]/g).test(item)) | ||
.map(i => new Hotfile(p.resolve(path,i))) | ||
} | ||
static async map(path){ | ||
const items = await this.readdir(path) | ||
for(let i = 0; i < items.length; i++){ | ||
if(items[i].isDirectory) items[i].children = await this.map(items[i].path) | ||
} | ||
return items | ||
} | ||
async rename(name, ext){ | ||
if(ext) this.ext = ext | ||
const toPath = this.parent + '/' + this.filename(name) | ||
return await this.move(toPath) | ||
} | ||
async moveTo(toPath){ | ||
if(toPath instanceof Hotfile && toPath.isDirectory) toPath = toPath.path | ||
toPath = toPath + '/' + this.filename() | ||
return fs.promises.rename(this.path, toPath).then(async () => { | ||
await Object.assign(this, new Hotfile(toPath)) | ||
return true | ||
}).catch(() => false) | ||
} | ||
async move(toPath){ | ||
return fs.promises.rename(this.path, toPath).then(async () => { | ||
await Object.assign(this, new Hotfile(toPath)) | ||
return true | ||
}).catch((err) => { | ||
console.log(err) | ||
return false | ||
}) | ||
} | ||
async appendDirectory(name){ | ||
if(this.isFile) throw('append only works on directories') | ||
let path = this.path + '/' + name | ||
return fs.promises.mkdir(path, { recursive: true }) | ||
.then(() => { | ||
const dir = new Hotfile(path) | ||
this.children.push(dir) | ||
return dir | ||
}).catch(() => false) | ||
} | ||
async delete(){ | ||
return fs.promises.unlink(this.path).then(async () => { | ||
await Object.assign(this,{parent: null, path: null, name: null, metadata: null}) | ||
return true | ||
}).catch(() => false) | ||
} | ||
} | ||
module.exports = Hotfile |
{ | ||
"name": "hotfile", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
108
README.md
@@ -6,7 +6,7 @@ # hotfile | ||
const file = '/some/file/path.ext' | ||
const hf = require('hotfile') | ||
const HF = require('hotfile') | ||
const someAsyncFunction = async () => { | ||
const hotfiles = await hf.map(directory) | ||
const hotfile = new hf(file) | ||
const hotfiles = await HF.map(directory) | ||
const hotfile = new HF(file) | ||
const hotdir = hotfiles[0] | ||
@@ -18,7 +18,105 @@ const newdir = await hotdir.appendDirectory('subtitles') | ||
```js | ||
const hotdir = new hf(directory) | ||
const hotdir = new HF(directory) | ||
await hotdir.rename('newfilename', 'ext') | ||
const hotfile = new hf(file) | ||
const hotfile = new HF(file) | ||
await hotfile.moveTo(hotdir) | ||
``` | ||
### Exclude all Hidden Files | ||
```js | ||
const items = await hotfile.map(someFolderPath, { | ||
exclude: /(^|\/)\.[^\/\.]/g, | ||
model: SomeModelThatInheritsFromHotfile | ||
}) | ||
``` | ||
### Create a Hotfile Wrapper Class | ||
Note that all wrapper class objects must inherit from the Hotfile class. | ||
```js | ||
const HF = require('hotfile') | ||
class SomeName extends HF { | ||
constructor(path){ | ||
super(path) | ||
// write your code here ... | ||
} | ||
} | ||
``` | ||
### Some Usefull Methods you might want to use in your wrapper class | ||
```js | ||
const HF = require('hotfile') | ||
class SomeName extends HF { | ||
constructor(path){ | ||
super(path) | ||
// write your code here ... | ||
} | ||
filename(name, ext){ | ||
if(ext) this.ext = ext | ||
return [ | ||
name || this.name, | ||
this.ext && this.ext.replace('.','') || null | ||
].filter(e => e != null).join('.') | ||
} | ||
static async map(path, options = {}){ | ||
const items = await this.readdir(path, options) | ||
for(let i = 0; i < items.length; i++){ | ||
if(items[i].isDirectory) items[i].children = await this.map(items[i].path, options) | ||
} | ||
return items | ||
} | ||
static async mkdir(path){ | ||
return fs.promises.mkdir(path, { recursive: true }) | ||
.then(() => new Hotfile(path)).catch(() => false) | ||
} | ||
async rename(name, ext){ | ||
if(ext) this.ext = ext | ||
const toPath = this.parent + '/' + this.filename(name) | ||
return await this.move(toPath) | ||
} | ||
async moveTo(toPath){ | ||
if(toPath instanceof Hotfile && toPath.isDirectory) toPath = toPath.path | ||
toPath = toPath + '/' + this.filename() | ||
return fs.promises.rename(this.path, toPath).then(async () => { | ||
await Object.assign(this, new Hotfile(toPath)) | ||
return true | ||
}).catch(() => false) | ||
} | ||
async move(toPath){ | ||
return fs.promises.rename(this.path, toPath).then(async () => { | ||
await Object.assign(this, new Hotfile(toPath)) | ||
return true | ||
}).catch((err) => { | ||
console.log(err) | ||
return false | ||
}) | ||
} | ||
async appendDirectory(name){ | ||
if(this.isFile) throw('append only works on directories') | ||
let path = this.path + '/' + name | ||
return fs.promises.mkdir(path, { recursive: true }) | ||
.then(() => { | ||
const dir = new Hotfile(path) | ||
this.children.push(dir) | ||
return dir | ||
}).catch(() => false) | ||
} | ||
async delete(){ | ||
return fs.promises.unlink(this.path).then(async () => { | ||
await Object.assign(this,{parent: null, path: null, name: null, metadata: null}) | ||
return true | ||
}).catch(() => false) | ||
} | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4826
120
28