Skip to content

Commit

Permalink
download extension information from the npm
Browse files Browse the repository at this point in the history
  • Loading branch information
zaggino committed Mar 7, 2015
1 parent 07fb81f commit 70ef242
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.12
before_install:
- npm install -g gulp-cli mocha
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
# brackets-npm-registry
# brackets-npm-registry [![Build Status](https://travis-ci.org/zaggino/brackets-npm-registry.svg?branch=master)](https://travis-ci.org/zaggino/brackets-npm-registry)
Extension to install other extensions with npm

## gulp tasks
Expand Down
7 changes: 7 additions & 0 deletions gulpfile.js
Expand Up @@ -31,6 +31,12 @@ function logPipe(str) {
});
}

// prevents watch from crashing on errors
function swallowError (error) {
console.log(error.toString());
this.emit('end');
}

// helper for transpiling es6 files to es5
function doBabel(globs, singleFile) {
if (singleFile) {
Expand All @@ -40,6 +46,7 @@ function doBabel(globs, singleFile) {
var task = gulp.src(globs, {base: path.resolve(__dirname, 'src')})
.pipe(sourcemaps.init())
.pipe(babel(babelOptions))
.on('error', swallowError)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(DIST_DIR));

Expand Down
16 changes: 13 additions & 3 deletions package.json
Expand Up @@ -2,7 +2,11 @@
"name": "brackets-npm-registry",
"version": "0.0.2",
"description": "Boilerplate for a Brackets extension written in ES6",
"keywords": ["brackets-extension", "boilerplate", "hello-world"],
"keywords": [
"brackets-extension",
"boilerplate",
"hello-world"
],
"homepage": "https://github.com/zaggino/brackets-npm-registry",
"bugs": "https://github.com/zaggino/brackets-npm-registry/issues",
"license": "MIT",
Expand All @@ -16,22 +20,28 @@
"url": "https://github.com/zaggino/brackets-npm-registry.git"
},
"scripts": {
"test": "gulp test",
"test": "mocha",
"install": "gulp build"
},
"dependencies": {
"bluebird": "^2.9.13",
"babel-core": "^4.6.6",
"bluebird": "^2.9.13",
"eslint": "^0.15.1",
"esprima-fb": "^13001.1.0-dev-harmony-fb",
"gulp": "^3.8.11",
"gulp-babel": "^4.0.0",
"gulp-eslint": "^0.5.0",
"gulp-sourcemaps": "^1.5.0",
"gulp-util": "^3.0.4",
"npm": "^2.7.0",
"request": "^2.53.0",
"through2": "^0.6.3"
},
"engines": {
"brackets": ">=1.2.0"
},
"devDependencies": {
"chai": "^2.1.1",
"mocha": "^2.2.0"
}
}
93 changes: 93 additions & 0 deletions src/npm-domain.js
@@ -0,0 +1,93 @@
(function () {
'use strict';

let npm = require('npm');
let domainName = 'brackets-npm-registry-domain';
let domainManager = null;
let Promise = require('bluebird');
let request = require('request');

let getExtensions = exports.getExtensions = function (callback) {

let npmLoad = Promise.promisify(npm.load, npm);
npmLoad()
.then(() => {
// get all entries tagged 'brackets-extension'
let npmSearch = Promise.promisify(npm.commands.search, npm.commands);
return npmSearch(['brackets-extension'], true);
})
.then(searchResults => {
// call view for all potential extensions
let npmView = Promise.promisify(npm.commands.view, npm.commands);
return Promise.all(Object.keys(searchResults).map(
extensionId =>
npmView([extensionId + '@latest'], true).then(result =>
result[Object.keys(result)[0]]
)
));
})
.then(viewResults => {
// filter out those, which doesn't have brackets engine specified
return viewResults.filter(result => result.engines && result.engines.brackets);
})
.then(extensionInfos => {
// get download counts for the extensions
let extensionIds = extensionInfos.map(i => i.name);
let from = '2015-01-01';
let to = new Date().toISOString().substring(0,10);
return new Promise((resolve, reject) => {
request(`https://api.npmjs.org/downloads/range/${from}:${to}/${extensionIds.join(',')}`, (error, response, body) => {

if (error) {
return reject(error);
}

if (response.statusCode !== 200) {
return reject(body);
}

if (typeof body === 'string') {
try {
body = JSON.parse(body);
} catch (err) {
return reject(err);
}
}

if (extensionIds.length === 1) {
extensionInfos[0].downloads = body.downloads;
} else {
extensionInfos.forEach(extensionInfo => {
extensionInfo.downloads = body[extensionInfo.name].downloads;
});
}

resolve(extensionInfos);

});
});
})
.nodeify(callback);

};

exports.init = function (_domainManager) {
domainManager = _domainManager;

if (!domainManager.hasDomain(domainName)) {
domainManager.registerDomain(domainName, {major: 0, minor: 1});
}

domainManager.registerCommand(
domainName,
'getExtensions', // command name
getExtensions, // handler function
true, // is async
'get a list of extensions from npm', // description
[
{name: 'extensions', type: 'array'}
]
);
};

}());
21 changes: 21 additions & 0 deletions test/index.js
@@ -0,0 +1,21 @@
var expect = require("chai").expect;
var npmDomain = require('../dist/npm-domain');

describe('npm-domain', function () {

describe('getExtensions()', function () {

it('should return a list of extensions', function (done) {

this.timeout(100000);

npmDomain.getExtensions(function (err, response) {
// console.log(JSON.stringify(response, null, 4));
done(err);
});

});

});

});

0 comments on commit 70ef242

Please sign in to comment.