Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacklone committed May 4, 2014
1 parent edc2cc5 commit 897c221
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 1 deletion.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 Barna Tóth
Copyright (c) 2014 Hacklone
https://github.com/Hacklone

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,68 @@ private-bower
=============

A simple private bower registry

#Installion

Install
> npm install -g private-bower
Run
> private-bower
Optional Parameters
> private-bower -p 5678 -o "output file path"
#Usage

##List packages
GET
> bower-server:5678/packages
##Register package
POST
> bower-server:5678/registerPackage
> { "name": "package-name", "repo": "git://repoPath" }
##Register packages
POST
> bower-server:5678/registerPackages
> { "packages": [ { "name": "package-name", "repo": "git://repoPath" } ] }
##Remove package
POST
> bower-server:5678/removePackage
> { "name": "package-name" }
##Remove packages
POST
> bower-server:5678/removePackages
> { "packages": ["package-name"] }
#License
> The MIT License (MIT)
> Copyright (c) 2014 Hacklone
> https://github.com/Hacklone
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.
111 changes: 111 additions & 0 deletions bin/private-bower
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env node

var PackageStore = require('../lib/private-bower');

var argv = require('optimist').argv;
var colors = require('colors');
var path = require('path');

var express = require('express');
var app = express();

var _port, _persistFilePath, _packageStore;

_init();
function _init() {
if (argv.h || argv.help) {
console.log([
'usage: private-bower [options]',
'',
'options:',
' -p Port to use [5678]',
' -o Repository persist file [bowerRepository.xml]',
' -h --help Print this list and exit.'
].join('\n'));
process.exit();
}

_port = argv.p || argv.port || 5678;
_persistFilePath = argv.o || argv.output || path.join(__dirname, 'bowerRepository.xml');

_packageStore = new PackageStore({
persistFilePath: _persistFilePath
});

_initService();
}

function _initService() {
app.use(express.bodyParser());

app.post('/registerPackage', function(req, res) {
_packageStore.registerPackages([ {
name: req.body.name,
repo: req.body.repo
} ]);

_log('registered package: ' + req.body.name);
res.send('ok');
});

app.post('/registerPackage', function(req, res) {
_packageStore.registerPackages(req.body.packages);

_log('registered several packages');
res.send('ok');
});

app.post('/removePackage', function(req, res) {
_packageStore.removePackages([ req.body.name ]);

_log('removed package: ' + req.body.name);
res.send('ok');
});

app.post('/removePackages', function(req, res) {
_packageStore.removePackages(req.body.packages);

_log('removed packages: ' + req.body.packages.join(', '));
res.send('ok');
});

app.get('/packages', function(req, res) {
var packages = [];

for(var packageName in _packageStore.packages) {
if(_packageStore.packages.hasOwnProperty(packageName)) {
packages.push({
name: packageName,
repo: _packageStore.packages[packageName]
});
}
}

res.send(packages);
});

//bower service
app.get('/packages/:name', function(req, res) {console.log(req);
var repo = _packageStore.packages[req.params.name];

if(repo) {
res.send({
"name": req.params.name,
"url": repo,
"hits": 1
});
}
else {
res.status(404).send('Not found');
}
});

app.listen(_port, function() {
_log('Bower server started on port ' + _port);
});
}

function _log(text) {
console.log('[bower] '.green, text);
}

64 changes: 64 additions & 0 deletions lib/private-bower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var fs = require('fs');
var js2xmlparser = require("js2xmlparser");
var xml2js = require('xml2js');


module.exports = function PackageStore(options) {
var _packages = {};

_init();
function _init() {
_loadPackages();
}

function _registerPackages(register) {
for(var i = 0, len = register.length; i < len; i ++) {
var registerPackage = register[i];
_packages[registerPackage.name] = registerPackage.repo;
}

_persistPackages();
}

function _removePackages(remove) {
for(var i = 0, len = remove.length; i < len; i ++) {
delete _packages[remove[i]];
}

_persistPackages();
}

function _persistPackages() {
if(fs.existsSync(options.persistFilePath)) {
fs.unlinkSync(options.persistFilePath);
}

var xml = js2xmlparser('packages', _packages);

fs.writeFile(options.persistFilePath, xml);
}

function _loadPackages() {
if(!fs.existsSync(options.persistFilePath)) {
return;
}

var parser = new xml2js.Parser();
var xml = fs.readFileSync(options.persistFilePath).toString();

parser.parseString(xml, function (err, result) {
for(var packageName in result.packages) {
if(result.packages.hasOwnProperty(packageName)) {
_packages[packageName] = result.packages[packageName][0];
}
}
});
}

return {
packages: _packages,

registerPackages: _registerPackages,
removePackages: _removePackages
};
};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "private-bower",
"version": "0.0.1",
"author": "Hacklone <[email protected]>",
"description": "A simple private bower registry",
"main": "./lib/private-bower",
"repository": {
"type": "git",
"url": "git://github.com/Hacklone/private-bower.git"
},
"keywords": [
"bower",
"private",
"registry",
"repository"
],
"scripts": {
"start": "node ./bin/private-bower"
},
"dependencies": {
"colors": "0.6.x",
"optimist": "0.5.x",
"express": "3.x.x",
"js2xmlparser": "0.1.x",
"xml2js": "0.4.x"
},
"bugs": {
"url": "https://github.com/Hacklone/private-bower/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/Hacklone/private-bower/raw/master/LICENSE"
}
],
"analyze": false,
"preferGlobal": "true",
"bin": {
"private-bower": "./bin/private-bower"
}
}

0 comments on commit 897c221

Please sign in to comment.