-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeTableOfContents.js
86 lines (76 loc) · 2.87 KB
/
makeTableOfContents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* makeTableOfContents command-line task
* @description Generate a TOC
* @module makeTableOfContents
* @author Tom Söderlund
*/
const fs = require('fs')
const path = require('path')
const AVOID_LIST = ['.git', '.DS_Store', 'makeTableOfContents.js', 'LICENSE', 'README.md', 'Sublime snippets']
const ROOT_FOLDER = '/Users/tomsoderlund/Documents/Guidebooks/'
const includesSomeString = (longString, stringArray) => stringArray.reduce((result, smallString) => result || longString.includes(smallString), false)
const scanFolder = function (dir, done) {
let results = []
fs.readdir(dir, function (err, list) {
if (err) return done(err)
let i = 0;
(function next () {
let file = list[i++]
if (!file) return done(null, results)
file = path.resolve(dir, file)
fs.stat(file, function (err, stat) {
if (stat && !includesSomeString(file, AVOID_LIST)) {
if (stat.isDirectory()) {
scanFolder(file, function (err, res) {
results = results.concat(res)
next()
})
} else {
results.push(file)
next()
}
} else {
next()
}
})
})()
})
}
function set (object, keys, val) {
keys = Array.isArray(keys) ? keys : keys.split('.')
if (keys.length > 1) {
object[keys[0]] = object[keys[0]] || {}
return set(object[keys[0]], keys.slice(1), val)
}
object[keys[0]] = val
}
const pad = (count, str = ' ') => Array(count).fill(str).join('')
const formatFileTree = (fileTree, level = 0) => {
return Object.keys(fileTree).map(fileName => {
const fileValue = fileTree[fileName]
const isFolder = typeof fileValue === 'object'
const niceName = fileName.replace('.md', '')
return isFolder
? pad(level * 2) + `- ${niceName}:\n` + formatFileTree(fileValue, level + 1)
: pad(level * 2) + `- [${niceName}](./${fileValue.replace(/ /g, '%20')})`
}).join('\n')
}
async function makeTableOfContents ({ folderName = '.' } = {}) {
scanFolder(folderName, (err, fileNames) => {
const relativeFiles = fileNames.map(fileName => fileName.replace(ROOT_FOLDER, ''))
const fileTree = relativeFiles.reduce((result, filePath) => {
let newResult = { ...result }
set(newResult, filePath.split('/'), filePath)
return newResult
}, {})
console.log(formatFileTree(fileTree))
})
}
const ARGUMENTS = []
if ((process.argv.length - 2) < ARGUMENTS.length) {
console.log('Usage: node tasks/makeTableOfContents ' + ARGUMENTS.map(str => `[${str.split(':')[0]}]`).join(' '))
console.log(' E.g: node tasks/makeTableOfContents ' + ARGUMENTS.map(str => str.split(':')[1] || '“something”').join(' '))
} else {
const argumentObj = process.argv.slice(2).reduce((result, value, index) => ({ ...result, [ARGUMENTS[index] ? ARGUMENTS[index].split(':')[0] : 'arg']: value }), {})
makeTableOfContents(argumentObj)
}