Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private/tmp
[options]
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
unsafe.enable_getters_and_setters=true
module.system.node.resolve_dirname=<PROJECT_ROOT>/node_modules

[version]
0.38.0
2 changes: 1 addition & 1 deletion src/cli/commands/public-cmds/search-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/scope/network/ssh/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
19 changes: 15 additions & 4 deletions src/search/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
Expand All @@ -27,7 +38,7 @@ function prepareDoc(docs: Object, component: Component): Object {
};
}

function addToLocalIndex(component: Component): Promise<any> {
function addToLocalIndex(component: Component): Promise<string> {
return new Promise((resolve, reject) => {
const doc = prepareDoc(component.docs, component);
localIndex.then((indexInstance) => {
Expand All @@ -47,7 +58,7 @@ function addToLocalIndex(component: Component): Promise<any> {
});
}

function index(component: Component, scopePath: string) {
function index(component: Component, scopePath: string): Promise<string> {
localIndex = serverlessIndex.initializeIndex(scopePath);
return addToLocalIndex(component);
}
Expand Down
26 changes: 12 additions & 14 deletions src/search/searcher.js
Original file line number Diff line number Diff line change
@@ -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<any>, query: string) {
return new Promise((resolve, reject) => {
return localIndex.then((indexInstance) => {
return index.then((indexInstance) => {
indexInstance.totalHits({
query: buildQuery(query)
}, (err, count) => {
Expand All @@ -17,9 +16,9 @@ function totalHits(query: string) {
});
}

function countDocs() {
function countDocs(index: Promise<any>) {
return new Promise((resolve, reject) => {
return localIndex.then((indexInstance) => {
return index.then((indexInstance) => {
indexInstance.countDocs((err, info) => {
if (err) reject(err);
resolve(info);
Expand All @@ -28,18 +27,17 @@ function countDocs() {
});
}

function getDoc(docIds: string[]) {
function getDoc(index: Promise<any>, 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);
});
});
});
}

function formatSearchResult(searchResult: Object): string {
const doc = searchResult.document;
function formatSearchResult(doc: Doc): string {
return `> ${doc.box}/${doc.name}`;
}

Expand All @@ -63,16 +61,16 @@ function buildQuery(queryStr: string) {
return query;
}

function search(queryStr: string, path: string) {
function search(queryStr: string, path: string): Promise<string> {
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));
});
Expand Down
3 changes: 1 addition & 2 deletions src/search/serverless-index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down