0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/lib/search.js

53 lines
1 KiB
JavaScript
Raw Normal View History

'use strict';
const lunr = require('lunr');
2017-04-17 08:52:36 -05:00
class Search {
constructor() {
this.index = lunr(function() {
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
}
2017-04-17 08:52:36 -05:00
query(q) {
return q === '*'
? this.storage.config.localList.get().map( function( pkg ) {
return {ref: pkg, score: 1};
2017-04-17 08:52:36 -05:00
}) : this.index.search(q);
}
add(pkg) {
this.index.add({
id: pkg.name,
name: pkg.name,
description: pkg.description,
author: pkg._npmUser ? pkg._npmUser.name : '???',
});
2017-04-17 08:52:36 -05:00
}
remove(name) {
this.index.remove({id: name});
2017-04-17 08:52:36 -05:00
}
reindex() {
let self = this;
2017-04-17 08:52:36 -05:00
this.storage.get_local(function(err, packages) {
if (err) throw err; // that function shouldn't produce any
let i = packages.length;
2017-04-17 08:52:36 -05:00
while (i--) {
self.add(packages[i]);
2017-04-17 08:52:36 -05:00
}
});
2017-04-17 08:52:36 -05:00
}
2017-04-17 08:52:36 -05:00
configureStorage(storage) {
this.storage = storage;
this.reindex();
2017-04-17 08:52:36 -05:00
}
}
2017-04-17 08:52:36 -05:00
module.exports = new Search();