Skip to content

Commit

Permalink
Use package for determining if path is directory
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Jan 15, 2017
1 parent 783a899 commit f144ab6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
23 changes: 4 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const fs = require('fs-promise')
const auth = require('basic-auth')
const stream = require('send')
const {copy} = require('copy-paste')
const pathType = require('path-type')

args
.option('port', 'Port to listen on', process.env.PORT)
Expand All @@ -37,22 +38,6 @@ if (directory) {
current = path.resolve(process.cwd(), directory)
}

const isDir = async dir => {
let stats

try {
stats = await fs.stat(dir)
} catch (err) {
return false
}

if (stats.isDirectory()) {
return true
}

return false
}

const prepareView = () => {
let viewContent = false
const viewPath = path.normalize(path.join(__dirname, '/../views/index.hbs'))
Expand Down Expand Up @@ -96,7 +81,7 @@ const renderDirectory = async dir => {

details.relative = path.join(subPath, details.base)

if (await isDir(filePath)) {
if (await pathType.dir(filePath)) {
details.base += '/'
} else {
details.ext = details.ext.split('.')[1] || 'txt'
Expand Down Expand Up @@ -208,7 +193,7 @@ const handler = async (req, res) => {
}

// Check if file or directory path
if (await isDir(related)) {
if (await pathType.dir(related)) {
let indexPath = path.join(related, '/index.html')

res.setHeader('Content-Type', mime.contentType(path.extname(indexPath)))
Expand Down Expand Up @@ -337,7 +322,7 @@ server.listen(flags.port, async () => {
process.exit(0)
})

if (!await isDir(current)) {
if (!await pathType.dir(current)) {
console.error(red('Specified directory doesn\'t exist!'))
process.exit(1)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"micro": "^6.1.0",
"micro-compress": "^1.0.0",
"mime-types": "^2.1.12",
"path-type": "^2.0.0",
"send": "^0.14.1",
"update-notifier": "^1.0.3"
}
Expand Down

0 comments on commit f144ab6

Please sign in to comment.