diff --git a/src/lib/search.ts b/src/lib/search.ts index 2816f3193..3dd614aef 100644 --- a/src/lib/search.ts +++ b/src/lib/search.ts @@ -43,13 +43,23 @@ class Search implements IWebSearch { public query(query: string): any[] { const localStorage = this.storage.localStorage as IStorage; - return query === '*' + const hasScope = query.startsWith('@'); + // FIXME: lunr-mutable-indexes ignored '@' during indexing + if (hasScope) { + query = query.replace('@', ''); + } + + const results = query === '*' ? localStorage.storagePlugin.get((items): any => { items.map(function (pkg): any { return { ref: pkg, score: 1 }; }); }) : this.index.search(`*${query}*`); + + return hasScope + ? results.filter(({ ref }) => ref.startsWith('@')) + : results; } /** diff --git a/test/unit/modules/search/search.spec.ts b/test/unit/modules/search/search.spec.ts index 81a454dd8..b16fd00d6 100644 --- a/test/unit/modules/search/search.spec.ts +++ b/test/unit/modules/search/search.spec.ts @@ -27,6 +27,20 @@ let packages = [ _npmUser: { name: 'test_user' } + }, + { + name: '@verdaccio/scope', + description: 'scope', + _npmUser: { + name: 'scope_user' + } + }, + { + name: '@any/scope', + description: 'scope', + _npmUser: { + name: 'scope_user' + } } ]; @@ -47,6 +61,13 @@ describe('search', () => { expect(result).toHaveLength(3); }); + test('search query with @scope', () => { + let result = Search.query('@'); + expect(result).toHaveLength(2); + result = Search.query('@verdaccio'); + expect(result).toHaveLength(1); + }) + test('search remove item', () => { let item = { name: 'test6', diff --git a/test/unit/modules/web/api.web.spec.ts b/test/unit/modules/web/api.web.spec.ts index 9aae329e2..4cbfa7127 100644 --- a/test/unit/modules/web/api.web.spec.ts +++ b/test/unit/modules/web/api.web.spec.ts @@ -180,8 +180,7 @@ describe('endpoint web unit test', () => { .end(function (err, res) { // in a normal world, the output would be 1 // https://github.com/verdaccio/verdaccio/issues/345 - // should fix this - expect(res.body).toHaveLength(0); + expect(res.body).toHaveLength(1); done(); }); });