Skip to content

Commit

Permalink
Add nls read <package>. Resolve #68
Browse files Browse the repository at this point in the history
  • Loading branch information
amio committed Aug 23, 2019
1 parent f9a80b7 commit 1d71a1f
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 21 deletions.
24 changes: 18 additions & 6 deletions lib/nls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const mri = require('mri')
const kleur = require('kleur')
const list = require('./list.js')
const why = require('./why.js')
const read = require('./read.js')

const help = `
${kleur.bold('nls')} - Missing inspector for npm packages.
Expand All @@ -12,6 +13,7 @@ const help = `
$ nls [<target-dir>] ${kleur.dim('List available npm scripts.')}
$ nls why <package-name> ${kleur.dim('Identifies why a package has been installed.')}
$ nls read <package-name> ${kleur.dim('Show readme file for specified package.')}
Options
Expand All @@ -31,20 +33,30 @@ const help = `
`

const args = mri(process.argv.slice(2), {
boolean: ['help', 'version', 'why'],
boolean: ['help', 'version'],
alias: {
h: 'help',
v: 'version',
w: 'why'
v: 'version'
}
})

if (args.help) {
console.info(help)
} else if (args.version) {
console.info(require('../package.json').version)
} else if (args._[0] === 'why' && args._.length === 2) {
why(process.cwd(), args._[1])
} else {
} else if (args._.length <= 1) {
list()
} else if (args._.length >= 2) {
const [cmd, ...params] = args._

switch (cmd) {
case 'why':
why(process.cwd(), ...params)
break
case 'read':
read(process.cwd(), ...params)
break
default:
console.info(help)
}
}
30 changes: 30 additions & 0 deletions lib/read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs')
const path = require('path')
const marked = require('marked')
const TerminalRenderer = require('marked-terminal')

module.exports = function (dir, pkg) {
const readme = findReadme(path.join(dir, 'node_modules', pkg))
if (readme) {
marked.setOptions({
renderer: new TerminalRenderer()
})
console.info(marked(readme))
}
}

function findReadme (dir) {
let readme
const names = ['README.md', 'readme.md', 'readme', 'README']
names.find(file => {
readme = tryRead(path.join(dir, file))
return readme
})
return readme
}

function tryRead (filePath) {
try {
return fs.readFileSync(filePath, 'utf8')
} catch {}
}
102 changes: 87 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"dependencies": {
"kleur": "^3.0.3",
"lodash.padend": "^4.6",
"marked": "^0.7.0",
"marked-terminal": "^3.3.0",
"mri": "^1.1.4",
"npm-why": "^1.1.7",
"yarn-why": "^0.3.1"
Expand Down

0 comments on commit 1d71a1f

Please sign in to comment.