Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hotfile

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hotfile - npm Package Compare versions

Comparing version 0.2.5 to 0.2.6

228

index.js
const fs = require('fs')
const p = require('path')
const crypto = require('crypto')
const { resolve, parse } = require('path')
class HotfileError extends Error {}
class Hotfile {
constructor(path){
if(p.extname(path) == "" && !this.existsSync(path)) this.mkdirSync(path)
const stat = fs.statSync(path)
this.isFile = stat.isFile()
this.path = path
this.basename = p.basename(path)
constructor(path, options = {}) {
const stat = fs.lstatSync(path)
const { ext, name, base } = parse(path)
this.isDirectory = stat.isDirectory()
this.path = resolve(path)
this.name = name
this.base = base
this.size = stat.size
stat.isDirectory() ? this.children = [] : this.ext = p.extname(path)
this.isDirectory ? this.children = [] : this.ext = ext
if(options.stat) this.stat = stat
}
/* COMPUTED PROPERTIES */
get name(){
return this.ext ? this.basename.replace(this.ext, '') : this.basename
}
get foldername(){
return this.basename
}
get lowername(){
return this.name.toLowerCase()
}
get lowerbasename(){
return this.basename.toLowerCase()
}
get parentPath(){
return p.dirname(this.path)
}
id(){
return this.md5Id(this.path)
}
/* GENERAL FUNCTIONS */
md5Id(string){
return crypto.createHash('md5').update(string).digest('hex');
}
static mkdirSync(path, options = { recursive: true }){

@@ -71,23 +38,3 @@ let flag = true

mkdirSync(path, options = { recursive: true }){
let flag = true
try{
fs.mkdirSync(path, options)
}catch(e){
flag = false
}
return flag
}
existsSync(path){
let flag = true
try{
fs.accessSync(path, fs.constants.F_OK)
}catch(e){
flag = false
}
return flag
}
async exists(path){
static async exists(path){
try {

@@ -101,3 +48,3 @@ await fs.promises.access(path, fs.constants.F_OK)

async mkdir(path, options = {}){
static async mkdir(path, options = {}){
const { recursive, force } = options

@@ -114,127 +61,62 @@ if(!force && await this.exists(path)) return false

async rename(from, to){
try {
await fs.promises.rename(from, to)
this.path = to
} catch (err) {
throw(err)
}
}
async loadChildren(options = {}, currentDepth) {
this.children = []
typeof currentDepth === 'number' ? currentDepth++ : currentDepth = 1
const items = await fs.promises.readdir(this.path)
for (let i = 0; i < items.length; i++) {
const path = resolve(this.path, items[i])
const hotfile = new Hotfile(path, options.options ? options.options: {})
if(typeof options.filter === 'function' && ! await options.filter(hotfile)) continue
if (typeof options.cb === 'function') await options.cb(hotfile)
this.children.push(hotfile)
async delete(){
try {
await fs.promises.unlink(this.path)
} catch (err) {
console.error('there was an error:', err.message)
if (hotfile.isDirectory) {
if ((typeof options.depth === 'number') && !(options.depth >= currentDepth)) continue
await hotfile.loadChildren(options, currentDepth)
}
}
return this
}
/* FILE FUNCTIONS */
setNameTo(name){
if(!name) throw(new Error(`setNameTo expects a string, ${name} provided`))
this.basename = name + this.ext
return this
async createChildDirectory(directoryName){
const newPath = resolve(this.path, directoryName)
await Hotfile.mkdir(newPath)
const hotfolder = new Hotfile(newPath)
this.children.push(hotfolder)
return hotfolder
}
appendToBasename(text = null){
if(text == null) throw(new Error(`setLangTo expects a string, ${text} provided`))
const basename = this.name.concat(text)
this.basename = this.basename.replace(this.name, basename)
return this
async delete(){
await fs.promises.unlink(this.path)
return null
}
setExtTo(ext = null){
if(ext == null) throw(new Error(`setExtTo expects a string, ${ext} provided`))
const _ext = '.' + ext.replace(/\.*/g,'')
this.basename = this.basename.replace(this.ext, _ext)
this.ext = _ext
updatePath(newPath){
this.path = newPath
const { name, base } = parse(newPath)
Object.assign(this, { name, base })
return this
}
setBasenameTo(name = null){
if(!name) throw(new Error(`setBasenameTo expects a string, ${name} provided`))
this.basename = name
this.ext = p.extname(name)
async rename(newbase){
const oldPath = this.path
const newPath = this.path.replace(this.base, newbase)
this.updatePath(newPath)
await fs.promises.rename(oldPath, newPath)
return this
}
async moveTo(instance = null, options = {}){
const { force } = options
if(instance && !(instance instanceof Hotfile))
throw(new Error(`moveTo: expects Hotfile instace or null, ${instance} provided`))
let destDir = this.parent
if(instance) destDir = instance.isFile ? instance.parent : instance.path
const to = p.join(destDir, this.basename)
if(force || !await this.exists(to)) await this.rename(this.path, to)
return new Hotfile(to)
}
/* FOLDER FUNCTIONS */
createFolderSync(name, options = { recursive: true }){
const path = p.join(this.path, name)
this.mkdirSync(path, options)
if(!options.force && this.existsSync(path)) return new Hotfile(path)
const folder = new Hotfile(path)
this.children.push(folder)
return folder
}
async createFolder(name, options = { recursive: true }){
const path = p.join(this.path, name)
await this.mkdir(path, options)
if(!options.force && await this.exists(path)) return new Hotfile(path)
const folder = new Hotfile(path)
this.children.push(folder)
return folder
}
async loadChildren(options = {}){
const { depth } = options
this.children = await this.loaddir(this.path, depth, options)
async moveTo(destinationPath){
const hotfolder = destinationPath instanceof Hotfile
? destinationPath
: new Hotfile(destinationPath)
if(!hotfolder.isDirectory) throw(new Error("destination must be a folder, file provided"))
const oldPath = this.path
const newPath = resolve(hotfolder.path, this.base)
this.updatePath(newPath)
await fs.promises.rename(oldPath, newPath)
return this
}
async loaddir(path, depth = 0, options = {}){
const { id, cb, files, exclude, include, $include, $exclude, allow } = options
let items = await fs.promises.readdir(path)
/* FILTERS */
if(exclude) items = items.filter(o => !exclude.includes(o))
if(include) items = items.filter(o => include.includes(o))
if($exclude) items = items.filter(o => !$exclude.find(regex => (regex).test(o)))
if($include) items = items.filter(o => $include.find(regex => (regex).test(o)))
items = items.map(o => new Hotfile(p.join(path, o)))
for(let i = 0; i < items.length; i++){
/* options */
if(cb) await cb(items[i])
if(id) items[i].id = this.md5Id(items[i].path)
if(files && items[i].isFile) {
if(!this.files) this.files = []
if(allow){
const ext = items[i].ext.toLowerCase()
if(allow.includes(ext)) this.files.push(items[i])
}else{
this.files.push(items[i])
}
}
if(!items[i].isFile && depth > 0){
items[i].children = await this.loaddir(items[i].path, depth - 1, options)
}
}
return items
}
}
exports = module.exports = (path = null) => {
if(!path) throw(new HotfileError(`hotfile requires a directory path, ${path} provided`))
return new Hotfile(path)
}
exports.Hotfile = Hotfile
exports.HotfileError = HotfileError
module.exports = Hotfile
{
"name": "hotfile",
"version": "0.2.5",
"version": "0.2.6",
"description": "Hotfile makes working with folders and files in node-js easy and clear.",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc