Created
December 6, 2016 19:12
-
-
Save craigshoemaker/2d67998a0d74fa2f5e8f47789fbb9d94 to your computer and use it in GitHub Desktop.
Using search-index with Gulp.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const through = require('through2'); | |
const uuidV4 = require('uuid/v4'); | |
module.exports = () => { | |
return through.obj(function (file, encoding, next) { | |
this.push({ | |
id: uuidV4(), | |
body: file.contents.toString(encoding) | |
}); | |
next(); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const searchIndex = require('search-index'); | |
const args = require('yargs') | |
.usage('gulp db --dbpath PATH_TO_DATABASE --src PATH_TO_FILES_TO_ADD_TO_DATABASE') | |
.demand(['dbpath', 'src']) | |
.argv; | |
// gulp db --dbpath "./_db/topics" --src "C:\topics\*.txt" | |
module.exports.registerTask = (gulp) => { | |
gulp.task('db', (done) => { | |
const options = { indexPath: args.dbpath }; | |
searchIndex(options, (openDatabaseError, index) => { | |
if (openDatabaseError) { | |
console.log(`Error: ${openDatabaseError}`); | |
return; | |
} | |
const addTopicToStream = require('./addTopicToStream.js'); | |
gulp.src(args.src) | |
.on('error', (e) => console.log(e)) | |
.pipe(addTopicToStream()) | |
.pipe(index.defaultPipeline()) | |
.pipe(index.add()) | |
.on('data', (data) => { console.log(data); }) | |
.on('end', done); | |
}); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulp = require('gulp'); | |
require('./gulp/gulp-db.js').registerTask(gulp); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"gulp": "^3.9.1", | |
"search-index": "^0.9.5", | |
"through2": "^2.0.3", | |
"uuid": "^3.0.1", | |
"yargs": "^6.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment