Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script for keeping tern repository updated by using metadataurl property... #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions core/tern.core/metadata/_update-tern-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Read '_metadata-repository.json'
* and replace every entry with new by reading the latest info from `metadataurl` (if present).
* If there is no `metadataurl` in the new entry, then keep older value
* (This allows an older plugin to redirect to new location).
* '_metadata-repository.json' file is only saved if and when all requested json files are received and parsed.
*
* before running do `npm i request`
* that will be install into ../node_modules
*
* @author Paul Verest
*/

var repositoryFile = '_metadata-repository.json';

var fs = require('fs');
var request = require('request');

var ternRepository = require('./'+repositoryFile);

var counter = 0;
var requestCount = 0;
var responseCount = 0;

var requestedAllEntries = false;

var saveRepositoryJsonFile = function(){
console.log(''+requestCount+' requests, '+responseCount+' responses');
if (!requestedAllEntries){
// unlikely situation when some response comes before cycle finishes
return;
}
if (requestCount > responseCount){
// not all responses yet came
return;
}

fs.writeFileSync('./'+repositoryFile, JSON.stringify(ternRepository,null,2) );
console.log('Finished processing '+counter+' entries. '+repositoryFile+' file is updated.');
};

for (var i in ternRepository){
var entry = ternRepository[i];
counter++;
console.log(entry);

if (!entry.metadataurl){
continue;
}

(function closure(i, metadataurl) { //closure
requestCount++;
request.get(entry.metadataurl, function onResponse(error, response, body) {
if (!error && response.statusCode == 200) {
console.log('updating entry '+i+' with');
console.log(body);
var entry = JSON.parse(body);
if (!entry.metadataurl){
entry.metadataurl = metadataurl;
}
ternRepository[i] = entry;
responseCount++;
saveRepositoryJsonFile();
// we don't know what response will come the last
// at that point we are to save file
}
});
})(i, entry.metadataurl);
};

requestedAllEntries = true;
saveRepositoryJsonFile();