Comparing version 2.0.0 to 2.0.1
@@ -1,1 +0,5 @@ | ||
aaaaa | ||
HELP-ME by Matteo | ||
* start starts a script | ||
* help shows help | ||
'use strict' | ||
const path = require('path') | ||
const commist = require('commist')() | ||
const help = require('./')() | ||
const help = require('./')({ | ||
dir: path.join(path.dirname(require.main.filename), 'doc') | ||
}) | ||
commist.register('help', help.toStdout) | ||
commist.register('start', function () { | ||
console.log('Starting the script!') | ||
}) | ||
commist.parse(process.argv.splice(2)) | ||
const res = commist.parse(process.argv.splice(2)) | ||
if (res) { | ||
help.toStdout() | ||
} |
'use strict' | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { PassThrough, pipeline } = require('readable-stream') | ||
@@ -9,3 +8,2 @@ const glob = require('glob') | ||
const defaults = { | ||
dir: path.join(path.dirname(require.main.filename), 'doc'), | ||
ext: '.txt', | ||
@@ -15,2 +13,11 @@ help: 'help' | ||
function isDirectory (path) { | ||
try { | ||
const stat = fs.lstatSync(path) | ||
return stat.isDirectory() | ||
} catch (err) { | ||
return false | ||
} | ||
} | ||
function helpMe (opts) { | ||
@@ -20,5 +27,9 @@ opts = Object.assign({}, defaults, opts) | ||
if (!opts.dir) { | ||
throw new Error('missing directory') | ||
throw new Error('missing dir') | ||
} | ||
if (!isDirectory(opts.dir)) { | ||
throw new Error(`${opts.dir} is not a directory`) | ||
} | ||
return { | ||
@@ -37,5 +48,9 @@ createStream: createStream, | ||
const out = new PassThrough() | ||
const re = new RegExp(args.map(function (arg) { | ||
return arg + '[a-zA-Z0-9]*' | ||
}).join('[ /]+')) | ||
const re = new RegExp( | ||
args | ||
.map(function (arg) { | ||
return arg + '[a-zA-Z0-9]*' | ||
}) | ||
.join('[ /]+') | ||
) | ||
@@ -45,13 +60,21 @@ glob(opts.dir + '/**/*' + opts.ext, function (err, files) { | ||
files = files.map(function (path) { | ||
const relative = path.replace(opts.dir, '').replace(/^\//, '') | ||
return { path, relative } | ||
}).filter(function (file) { | ||
return file.relative.match(re) | ||
}) | ||
files = files | ||
.map(function (file) { | ||
if (process.platform === 'win32') { | ||
opts.dir = opts.dir.split('\\').join('/') | ||
} | ||
const relative = file.replace(opts.dir, '').replace(/^\//, '') | ||
return { file, relative } | ||
}) | ||
.filter(function (file) { | ||
return file.relative.match(re) | ||
}) | ||
if (files.length === 0) { | ||
return out.emit('error', new Error('no such help file')) | ||
} else if (files.length > 1) { | ||
const exactMatch = files.find((file) => file.relative === `${args[0]}${opts.ext}`) | ||
const exactMatch = files.find( | ||
(file) => file.relative === `${args[0]}${opts.ext}` | ||
) | ||
if (!exactMatch) { | ||
@@ -71,3 +94,3 @@ out.write('There are ' + files.length + ' help pages ') | ||
pipeline(fs.createReadStream(files[0].path), out, () => {}) | ||
pipeline(fs.createReadStream(files[0].file), out, () => {}) | ||
}) | ||
@@ -74,0 +97,0 @@ |
{ | ||
"name": "help-me", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Help command for node, partner of minimist and commist", | ||
@@ -5,0 +5,0 @@ "main": "help-me.js", |
@@ -13,6 +13,6 @@ help-me | ||
var helpMe = require('help-me') | ||
var path = require('path') | ||
var help = helpMe({ | ||
dir: path.join(__dirname, 'doc'), | ||
// the default | ||
dir: path.join(path.dirname(require.main.filename), 'doc'), | ||
// the default | ||
ext: '.txt' | ||
@@ -36,3 +36,6 @@ }) | ||
var commist = require('commist')() | ||
var help = require('help-me')() | ||
var path = require('path') | ||
var help = require('help-me')({ | ||
dir: path.join(__dirname, 'doc') | ||
}) | ||
@@ -39,0 +42,0 @@ commist.register('help', help.toStdout) |
41
test.js
@@ -8,15 +8,36 @@ 'use strict' | ||
test('show the doc/help.txt from the require.main folder if no options are passed', function (t) { | ||
t.plan(2) | ||
test('throws if no directory is passed', function (t) { | ||
try { | ||
helpMe() | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.message, 'missing dir') | ||
} | ||
t.end() | ||
}) | ||
helpMe() | ||
.createStream() | ||
.pipe(concat(function (data) { | ||
fs.readFile('./doc/help.txt', function (err, expected) { | ||
t.error(err) | ||
t.equal(data.toString(), expected.toString()) | ||
}) | ||
})) | ||
test('throws if a normal file is passed', function (t) { | ||
try { | ||
helpMe({ | ||
dir: __filename | ||
}) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.message, `${__filename} is not a directory`) | ||
} | ||
t.end() | ||
}) | ||
test('throws if the directory cannot be accessed', function (t) { | ||
try { | ||
helpMe({ | ||
dir: './foo' | ||
}) | ||
t.fail() | ||
} catch (err) { | ||
t.equal(err.message, './foo is not a directory') | ||
} | ||
t.end() | ||
}) | ||
test('show a generic help.txt from a folder to a stream', function (t) { | ||
@@ -23,0 +44,0 @@ t.plan(2) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11560
294
54