2017-04-23 13:02:26 -05:00
|
|
|
'use strict';
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-23 13:02:26 -05:00
|
|
|
const lunr = require('lunr');
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Handle the search Indexer.
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
class Search {
|
2017-04-26 16:48:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
constructor() {
|
|
|
|
this.index = lunr(function() {
|
2017-04-23 13:02:26 -05:00
|
|
|
this.field('name', {boost: 10});
|
|
|
|
this.field('description', {boost: 4});
|
|
|
|
this.field('author', {boost: 6});
|
|
|
|
this.field('readme');
|
|
|
|
});
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Performs a query to the indexer.
|
|
|
|
* If the keyword is a * it returns all local elements
|
|
|
|
* otherwise performs a search
|
|
|
|
* @param {*} q the keyword
|
2017-04-26 20:59:11 -05:00
|
|
|
* @return {Array} list of results.
|
2017-04-26 16:48:55 -05:00
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
query(q) {
|
|
|
|
return q === '*'
|
|
|
|
? this.storage.config.localList.get().map( function( pkg ) {
|
2017-04-23 13:02:26 -05:00
|
|
|
return {ref: pkg, score: 1};
|
2017-04-17 08:52:36 -05:00
|
|
|
}) : this.index.search(q);
|
|
|
|
}
|
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Add a new element to index
|
|
|
|
* @param {*} pkg the package
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
add(pkg) {
|
|
|
|
this.index.add({
|
2017-04-23 13:02:26 -05:00
|
|
|
id: pkg.name,
|
|
|
|
name: pkg.name,
|
|
|
|
description: pkg.description,
|
|
|
|
author: pkg._npmUser ? pkg._npmUser.name : '???',
|
|
|
|
});
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Remove an element from the index.
|
|
|
|
* @param {*} name the id element
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
remove(name) {
|
2017-04-23 13:02:26 -05:00
|
|
|
this.index.remove({id: name});
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Force a reindex.
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
reindex() {
|
2017-04-23 13:02:26 -05:00
|
|
|
let self = this;
|
2017-04-17 08:52:36 -05:00
|
|
|
this.storage.get_local(function(err, packages) {
|
2017-04-23 13:02:26 -05:00
|
|
|
if (err) throw err; // that function shouldn't produce any
|
|
|
|
let i = packages.length;
|
2017-04-17 08:52:36 -05:00
|
|
|
while (i--) {
|
2017-04-23 13:02:26 -05:00
|
|
|
self.add(packages[i]);
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
2017-04-23 13:02:26 -05:00
|
|
|
});
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-26 16:48:55 -05:00
|
|
|
/**
|
|
|
|
* Set up the {Storage}
|
|
|
|
* @param {*} storage An storage reference.
|
|
|
|
*/
|
2017-04-17 08:52:36 -05:00
|
|
|
configureStorage(storage) {
|
2017-04-23 13:02:26 -05:00
|
|
|
this.storage = storage;
|
|
|
|
this.reindex();
|
2017-04-17 08:52:36 -05:00
|
|
|
}
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 08:52:36 -05:00
|
|
|
module.exports = new Search();
|