Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used module for file matching #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"underscore": "~1.5.1",
"marked": "~0.3.2",
"special-html": "0.0.1"
"special-html": "0.0.1",
"glob": "~3.2.9"
},
"devDependencies": {
"d3": "~3.3.3"
Expand Down
74 changes: 29 additions & 45 deletions readmetree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ var exec = require('child_process').exec
var _ = require('underscore')
var marked = require('marked')
var special = require('special-html')
var glob = require('glob')

var style = fs.readFileSync(path.join(__dirname, 'assets', 'style.css'), 'utf8')
var nav = fs.readFileSync(path.join(__dirname, 'assets', 'nav.html'), 'utf8')
var PORT = 9999
var root = process.argv[2]

if (!root) root = process.cwd()
root = root.replace('~', process.env.HOME)
root = path.resolve(process.cwd(), root)

var content = {
Expand All @@ -28,51 +28,35 @@ http.createServer(function(req, res) {
return res.end(content[req.url])
}).listen(PORT)

readdirRecur(root, isNodeModules)
.filter(isReadme)
.sort(function(a, b) {
var aa = path.basename(path.dirname(a)).toLowerCase()
var bb = path.basename(path.dirname(b)).toLowerCase()
return aa.localeCompare(bb)
})
.forEach(function(readme) {
console.log(readme)
var shortpath = readme.replace(path.dirname(path.dirname(readme)), '')
if (content[shortpath]) return
content[shortpath] =
style + nav + special(marked(fs.readFileSync(readme, 'utf8')))
content['/'] +=
'<br><a href="http://localhost:' + PORT + shortpath + '">'
+ 'http://localhost:' + PORT + '<b>' + shortpath + '</b></a>'
})
glob('**/readme.m*', { cwd: root, nocase: true }, renderFiles)

var url = 'http://localhost:' + PORT
console.log(url + ' is serving all your readmes')
exec('open ' + url)

function isNodeModules(f) {
var bname = path.basename(f)
return 'node_modules' === bname
}
function isReadme(file) {
return path.basename(file).toLowerCase().indexOf('readme.m') === 0
}
function renderFiles(err, files) {
if (err) {
console.error(err)
return process.exit(1)
}

function readdirRecur(dir, recurAgain) {
var files = fs.readdirSync(dir)
files = files.map(function(f) {
return path.join(dir, f)
})
var moreFiles = []
files.forEach(function(f) {
if (!recurAgain(f)) return
// console.log('recur on', f)
var module_dirs = fs.readdirSync(f)
module_dirs.forEach(function(module_dir) {
moreFiles = moreFiles.concat(
readdirRecur(path.join(f, module_dir), recurAgain)
)
files.sort(function(a, b) {
var aa = path.basename(path.dirname(a)).toLowerCase()
var bb = path.basename(path.dirname(b)).toLowerCase()
return aa.localeCompare(bb)
})
.forEach(function(readme) {
var shortpath = path.sep + readme.split(path.sep).slice(-2).join(path.sep)
var moduleName = shortpath.split(path.sep)[1]
if (shortpath.lastIndexOf(path.sep) === 0) {
moduleName = path.basename(root)
}
if (content[shortpath]) return
content[shortpath] =
style + nav +
special(marked(fs.readFileSync(path.resolve(root, readme), 'utf8')))
content['/'] +=
'<br><a href="http://localhost:'+PORT+shortpath+'">'
+ moduleName + '</a>'
})
})
return files.concat(moreFiles)

var url = 'http://localhost:'+PORT
console.log(url+' is serving all your readmes')
exec('open '+url)
}