| ID | Title | Duration (ms) |
|---|---|---|
| 1 | fixTime processes time.modified | 8 |
| 2 | cacheDb handles errors | 1 |
| 3 | complete passes expected args to database | 2 |
| 4 | trimit does not error on nonwords | 0 |
| 5 | main can produce help | 6 |
| 6 | main getReporter has a default of "write" | 1 |
| 7 | main getReporter has default that can be overridden by cmd | 5 |
| 8 | main getReporter cmd can be overridden by user cmd | 1 |
| 9 | main getOptions has default db | 2 |
| 10 | main getOptions default db can be overridden | 0 |
| 11 | main getCommand identifies command when set | 0 |
| 12 | main getCommand returns search when no command set | 1 |
| 13 | main runCommand avoids db when not required | 0 |
| 14 | main runCommand loads db when required | 13 |
| 15 | script writes out the nks completion script | 1 |
| 16 | version writes out the nks script | 1 |
| 17 | withDb handles early error | 1 |
| 18 | cleanup objects gracefully handles missing members | 1 |
| 19 | populate db can add a record | 2 |
| 20 | populate db can freeze db | 6 |
| 21 | populate db misses missing records | 4 |
| 22 | populate db can find by fts | 4 |
| 23 | search searches single word fts | 1 |
| 24 | search searches by name | 0 |
| 25 | search handles missing search by name | 1 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | module.exports = function (maintainers) { |
| 4 | 4 | return (maintainers || []).map(function (m) { |
| 5 | 2 | return '=' + m.name; |
| 6 | }).join(' '); | |
| 7 | }; | |
| 8 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | function complete(db, opts, done) { |
| 4 | 1 | var start = opts.complete, |
| 5 | end = start + String.fromCharCode(0x10FFFF), | |
| 6 | reporter = opts.reporter; | |
| 7 | ||
| 8 | 1 | db.findRange(start, end, function (err, val) { |
| 9 | /*jslint unparam: true*/ | |
| 10 | 1 | reporter.write(val); |
| 11 | }, function () { | |
| 12 | 1 | done(); |
| 13 | }); | |
| 14 | } | |
| 15 | ||
| 16 | 1 | module.exports = complete; |
| 17 | 1 | complete.useDb = true; |
| 18 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var main = require('./main'); |
| 4 | 1 | var complete = require('./complete'); |
| 5 | 1 | var makeWriteReporter = require('./reporters/write'); |
| 6 | ||
| 7 | 1 | function completeApi(term, stream, done) { |
| 8 | var opts = { | |
| 9 | db: main.defaultDb(), | |
| 10 | reporter: makeWriteReporter(stream), | |
| 11 | complete: term | |
| 12 | }; | |
| 13 | ||
| 14 | main.runCommand(complete, opts, done); | |
| 15 | } | |
| 16 | ||
| 17 | 1 | module.exports = completeApi; |
| 18 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var Hoek = require('hoek'); |
| 4 | 1 | var authors = require('./authors'); |
| 5 | ||
| 6 | 1 | function trimit(w) { |
| 7 | 3 | return w && w.trim && w.trim(); |
| 8 | } | |
| 9 | ||
| 10 | 1 | function fts(obj) { |
| 11 | 2 | var words = [ obj.name ] |
| 12 | .concat(obj.keywords) | |
| 13 | .concat(obj.description); | |
| 14 | ||
| 15 | 2 | words = words.join(' ') |
| 16 | .split(/(\s|[\.-\/\\!=])+/); | |
| 17 | ||
| 18 | 2 | words = words.concat(authors(obj.maintainers)); |
| 19 | ||
| 20 | 2 | words = Hoek.unique(words); |
| 21 | ||
| 22 | 2 | return words.join(' '); |
| 23 | } | |
| 24 | ||
| 25 | 1 | module.exports = fts; |
| 26 | ||
| 27 | 1 | fts.trimit = trimit; |
| 28 | ||
| 29 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var dbs = {}, |
| 4 | makeDb = require('./makeDb'); | |
| 5 | ||
| 6 | 1 | function cacheDb(name, cb) { |
| 7 | 3 | return function (err, db) { |
| 8 | 3 | if (err) { |
| 9 | 1 | return cb(err); |
| 10 | } | |
| 11 | ||
| 12 | 2 | dbs[name] = db; |
| 13 | 2 | cb(null, db); |
| 14 | }; | |
| 15 | } | |
| 16 | ||
| 17 | ||
| 18 | 1 | function getDb(name, cb) { |
| 19 | 3 | if (!dbs[name]) { |
| 20 | 2 | makeDb(name, cacheDb(name, cb)); |
| 21 | } else { | |
| 22 | 1 | setImmediate(function () { |
| 23 | 1 | cb(null, dbs[name]); |
| 24 | }); | |
| 25 | } | |
| 26 | } | |
| 27 | ||
| 28 | 1 | module.exports = getDb; |
| 29 | ||
| 30 | 1 | getDb.cacheDb = cacheDb; |
| 31 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | function help(opts, done) { |
| 4 | 1 | var reporter = opts.reporter; |
| 5 | ||
| 6 | 1 | reporter.write('npm-kludge-search [flags] <term> [...<terms>]'); |
| 7 | 1 | reporter.write(''); |
| 8 | 1 | reporter.write(' --help Brief help text'); |
| 9 | 1 | reporter.write(' --db db Search database <db>'); |
| 10 | 1 | reporter.write(' --name name Exact search by <name>'); |
| 11 | 1 | reporter.write(' --complete prefix Show completions staring with <prefix>'); |
| 12 | 1 | reporter.write(' --script Output completion shell script'); |
| 13 | 1 | reporter.write(' --reporter Set reporter (slow, fast, json)'); |
| 14 | 1 | reporter.write(''); |
| 15 | ||
| 16 | 1 | reporter.end(); |
| 17 | 1 | done(); |
| 18 | } | |
| 19 | ||
| 20 | 1 | module.exports = help; |
| 21 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var getDb = require('./getDb'), |
| 4 | path = require('path'), | |
| 5 | nopt = require('nopt'), | |
| 6 | commands = { | |
| 7 | "help": require('./help'), | |
| 8 | "script": require('./script'), | |
| 9 | "version": require('./version'), | |
| 10 | "name": require('./name'), | |
| 11 | "complete": require('./complete'), | |
| 12 | "default": require('./search') | |
| 13 | }, | |
| 14 | known = { | |
| 15 | "db": path, | |
| 16 | "name": String, | |
| 17 | "version": Boolean, | |
| 18 | "reporter": ["slow", "fast", "json", "write", "null"], | |
| 19 | "complete": String, | |
| 20 | "script": Boolean, | |
| 21 | "help": Boolean | |
| 22 | }; | |
| 23 | ||
| 24 | 1 | function getReporter(r, output) { |
| 25 | 6 | var opts = this, |
| 26 | makeReporter; | |
| 27 | ||
| 28 | 6 | r = opts.reporter || r || 'write'; |
| 29 | 6 | makeReporter = require('./reporters/' + r); |
| 30 | ||
| 31 | 6 | opts.rep = r; |
| 32 | 6 | opts.reporter = makeReporter(output); |
| 33 | ||
| 34 | 6 | return opts.reporter; |
| 35 | } | |
| 36 | ||
| 37 | 1 | function defaultDb() { |
| 38 | /*jslint nomen:true */ | |
| 39 | 2 | return path.resolve(__dirname, "..", "npmdb.pft"); |
| 40 | } | |
| 41 | ||
| 42 | 1 | function getOptions(argv) { |
| 43 | 3 | var opts = nopt(known, {}, argv, 2); |
| 44 | ||
| 45 | // defaults | |
| 46 | 3 | opts.db = opts.db || defaultDb(); |
| 47 | 3 | opts.getReporter = getReporter; |
| 48 | ||
| 49 | 3 | return opts; |
| 50 | } | |
| 51 | ||
| 52 | 1 | function getCommand(opts) { |
| 53 | 5 | var cmd; |
| 54 | ||
| 55 | 5 | Object.keys(opts).forEach(function (key) { |
| 56 | 10 | if (commands[key]) { |
| 57 | 4 | cmd = commands[key]; |
| 58 | } | |
| 59 | }); | |
| 60 | ||
| 61 | 5 | if (!cmd) { |
| 62 | 1 | cmd = commands.default; |
| 63 | } | |
| 64 | ||
| 65 | 5 | return cmd; |
| 66 | } | |
| 67 | ||
| 68 | 1 | function withDb(getDb, opts, cb, done) { |
| 69 | 2 | getDb(opts.db, function (err, db) { |
| 70 | 2 | if (err) { |
| 71 | 1 | return done(err); |
| 72 | } | |
| 73 | ||
| 74 | 1 | cb(db, opts, done); |
| 75 | }); | |
| 76 | } | |
| 77 | ||
| 78 | 1 | function runCommand(cmd, opts, done) { |
| 79 | 5 | if (!cmd.useDb) { |
| 80 | 4 | cmd(opts, done); |
| 81 | } else { | |
| 82 | 1 | withDb(getDb, opts, cmd, done); |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | ||
| 87 | 1 | function main(args, done) { |
| 88 | // deal with EPIPE | |
| 89 | 1 | process.stdout.on('error', done); |
| 90 | ||
| 91 | 1 | var opts = getOptions(args), |
| 92 | cmd = getCommand(opts); | |
| 93 | ||
| 94 | 1 | opts.getReporter(cmd.reporter, process.stdout); |
| 95 | ||
| 96 | 1 | runCommand(cmd, opts, done); |
| 97 | } | |
| 98 | ||
| 99 | 1 | module.exports = main; |
| 100 | ||
| 101 | // exported as completion API | |
| 102 | 1 | main.complete = require('./completeApi'); |
| 103 | 1 | main.defaultDb = defaultDb; |
| 104 | ||
| 105 | // exported for unit tests | |
| 106 | 1 | main.getCommand = getCommand; |
| 107 | 1 | main.getOptions = getOptions; |
| 108 | 1 | main.getReporter = getReporter; |
| 109 | 1 | main.runCommand = runCommand; |
| 110 | 1 | main.withDb = withDb; |
| 111 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var Purefts = require('pure-fts'); |
| 4 | ||
| 5 | 1 | function makeDb(name, cb) { |
| 6 | // return an sqlite db | |
| 7 | ||
| 8 | 4 | Purefts.thaw(name, function (err, db) { |
| 9 | 4 | if (err) { |
| 10 | 2 | db = new Purefts(); |
| 11 | 2 | db.name = name; |
| 12 | } | |
| 13 | ||
| 14 | 4 | cb(null, db); |
| 15 | }); | |
| 16 | } | |
| 17 | ||
| 18 | 1 | module.exports = makeDb; |
| 19 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var getDb = require('./getDb'); |
| 4 | ||
| 5 | 1 | function name(db, opts, done) { |
| 6 | 2 | var reporter = opts.reporter; |
| 7 | ||
| 8 | 2 | function results_done() { |
| 9 | 2 | reporter.end(); |
| 10 | 2 | done(); |
| 11 | } | |
| 12 | ||
| 13 | 2 | db.get(opts.name, function (err, val) { |
| 14 | 2 | if (err) { |
| 15 | 1 | return results_done(); |
| 16 | } | |
| 17 | ||
| 18 | 1 | reporter.write(val); |
| 19 | 1 | setImmediate(results_done); |
| 20 | }); | |
| 21 | } | |
| 22 | ||
| 23 | 1 | module.exports = name; |
| 24 | 1 | name.useDb = true; |
| 25 | 1 | name.reporter = 'slow'; |
| 26 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var authors = require('./authors'); |
| 4 | 1 | var fts = require('./fts'); |
| 5 | ||
| 6 | ||
| 7 | 1 | function formatTime(t) { |
| 8 | 3 | return new Date(t).toISOString() |
| 9 | .split('T').join(' ') | |
| 10 | .replace(/:[0-9]{2}\.[0-9]{3}Z$/, '') | |
| 11 | .slice(0, -5); | |
| 12 | } | |
| 13 | ||
| 14 | 1 | function fixTime(time) { |
| 15 | 7 | if (time && time.modified) { |
| 16 | 3 | return formatTime(time.modified); |
| 17 | } | |
| 18 | ||
| 19 | 4 | return 'prehistoric'; |
| 20 | } | |
| 21 | ||
| 22 | 1 | module.exports = function (db) { |
| 23 | ||
| 24 | 4 | db.cleanPackage = function (obj) { |
| 25 | 2 | return { |
| 26 | author: authors(obj.maintainers), | |
| 27 | version: Object.keys(obj.versions || {})[0], | |
| 28 | keywords: [].concat(obj.keywords).join(', '), | |
| 29 | description: obj.description || "", | |
| 30 | date: fixTime(obj.time), | |
| 31 | name: obj.name, | |
| 32 | fts: fts(obj) | |
| 33 | }; | |
| 34 | }; | |
| 35 | ||
| 36 | 4 | db.addPackage = function (obj) { |
| 37 | 1 | var o = this.cleanPackage(obj); |
| 38 | 1 | this.add(o); |
| 39 | }; | |
| 40 | ||
| 41 | 4 | return db; |
| 42 | }; | |
| 43 | ||
| 44 | 1 | module.exports.fixTime = fixTime; |
| 45 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var fs = require('fs'); |
| 4 | 1 | var path = require('path'); |
| 5 | ||
| 6 | /*jslint nomen:true*/ | |
| 7 | 1 | var shellFile = path.resolve(__dirname, '../nks.sh'); |
| 8 | /*jslint nomen:false*/ | |
| 9 | ||
| 10 | 1 | function script(opts, done) { |
| 11 | 1 | var reporter = opts.reporter; |
| 12 | ||
| 13 | 1 | fs.readFile(shellFile, {encoding: "utf8"}, function (err, body) { |
| 14 | 1 | reporter.write(String(body)); |
| 15 | 1 | reporter.end(); |
| 16 | 1 | done(err); |
| 17 | }); | |
| 18 | } | |
| 19 | ||
| 20 | 1 | module.exports = script; |
| 21 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var populateDb = require('./populateDb'), |
| 4 | getDb = require('./getDb'); | |
| 5 | ||
| 6 | 1 | function searchDb(db, opts, done) { |
| 7 | 1 | var reporter = opts.reporter; |
| 8 | ||
| 9 | 1 | function each_result(err, obj) { |
| 10 | /*jslint unparam: true*/ | |
| 11 | 1 | reporter.write(obj); |
| 12 | } | |
| 13 | ||
| 14 | 1 | function results_done() { |
| 15 | 1 | reporter.end(); |
| 16 | 1 | done(); |
| 17 | } | |
| 18 | ||
| 19 | 1 | db.search(opts.argv.remain[0], each_result, results_done); |
| 20 | } | |
| 21 | ||
| 22 | 1 | module.exports = searchDb; |
| 23 | 1 | searchDb.useDb = true; |
| 24 | 1 | searchDb.reporter = 'slow'; |
| 25 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | function version(opts, done) { |
| 4 | 1 | var p = require('../package.json'), |
| 5 | r = opts.reporter; | |
| 6 | ||
| 7 | 1 | r.write('npm-kludge-search@' + p.version); |
| 8 | 1 | r.end(); |
| 9 | ||
| 10 | 1 | done(); |
| 11 | } | |
| 12 | ||
| 13 | 1 | module.exports = version; |
| 14 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var es = require('event-stream'); |
| 4 | ||
| 5 | 1 | function makeReporter(output) { |
| 6 | ||
| 7 | 2 | return es.through(function write(data) { |
| 8 | 2 | delete data.fts; |
| 9 | 2 | output.write(JSON.stringify(data, null, 0) + '\n'); |
| 10 | }, function end() { | |
| 11 | 2 | output.end(); |
| 12 | }); | |
| 13 | } | |
| 14 | ||
| 15 | 1 | module.exports = makeReporter; |
| 16 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var es = require('event-stream'); |
| 4 | ||
| 5 | 1 | function makeReporter() { |
| 6 | 10 | var rep, res = []; |
| 7 | ||
| 8 | 10 | rep = es.through(function (item) { |
| 9 | 17 | res.push(item); |
| 10 | }); | |
| 11 | 10 | rep.res = res; |
| 12 | ||
| 13 | 10 | return rep; |
| 14 | } | |
| 15 | ||
| 16 | 1 | module.exports = makeReporter; |
| 17 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | 'use strict'; |
| 2 | ||
| 3 | 1 | var es = require('event-stream'); |
| 4 | ||
| 5 | 1 | function makeReporter(output) { |
| 6 | 3 | return es.through(function write(data) { |
| 7 | 3 | output.write(data + "\n"); |
| 8 | }, function end() { | |
| 9 | 3 | output.end(); |
| 10 | }); | |
| 11 | } | |
| 12 | ||
| 13 | 1 | module.exports = makeReporter; |
| 14 |