-
Notifications
You must be signed in to change notification settings - Fork 131
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
5 changed files
with
283 additions
and
1 deletion.
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
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,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); | ||
} | ||
|
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,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 | ||
}; | ||
}; |
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,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" | ||
} | ||
} |