-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const emoji = { | ||
directory: '📁', | ||
file: '📄', | ||
} | ||
const characters = { | ||
border: '|', | ||
contain: '├', | ||
line: '─', | ||
last: '└' | ||
} | ||
|
||
module.exports = { | ||
emoji, | ||
characters, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { emoji, characters } = require('./config') | ||
const { isNumber } = require('./utils') | ||
|
||
function generate(data, options = {}, deep = 0) { | ||
if (isNumber(options.level) && deep >= options.level) { | ||
return '' | ||
} | ||
let output = '' | ||
|
||
data.forEach((item, index) => { | ||
const borderPrefix = (characters.border + ' '.repeat(3)).repeat(deep) | ||
let contentPrefix = index === data.length - 1 ? characters.last : characters.contain | ||
contentPrefix += characters.line.repeat(2) | ||
const content = options.icon ? `${emoji[item.type]}${item.name}` : `${item.name}` | ||
|
||
let currentLineStr = `${borderPrefix}${contentPrefix}${content}` | ||
currentLineStr += '\n' | ||
output += currentLineStr | ||
|
||
if (item.children) { // directory | ||
output += generate(item.children, options, deep + 1) | ||
} | ||
}) | ||
|
||
return output | ||
} | ||
|
||
module.exports = generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const fs = require('fs') | ||
const { isDirectory } = require('./utils') | ||
|
||
// The difference between statSync and lstatSync | ||
// https://stackoverflow.com/questions/32478698/what-is-the-different-between-stat-fstat-and-lstat-functions-in-node-js | ||
|
||
function toTree(path, options = {}) { | ||
if (isDirectory(path)) { | ||
let dir = fs.readdirSync(path) | ||
const ignoreList = options.ignore || [] | ||
|
||
if (ignoreList.length) { | ||
dir = dir.filter(child => !ignoreList.includes(child)) | ||
} | ||
if (options.onlyFolder) { | ||
dir = dir.filter(child => isDirectory(`${path}/${child}`)) | ||
} | ||
const children = dir.map(child => toTree(`${path}/${child}`)) | ||
const dirName = path.split('/').pop() | ||
|
||
return { | ||
type: 'directory', | ||
name: dirName, | ||
children, | ||
} | ||
} else { | ||
return { | ||
type: 'file', | ||
name: path.split('/').pop(), | ||
} | ||
} | ||
} | ||
|
||
module.exports = toTree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters