list-contents
Advanced tools
Comparing version 2.0.3 to 2.0.4
92
index.js
@@ -1,8 +0,8 @@ | ||
/* global Promise */ | ||
const fs = require('fs-extra'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const args = require('typeof-arguments'); | ||
const cliColor = require('cli-color'); | ||
const moveOn = require('move-on'); | ||
const error = cliColor.red; | ||
const warn = cliColor.bgYellow.black; | ||
const warn = cliColor.bgYellow.blue; | ||
@@ -14,60 +14,48 @@ module.exports = function(getPath,callback){ | ||
}); | ||
var msgError = new Error(warn('list-contents') + ': ' + error('Could not get the access to the contents of the given directory path.')); | ||
var dirs = []; | ||
var files = []; | ||
var userContext = {dirs:[], files:[], path:getPath, error:null }; | ||
run(getPath) | ||
.then(()=>callback({error:null,files:files,dirs:dirs,path:getPath})) | ||
.catch((err)=>callback({error:err,files:files,dirs:dirs,path:getPath})); | ||
moveOn([explore],userContext,callback,(u,e)=>{ | ||
callback({dirs:[], files:[], path:getPath, error:e }); | ||
}); | ||
function run(pth){ | ||
return new Promise((resolve,reject)=>{ | ||
getContent(pth) | ||
.then((o)=>getDirsAndFiles(o)) | ||
.then(()=>resolve()) | ||
.catch((err)=>reject(err)); | ||
}); | ||
} | ||
function getContent(pth){ | ||
return new Promise((resolve,reject)=>{ | ||
fs.readdir(pth,(err,content)=>{ | ||
if(err) { | ||
reject(msgError); | ||
} else { | ||
resolve({pth:pth,content:content}); | ||
function explore(resolve,reject,r){ | ||
var relative = typeof r==='undefined' ? '':r; | ||
var abs = path.resolve(this.path,relative); | ||
fs.readdir(abs,(err,contents)=>{ | ||
if(err) reject(new Error(warn('list-contents') + ': ' + error(`Could not get the access to the '${parseSlashes(abs)}' path.`))); | ||
if(!err){ | ||
var contentsIter = 0; | ||
if(!contents.length) resolve(); | ||
for(let i in contents){ | ||
checkItem.call(this,relative,contents[i],(isDir,relative)=>{ | ||
if(isDir) explore.call(this,iter,reject,relative); | ||
if(!isDir) iter(); | ||
}); | ||
} | ||
}); | ||
} | ||
function iter(){ | ||
if(++contentsIter===contents.length) resolve(); | ||
} | ||
}); | ||
} | ||
function getDirsAndFiles(o){ | ||
return new Promise((resolve,reject)=>{ | ||
var promises = []; | ||
for(let i in o.content){ | ||
let p = path.resolve(o.pth,o.content[i]); | ||
let rel = path.relative(getPath,p); | ||
var promise = new Promise((resolve,reject)=>{ | ||
fs.lstat(p,(err,stats)=>{ | ||
if(err){ | ||
reject(msgError); | ||
return; | ||
} else { | ||
if(stats.isDirectory()){ | ||
dirs.push(rel); | ||
run(p).then(()=>{ | ||
resolve(); | ||
}); | ||
} else { | ||
files.push(rel); | ||
resolve(); | ||
} | ||
} | ||
}); | ||
}); | ||
promises.push(promise); | ||
function checkItem(r,item,resolve,reject){ | ||
var absolute = path.resolve(this.path,r,item); | ||
var relative = path.join(r,item); | ||
fs.stat(absolute,(err,stats)=>{ | ||
if(err) reject(new Error(warn('list-contents') + ': ' + error(`Could not get the access to the '${parseSlashes(absolute)}' path.`))); | ||
if(!err){ | ||
var exists = err === null, | ||
isFile = stats && stats.isFile(), | ||
isDir = stats && stats.isDirectory(); | ||
if(exists && isFile) this.files.push(parseSlashes(relative)); | ||
if(exists && isDir) this.dirs.push(parseSlashes(relative)); | ||
resolve(isDir,relative); | ||
} | ||
Promise.all(promises).then(()=>resolve(),(err)=>reject(err)); | ||
}); | ||
} | ||
function parseSlashes(path){ | ||
return path.replace(/\\/g,'/'); | ||
} | ||
}; |
{ | ||
"name": "list-contents", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Returns a list of paths to the subfolders and subfiles of the specified location.", | ||
@@ -22,3 +22,3 @@ "main": "index.js", | ||
"cli-color": "^1.2.0", | ||
"fs-extra": "^4.0.1", | ||
"move-on": "^1.0.0", | ||
"typeof-arguments": "^3.0.1" | ||
@@ -25,0 +25,0 @@ }, |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6349
5
54
+ Addedmove-on@^1.0.0
+ Addedmove-on@1.1.2(transitive)
- Removedfs-extra@^4.0.1
- Removedfs-extra@4.0.3(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removeduniversalify@0.1.2(transitive)