diff --git a/.flowconfig b/.flowconfig index 19c7441a4bed..aed572defa69 100644 --- a/.flowconfig +++ b/.flowconfig @@ -14,6 +14,7 @@ private/tmp [options] suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe unsafe.enable_getters_and_setters=true +module.system.node.resolve_dirname=/node_modules [version] 0.38.0 \ No newline at end of file diff --git a/src/cli/commands/public-cmds/search-cmd.js b/src/cli/commands/public-cmds/search-cmd.js index 01c34c2e8f3f..b536fe1bd9f3 100644 --- a/src/cli/commands/public-cmds/search-cmd.js +++ b/src/cli/commands/public-cmds/search-cmd.js @@ -12,7 +12,7 @@ export default class Search extends Command { ['r', 'reindex', 're-index all components'] ]; - action([query, ]: [string[], ], { scope, reindex }) { + action([query, ]: [string[], ], { scope, reindex }: { scope: string, reindex: boolean }) { const queryStr = query.join(' '); console.log(`searching bits in ${scope ? scope : 'local scope'} for "${queryStr}"`); if (scope) { diff --git a/src/scope/network/ssh/ssh.js b/src/scope/network/ssh/ssh.js index d0b568c44bb3..3eacc394c76c 100644 --- a/src/scope/network/ssh/ssh.js +++ b/src/scope/network/ssh/ssh.js @@ -100,6 +100,10 @@ export default class SSH { }); } + search(query: string, reindex: boolean) { + return this.exec('_search', query, reindex.toString()); + } + show(id: BitId) { return this.exec('_show', id.toString()) .then((str: string) => { diff --git a/src/search/indexer.js b/src/search/indexer.js index 2ec32c8aed1a..d76ffe027cfb 100644 --- a/src/search/indexer.js +++ b/src/search/indexer.js @@ -5,13 +5,24 @@ import { parser } from '../jsdoc'; import Component from '../consumer/component'; import serverlessIndex from './serverless-index'; +export type Doc = { + id: string, + name: string, + tokenizedName: string, + box: string, + tokenizedBox: string, + functionNames: string, + tokenizedFunctionNames: string, + description: string +}; + let localIndex; -function tokenizeStr(str: string) { +function tokenizeStr(str: string): string { return str.trim().split(/(?=[A-Z])/).join(' ').toLowerCase().split(/ |_|-/).join(' '); } -function prepareDoc(docs: Object, component: Component): Object { +function prepareDoc(docs: Object, component: Component): Doc { const name = component.name; const box = component.box; const functionNames = docs.map(doc => doc.name).join(' '); @@ -27,7 +38,7 @@ function prepareDoc(docs: Object, component: Component): Object { }; } -function addToLocalIndex(component: Component): Promise { +function addToLocalIndex(component: Component): Promise { return new Promise((resolve, reject) => { const doc = prepareDoc(component.docs, component); localIndex.then((indexInstance) => { @@ -47,7 +58,7 @@ function addToLocalIndex(component: Component): Promise { }); } -function index(component: Component, scopePath: string) { +function index(component: Component, scopePath: string): Promise { localIndex = serverlessIndex.initializeIndex(scopePath); return addToLocalIndex(component); } diff --git a/src/search/searcher.js b/src/search/searcher.js index a705b69ecc05..c41cc59e63d1 100644 --- a/src/search/searcher.js +++ b/src/search/searcher.js @@ -1,12 +1,11 @@ /** @flow */ import serverlessIndex from './serverless-index'; import indexer from './indexer'; +import type Doc from './indexer'; -let localIndex; - -function totalHits(query: string) { +function totalHits(index: Promise, query: string) { return new Promise((resolve, reject) => { - return localIndex.then((indexInstance) => { + return index.then((indexInstance) => { indexInstance.totalHits({ query: buildQuery(query) }, (err, count) => { @@ -17,9 +16,9 @@ function totalHits(query: string) { }); } -function countDocs() { +function countDocs(index: Promise) { return new Promise((resolve, reject) => { - return localIndex.then((indexInstance) => { + return index.then((indexInstance) => { indexInstance.countDocs((err, info) => { if (err) reject(err); resolve(info); @@ -28,9 +27,9 @@ function countDocs() { }); } -function getDoc(docIds: string[]) { +function getDoc(index: Promise, docIds: string[]) { return new Promise((resolve, reject) => { - return localIndex.then((indexInstance) => { + return index.then((indexInstance) => { indexInstance.get(docIds).on('data', function (doc) { console.log(doc); }); @@ -38,8 +37,7 @@ function getDoc(docIds: string[]) { }); } -function formatSearchResult(searchResult: Object): string { - const doc = searchResult.document; +function formatSearchResult(doc: Doc): string { return `> ${doc.box}/${doc.name}`; } @@ -63,16 +61,16 @@ function buildQuery(queryStr: string) { return query; } -function search(queryStr: string, path: string) { +function search(queryStr: string, path: string): Promise { return new Promise((resolve, reject) => { - localIndex = serverlessIndex.initializeIndex(path); + const index = serverlessIndex.initializeIndex(path); const searchResults = []; const query = buildQuery(queryStr); - return localIndex.then((indexInstance) => { + return index.then((indexInstance) => { indexInstance.search({ query, }).on('data', function (data) { - searchResults.push(formatSearchResult(data)); + searchResults.push(formatSearchResult(data.document)); }).on('end', function () { return resolve(JSON.stringify(searchResults)); }); diff --git a/src/search/serverless-index.js b/src/search/serverless-index.js index 8e97cfec5fc3..0f9910418763 100644 --- a/src/search/serverless-index.js +++ b/src/search/serverless-index.js @@ -1,8 +1,7 @@ /** @flow */ import path from 'path'; +import fs from 'fs-extra'; import searchIndex from 'search-index'; -import { loadConsumer } from '../consumer'; -const fs = require('fs-extra'); const indexName = 'search_index'; const logLevel = 'error';