-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
182 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const mkdirp = require('mkdirp'); | ||
const rimraf = require('rimraf'); | ||
const Door43Client = require('../'); | ||
const promiseUtils = require('../lib/utils/promises'); | ||
const util = require('./util'); | ||
|
||
exports.command = 'index'; | ||
exports.describe = 'Generates a brand new index'; | ||
exports.builder = { | ||
i: { | ||
alias: 'index', | ||
description: 'The path to the generated index', | ||
default: path.join(process.cwd(), 'index.sqlite') | ||
}, | ||
f: { | ||
alias: 'force', | ||
description: 'Overwrites the index if it already exists', | ||
default: false | ||
}, | ||
u: { | ||
alias: 'url', | ||
description: 'The api url', | ||
default: 'https://api.unfoldingword.org/ts/txt/2/catalog.json' | ||
} | ||
}; | ||
exports.handler = function(argv) { | ||
let indexPath = argv.index; | ||
|
||
// don't overwrite | ||
if(!argv.force) { | ||
let exists = false; | ||
try { | ||
exists = fs.statSync(indexPath).isFile(); | ||
} catch (err) { | ||
} | ||
if (exists) throw new Error('The index path already exist. Aborting.'); | ||
} else { | ||
rimraf.sync(indexPath); | ||
} | ||
|
||
mkdirp.sync(path.dirname(indexPath)); | ||
|
||
console.log('Generating primary index:'); | ||
var client = new Door43Client(indexPath, null); | ||
client.updatePrimaryIndex(argv.url, function(id, total, completed) { | ||
util.logProgress(id, total, completed); | ||
}).then(function() { | ||
// index the catalogs | ||
console.log('\n\nGenerating catalog indexes:'); | ||
return indexCatalogs(client); | ||
}).catch(function(err) { | ||
console.error(err); | ||
}); | ||
}; | ||
|
||
function indexCatalogs(client) { | ||
return client.index.getCatalogs() | ||
.then(function(catalogs) { | ||
var list = []; | ||
for(var catalog of catalogs) { | ||
list.push({ | ||
slug: catalog.slug, | ||
onProgress: util.logProgress | ||
}); | ||
} | ||
return promiseUtils.chain(client.updateCatalogIndex, function (err, data) { | ||
console.log(err); | ||
return false; | ||
})(list); | ||
}) | ||
.then(function() { | ||
// so gulp doesn't choke | ||
console.log('\nFinished!'); | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const readline = require('readline'); | ||
|
||
var lastProgressId; | ||
|
||
/** | ||
* Displays a progress indicator in the console. | ||
* @param id | ||
* @param total | ||
* @param completed | ||
*/ | ||
function writeProgress(id, total, completed) { | ||
var percent = Math.round(10 * (100 * completed) / total) / 10; | ||
if(id == lastProgressId) { | ||
readline.cursorTo(process.stdout, 0); | ||
readline.clearLine(process.stdout, 0); | ||
} else { | ||
lastProgressId = id; | ||
process.stdout.write('\n'); | ||
} | ||
var progressTitles = { | ||
projects: 'Indexing Projects', | ||
chunks: 'Indexing Chunks', | ||
resources: 'Indexing Resources', | ||
container: 'Downloading Containers', | ||
catalog: 'Indexing Catalogs', | ||
langnames: 'Indexing Target Languages', | ||
'temp-langnames': 'Indexing Temporary Target Languages', | ||
'approved-temp-langnames': 'Indexing Approved Temporary Target Languages', | ||
'new-language-questions': 'Indexing Questionnaire' | ||
}; | ||
process.stdout.write((progressTitles[id] || id) + ' ' + percent + '%'); | ||
} | ||
|
||
module.exports.logProgress = writeProgress; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#! /usr/bin/env node | ||
|
||
var commandDir = './bin'; | ||
|
||
var argv = require('yargs') | ||
.commandDir(commandDir) | ||
.help('help') | ||
.alias('h', 'help') | ||
.strict(true) | ||
.demand(1) | ||
.argv; |
This file contains 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
This file contains 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