Skip to content

Commit

Permalink
Merge branch 'master' into pr/60
Browse files Browse the repository at this point in the history
  • Loading branch information
F1LT3R committed Sep 30, 2018
2 parents a8c1cee + d1ade0e commit 08388ee
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 42 deletions.
115 changes: 78 additions & 37 deletions lib/readme.js
Expand Up @@ -2,63 +2,104 @@

'use strict'

require('./splash')()

const fs = require('fs')
const path = require('path')
const flags = require('commander')
const splash = require('./splash')

const markserv = require(path.join(__dirname, 'server'))
const pkg = require(path.join(__dirname, '..', './package.json'))

const cwd = process.cwd()
const run = args => {
splash(args)

flags.dir = cwd
const cwd = process.cwd()
/* eslint-disable-next-line no-mixed-operators */
flags.dir = args && args.dir || cwd
flags.$pathProvided = true

const fileExistsSync = uri => {
let exists
const fileExistsSync = uri => {
let exists

try {
const stat = fs.statSync(uri)
if (stat.isFile()) {
exists = true
try {
const stat = fs.statSync(uri)
if (stat.isFile()) {
exists = true
}
} catch (err) {
exists = false
}
} catch (err) {
exists = false

return exists
}

return exists
}
const findFileUp = (dir, fileToFind) => {
const filepath = path.join(dir, fileToFind)
const existsHere = fileExistsSync(filepath)

if (dir === path.sep || dir === '.') {
return false
}

if (existsHere) {
return filepath
}

const findFileUp = (dir, fileToFind) => {
const filepath = path.join(dir, fileToFind)
const existsHere = fileExistsSync(filepath)
const nextDirUp = path.dirname(dir)
return findFileUp(nextDirUp, fileToFind)
}

if (dir === path.sep || dir === '.') {
return false
const findReadmeFile = dir => {
const readmeFile = findFileUp(dir, 'README.md') ||
findFileUp(dir, 'readme.md') ||
findFileUp(dir, 'README.MD') ||
findFileUp(dir, 'Readme.md')
return readmeFile
}

if (existsHere) {
return filepath
flags.version(pkg.version)
.usage('[path_to_readme]')
.option('-p, --port [type]', 'HTTP port [port]', 8642)
.option('-l, --livereloadport [type]', 'LiveReload port [livereloadport]', 35729)
.option('-i, --silent [type]', 'Silent (no logs to CLI)', false)
.option('-a, --address [type]', 'Serve on ip/address [address]', 'localhost')
.option('-b, --browser [type]', 'Launch browser', true)
.option('-v, --verbose', 'verbose output')
.action(pathToReadme => {
pathToReadme = path.resolve(pathToReadme)

if (flags.dir[0] === '/') {
flags.dir = pathToReadme
} else {
flags.dir = path.normalize(path.join(cwd, pathToReadme))
}

const readmeFile = findReadmeFile(flags.dir)
flags.dir = readmeFile || flags.dir
flags.$pathProvided = true
}).parse(process.argv)

if (!Array.isArray(args) && typeof args === 'object') {
Reflect.ownKeys(args).forEach(key => {
flags[key] = args[key]
})
}

const nextDirUp = path.dirname(dir)
return findFileUp(nextDirUp, fileToFind)
}
if (typeof flags.pathToReadme === 'undefined') {
const readmeFile = findReadmeFile(flags.dir)
flags.dir = readmeFile || flags.dir
flags.$pathProvided = true
flags.$openLocation = true
}

const README = findFileUp(cwd, 'README.md')
const readme = findFileUp(cwd, 'readme.md')
return markserv.init(flags)
}

flags.version(pkg.version)
.usage('<file/dir>')
.option('-p, --port [type]', 'HTTP port [port]', 8642)
.option('-l, --livereloadport [type]', 'LiveReload port [livereloadport]', 35729)
.option('-i, --silent [type]', 'Silent (no logs to CLI)', false)
.option('-a, --address [type]', 'Serve on ip/address [address]', 'localhost')
.option('-v, --verbose', 'verbose output')
.parse(process.argv)
const cli = !module.parent

flags.dir = README || readme || cwd
flags.$pathProvided = true
if (cli) {
run()
} else {
module.exports = {run}
}

markserv.init(flags)
19 changes: 15 additions & 4 deletions lib/server.js
Expand Up @@ -92,7 +92,8 @@ const fileTypes = {
],

exclusions: [
'node_modules/'
'node_modules/',
'.git/'
]
}

Expand Down Expand Up @@ -688,16 +689,26 @@ const init = async flags => {
// Log server info to CLI
logActiveServerInfo(serveURL, httpPort, liveReloadPort, flags)

let launchUrl = false
if (flags.$openLocation || flags.$pathProvided) {
launchUrl = serveURL + '/' + flags.$openLocation
}

const service = {
pid: process.pid,
httpServer,
liveReloadServer,
connectApp
connectApp,
launchUrl
}

const launchBrowser = () => {
if (flags.$openLocation || flags.$pathProvided) {
opn(serveURL + '/' + flags.$openLocation)
if (flags.browser === false) {
return
}

if (launchUrl) {
opn(launchUrl)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "markserv",
"version": "1.13.1",
"version": "1.14.0",
"description": "馃弫 serve markdown as html (GitHub style), index directories, live-reload as you edit",
"preferGlobal": true,
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions tests/markserv-cli-readme-dir-down.expected.html
@@ -0,0 +1 @@
<p>馃弫 serve markdown as html (GitHub style), index directories, and live-reload as you edit</p>
60 changes: 60 additions & 0 deletions tests/markserv-cli-readme-dir-down.test.js
@@ -0,0 +1,60 @@
import fs from 'fs'
import path from 'path'
import request from 'request'
import test from 'ava'
import getPort from 'get-port'
import readme from '../lib/readme'

test.cb('start markserv via "readme" command', t => {
t.plan(3)

const expected = String(
fs.readFileSync(
path.join(__dirname, 'markserv-cli-readme-dir-down.expected.html')
)
)

const dir = path.join(__dirname, '..')

getPort().then(port => {
const flags = {
dir,
port,
livereloadport: false,
address: 'localhost',
silent: true,
browser: false
}

const done = () => {
t.end()
}

readme.run(flags).then(service => {
const closeServer = () => {
service.httpServer.close(done)
}

const opts = {
url: service.launchUrl,
timeout: 1000 * 2
}

request(opts, (err, res, body) => {
if (err) {
t.fail(err)
closeServer()
}

t.true(body.includes(expected))

t.is(res.statusCode, 200)
t.pass()
closeServer()
})
}).catch(err => {
t.fail(err)
t.end()
})
})
})
1 change: 1 addition & 0 deletions tests/markserv-cli-readme.expected.html
@@ -0,0 +1 @@
<h1 id="pass">PASS</h1>
62 changes: 62 additions & 0 deletions tests/markserv-cli-readme.test.js
@@ -0,0 +1,62 @@
import fs from 'fs'
import path from 'path'
import request from 'request'
import test from 'ava'
import getPort from 'get-port'
import readme from '../lib/readme'

test.cb('start markserv via "readme" command', t => {
t.plan(3)

const expected = String(
fs.readFileSync(
path.join(__dirname, 'markserv-cli-readme.expected.html')
)
)

const dir = path.join(
__dirname, '..', 'tests', 'markserv-cli-readme'
) + path.sep

getPort().then(port => {
const flags = {
dir,
port,
livereloadport: false,
address: 'localhost',
silent: true,
browser: false
}

const done = () => {
t.end()
}

readme.run(flags).then(service => {
const closeServer = () => {
service.httpServer.close(done)
}

const opts = {
url: service.launchUrl,
timeout: 1000 * 2
}

request(opts, (err, res, body) => {
if (err) {
t.fail(err)
closeServer()
}

t.true(body.includes(expected))

t.is(res.statusCode, 200)
t.pass()
closeServer()
})
}).catch(err => {
t.fail(err)
t.end()
})
})
})
1 change: 1 addition & 0 deletions tests/markserv-cli-readme/README.md
@@ -0,0 +1 @@
# PASS

0 comments on commit 08388ee

Please sign in to comment.