directory-tree-md
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -6,82 +6,83 @@ 'use strict'; | ||
const constants = { | ||
DIRECTORY: 'directory', | ||
FILE: 'file' | ||
DIRECTORY: 'directory', | ||
FILE: 'file' | ||
} | ||
function safeReadDirSync(path) { | ||
let dirData = {}; | ||
try { | ||
dirData = FS.readdirSync(path); | ||
} catch (ex) { | ||
if (ex.code == "EACCES") | ||
//User does not have permissions, ignore directory | ||
return null; | ||
else throw ex; | ||
} | ||
return dirData; | ||
let dirData = {}; | ||
try { | ||
dirData = FS.readdirSync(path); | ||
} catch (ex) { | ||
if (ex.code == "EACCES") | ||
//User does not have permissions, ignore directory | ||
return null; | ||
else throw ex; | ||
} | ||
return dirData; | ||
} | ||
function strToObj(str) { | ||
const arr = str.split('\n').filter((item) => !!item); | ||
const json = {}; | ||
for (let i = 0; i < arr.length; i += 1) { | ||
const item = arr[i].split(":"); | ||
const key = item[0] ? String.prototype.trim.call(item[0]) : item[0] | ||
const value = item[1] ? String.prototype.trim.call(item[1]) : item[1] | ||
json[key] = value; | ||
} | ||
return json; | ||
const arr = str.split('\n').filter((item) => !!item); | ||
const json = {}; | ||
for (let i = 0; i < arr.length; i += 1) { | ||
const item = arr[i].split(":"); | ||
const key = item[0] ? String.prototype.trim.call(item[0]) : item[0] | ||
const value = item[1] ? String.prototype.trim.call(item[1]) : item[1] | ||
json[key] = value; | ||
} | ||
return json; | ||
} | ||
function directoryTree(path, options, onEachFile) { | ||
const name = PATH.basename(path); | ||
const item = { path, name }; | ||
let stats; | ||
const name = PATH.basename(path); | ||
const item = { path, name }; | ||
let stats; | ||
try { stats = FS.statSync(path); } | ||
catch (e) { return null; } | ||
try { stats = FS.statSync(path); } | ||
catch (e) { return null; } | ||
// Skip if it matches the exclude regex | ||
if (options && options.exclude && options.exclude.test(path)) | ||
return null; | ||
// Skip if it matches the exclude regex | ||
if (options && options.exclude && options.exclude.test(path)) | ||
return null; | ||
if (stats.isFile()) { | ||
if (stats.isFile()) { | ||
const ext = PATH.extname(path).toLowerCase(); | ||
const ext = PATH.extname(path).toLowerCase(); | ||
// Skip if it does not match the extension regex | ||
if (options && options.extensions && !options.extensions.test(ext)) | ||
return null; | ||
// Skip if it does not match the extension regex | ||
if (options && options.extensions && !options.extensions.test(ext)) | ||
return null; | ||
if (options && options.mdconf) { | ||
const contentStr = FS.readFileSync(path).toString(); | ||
if (contentStr) { | ||
const contentMatch = contentStr.match(/^<!--(\s?[^>]*)-->/); | ||
item.mdconf = contentMatch ? strToObj(contentMatch[1]) : {}; | ||
item.isEmpty = contentMatch ? !String.prototype.trim.call(contentStr.replace(contentMatch[0], '')) : true; | ||
} | ||
} | ||
if (options && options.mdconf) { | ||
const contentStr = FS.readFileSync(path).toString(); | ||
if (contentStr) { | ||
const contentMatch = contentStr.match(/^<!--(\s?[^>]*)-->/); | ||
item.relative = item.path.replace(process.cwd(), ''); | ||
item.mdconf = contentMatch ? strToObj(contentMatch[1]) : {}; | ||
item.isEmpty = contentMatch ? !String.prototype.trim.call(contentStr.replace(contentMatch[0], '')) : true; | ||
} | ||
} | ||
item.size = stats.size; // File size in bytes | ||
item.extension = ext; | ||
item.type = constants.FILE; | ||
if (onEachFile) { | ||
onEachFile(item, PATH); | ||
} | ||
} | ||
else if (stats.isDirectory()) { | ||
let dirData = safeReadDirSync(path); | ||
if (dirData === null) return null; | ||
item.size = stats.size; // File size in bytes | ||
item.extension = ext; | ||
item.type = constants.FILE; | ||
if (onEachFile) { | ||
onEachFile(item, PATH); | ||
} | ||
} | ||
else if (stats.isDirectory()) { | ||
let dirData = safeReadDirSync(path); | ||
if (dirData === null) return null; | ||
item.children = dirData | ||
.map(child => directoryTree(PATH.join(path, child), options, onEachFile)) | ||
.filter(e => !!e); | ||
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); | ||
item.type = constants.DIRECTORY; | ||
} else { | ||
return null; // Or set item.size = 0 for devices, FIFO and sockets ? | ||
} | ||
return item; | ||
item.children = dirData | ||
.map(child => directoryTree(PATH.join(path, child), options, onEachFile)) | ||
.filter(e => !!e); | ||
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); | ||
item.type = constants.DIRECTORY; | ||
} else { | ||
return null; // Or set item.size = 0 for devices, FIFO and sockets ? | ||
} | ||
return item; | ||
} | ||
module.exports = directoryTree; |
{ | ||
"name": "directory-tree-md", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "Convert a directory tree to a JS object.", | ||
@@ -5,0 +5,0 @@ "repository": { |
7704
74