mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-18 00:47:01 +00:00
feature(package) rm freeport
This commit is contained in:
parent
87895ab946
commit
be370beb05
3 changed files with 18 additions and 36 deletions
|
|
@ -11,7 +11,6 @@ var DIR_LIB = './',
|
|||
config = require(DIR_SERVER + 'config'),
|
||||
prefixer = require(DIR_SERVER + 'prefixer'),
|
||||
express = require('express'),
|
||||
freeport = require('freeport'),
|
||||
tryRequire = require('tryrequire'),
|
||||
logger = tryRequire('morgan'),
|
||||
io = require('socket.io'),
|
||||
|
|
@ -25,7 +24,7 @@ var DIR_LIB = './',
|
|||
*/
|
||||
module.exports = function(options) {
|
||||
var server,
|
||||
getPort, onError,
|
||||
getPort,
|
||||
|
||||
port = process.env.PORT || /* c9 */
|
||||
process.env.VCAP_APP_PORT || /* cloudfoundry */
|
||||
|
|
@ -52,28 +51,19 @@ module.exports = function(options) {
|
|||
if (port < 0 || port > 65535)
|
||||
exit('cloudcmd --port: %s', 'port number could be 1..65535, 0 means any available port');
|
||||
|
||||
getPort = function(fn) {
|
||||
port ? fn(null, port) : freeport(fn);
|
||||
};
|
||||
|
||||
onError = function(error) {
|
||||
exit('cloudcmd --port: %s', error.message);
|
||||
};
|
||||
|
||||
getPort(function(error, port) {
|
||||
server.listen(port, ip, function() {
|
||||
var host = config('ip') || 'localhost';
|
||||
var url = 'http://' + host + ':' + port + prefix() + '/';
|
||||
|
||||
if (error)
|
||||
return onError(error);
|
||||
|
||||
server.listen(port, ip);
|
||||
server.on('error', onError);
|
||||
var port0 = port || server.address().port;
|
||||
var url = 'http://' + host + ':' + port0 + prefix() + '/';
|
||||
|
||||
console.log('url:', url);
|
||||
|
||||
if (config('open'))
|
||||
opn(url);
|
||||
});
|
||||
|
||||
server.on('error', function(error) {
|
||||
exit('cloudcmd --port: %s', error.message);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -98,7 +98,6 @@
|
|||
"files-io": "^1.2.0",
|
||||
"flop": "^1.4.0",
|
||||
"format-io": "^0.9.6",
|
||||
"freeport": "^1.0.5",
|
||||
"http-auth": "^2.3.1",
|
||||
"ishtar": "^1.5.0",
|
||||
"jaguar": "^1.1.0",
|
||||
|
|
|
|||
|
|
@ -7,10 +7,8 @@ const promisify = require('es6-promisify');
|
|||
const pullout = require('pullout');
|
||||
const request = require('request');
|
||||
|
||||
const wrap = (fn, ...a) => (...b) => fn(...a, ...b);
|
||||
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
|
||||
|
||||
const freeport = promisify(require('freeport'));
|
||||
const _pullout = promisify(pullout);
|
||||
|
||||
const get = promisify((url, fn) => {
|
||||
|
|
@ -23,8 +21,6 @@ const put = promisify((options, fn) => {
|
|||
|
||||
const cloudcmd = require('..');
|
||||
|
||||
const host = '127.0.0.1';
|
||||
|
||||
const before = (fn) => {
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
|
@ -32,10 +28,6 @@ const before = (fn) => {
|
|||
server.close();
|
||||
};
|
||||
|
||||
const listen = (port) => {
|
||||
server.listen(port, host, wrap(fn, port, after));
|
||||
};
|
||||
|
||||
app.use(cloudcmd({
|
||||
config: {
|
||||
auth: false,
|
||||
|
|
@ -43,13 +35,14 @@ const before = (fn) => {
|
|||
}
|
||||
}));
|
||||
|
||||
freeport()
|
||||
.then(listen)
|
||||
server.listen(() => {
|
||||
fn(server.address().port, after);
|
||||
});
|
||||
};
|
||||
|
||||
test('cloudcmd: rest: fs: path', (t) => {
|
||||
before((port, after) => {
|
||||
get(`http://${host}:${port}/api/v1/fs`)
|
||||
get(`http://localhost:${port}/api/v1/fs`)
|
||||
.then(warp(_pullout, 'string'))
|
||||
.then(JSON.parse)
|
||||
.then((dir) => {
|
||||
|
|
@ -65,7 +58,7 @@ test('cloudcmd: rest: fs: path', (t) => {
|
|||
|
||||
test('cloudcmd: rest: pack: get', (t) => {
|
||||
before((port, after) => {
|
||||
get(`http://${host}:${port}/api/v1/pack/fixture/pack`)
|
||||
get(`http://localhost:${port}/api/v1/pack/fixture/pack`)
|
||||
.then(_pullout)
|
||||
.then((pack) => {
|
||||
const fixture = fs.readFileSync(__dirname + '/fixture/pack.tar.gz');
|
||||
|
|
@ -82,7 +75,7 @@ test('cloudcmd: rest: pack: get', (t) => {
|
|||
test('cloudcmd: rest: pack: put: file', (t) => {
|
||||
before((port, after) => {
|
||||
const name = String(Math.random()) + '.tar.gz';
|
||||
const options = getPackOptions(host, port, name);
|
||||
const options = getPackOptions(port, name);
|
||||
|
||||
put(options)
|
||||
.then(warp(_pullout, 'string'))
|
||||
|
|
@ -105,7 +98,7 @@ test('cloudcmd: rest: pack: put: file', (t) => {
|
|||
test('cloudcmd: rest: pack: put: response', (t) => {
|
||||
before((port, after) => {
|
||||
const name = String(Math.random()) + '.tar.gz';
|
||||
const options = getPackOptions(host, port, name);
|
||||
const options = getPackOptions(port, name);
|
||||
|
||||
put(options)
|
||||
.then(warp(_pullout, 'string'))
|
||||
|
|
@ -125,7 +118,7 @@ test('cloudcmd: rest: pack: put: response', (t) => {
|
|||
|
||||
test('cloudcmd: rest: pack: put: error', (t) => {
|
||||
before((port, after) => {
|
||||
const options = getPackOptions(host, port, 'name', [
|
||||
const options = getPackOptions(port, 'name', [
|
||||
'not found'
|
||||
]);
|
||||
|
||||
|
|
@ -143,9 +136,9 @@ test('cloudcmd: rest: pack: put: error', (t) => {
|
|||
});
|
||||
});
|
||||
|
||||
function getPackOptions(host, port, to, names = ['pack']) {
|
||||
function getPackOptions(port, to, names = ['pack']) {
|
||||
return {
|
||||
url: `http://${host}:${port}/api/v1/pack`,
|
||||
url: `http://localhost:${port}/api/v1/pack`,
|
||||
json: {
|
||||
to,
|
||||
names,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue