Skip to content

Commit

Permalink
Added path distinction for project (rethinkdb#298)
Browse files Browse the repository at this point in the history
* Added path distinction for project
tests for hz serve

* Allow project_name in server options

* Wrong command arg name

* Upping node versions

* Adding CLI tests to Circle

* Cleaning up of circle.yml
  • Loading branch information
deontologician authored and dalanmiller committed Apr 29, 2016
1 parent 53e2ee7 commit 832cbc7
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 291 deletions.
15 changes: 11 additions & 4 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ machine:
# Set version of node to use
node:
version:
4.2.2
5.7.0

post:
- source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
Expand Down Expand Up @@ -51,13 +51,20 @@ test:
pre:
- ./test/serve.js:
background: true
- mkdir -p $CIRCLE_TEST_REPORTS/xunit
- touch $CIRCLE_TEST_REPORTS/xunit/cli-tests.xml
- touch $CIRCLE_TEST_REPORTS/xunit/client-tests.xml
- touch $CIRCLE_TEST_REPORTS/xunit/server-tests.xml
override:
# Run client tests
- mkdir -p $CIRCLE_TEST_REPORTS/xunit; touch $CIRCLE_TEST_REPORTS/xunit/client-tests.xml; ./node_modules/.bin/mocha --timeout 15000 -R xunit dist/test.js > $CIRCLE_TEST_REPORTS/xunit/client-tests.xml:
- ./node_modules/.bin/mocha --timeout 15000 -R xunit dist/test.js > $CIRCLE_TEST_REPORTS/xunit/client-tests.xml:
pwd: client
parallel: true

# Run server tests
- mkdir -p $CIRCLE_TEST_REPORTS/xunit; touch $CIRCLE_TEST_REPORTS/xunit/server-tests.xml; ./node_modules/.bin/mocha --timeout 15000 -R xunit test/test.js test/schema.js > $CIRCLE_TEST_REPORTS/xunit/server-tests.xml:
- ./node_modules/.bin/mocha --timeout 15000 -R xunit test/test.js test/schema.js > $CIRCLE_TEST_REPORTS/xunit/server-tests.xml:
pwd: server
parallel: true
# Run cli tests
- ./node_modules/.bin/mocha --timeout 15000 -R xunit test > $CIRCLE_TEST_REPORTS/xunit/cli-tests.xml:
pwd: cli
parallel: true
2 changes: 1 addition & 1 deletion cli/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const makeDefaultConfig = (projectName) => `\
# 'project' will change to the given directory
# 'serve_static' will serve files from the given directory over HTTP/HTTPS
#------------------------------------------------------------------------------
project = "${projectName}"
project_name = "${projectName}"
# serve_static = "dist"
Expand Down
65 changes: 32 additions & 33 deletions cli/src/serve.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
'use strict';

const horizon_server = require('@horizon/server');
const interrupt = require('./utils/interrupt');

const fs = require('fs');
const http = require('http');
const https = require('https');
const logger = horizon_server.logger;
const path = require('path');
const toml = require('toml');
const url = require('url');

const start_rdb_server = require('./utils/start_rdb_server');
const interrupt = require('./utils/interrupt');
const exitWithError = require('./utils/exit_with_error');
const isDirectory = require('./utils/is_directory');

const horizon_server = require('@horizon/server');
const logger = horizon_server.logger;

const addArguments = (parser) => {
parser.addArgument([ 'project' ],
parser.addArgument([ 'project_path' ],
{ type: 'string', nargs: '?',
help: 'Change to this directory before serving' });

parser.addArgument([ '--project-name', '-n' ],
{ type: 'string', action: 'store', metavar: 'NAME',
help: 'Name of the Horizon Project server' });

parser.addArgument([ '--bind', '-b' ],
{ type: 'string', action: 'append', metavar: 'HOST',
help: 'Local hostname to serve horizon on (repeatable).' });
Expand Down Expand Up @@ -104,7 +110,10 @@ const default_config_file = './.hz/config.toml';
const make_default_config = () => ({
config: default_config_file,
debug: false,
project: null,
// Default to current directory for path
project_path: '.',
// Default to current directory name for project name
project_name: path.basename(process.cwd()),

bind: [ 'localhost' ],
port: 8181,
Expand Down Expand Up @@ -201,16 +210,14 @@ const initialize_servers = (ctor, opts) => {
}
});
srv.on('error', (err) => {
logger.error(
`HTTP${opts.insecure ? '' : 'S'} server: ${err}`);
process.exit(1);
exitWithError(`HTTP${opts.insecure ? '' : 'S'} server: ${err}`);
});
});
});
};

const createInsecureServers = (opts) => {
logger.warn('Creating insecure HTTP server.');
console.error('Warning: Creating insecure HTTP server.');
return initialize_servers(() => new http.Server(), opts);
};

Expand Down Expand Up @@ -325,8 +332,12 @@ const read_config_from_flags = (parsed) => {
config.serve_static = 'dist';
}

if (parsed.project !== null) {
config.project = parsed.project;
if (parsed.project_name !== null) {
config.project_name = parsed.project_name;
}

if (parsed.project_path) {
config.project_path = parsed.project_path;
}

// Simple boolean flags
Expand Down Expand Up @@ -422,6 +433,7 @@ const startHorizonServer = (servers, opts) => {
auto_create_index: opts.auto_create_index,
rdb_host: opts.rdb_host,
rdb_port: opts.rdb_port,
project_name: opts.project_name,
auth: {
token_secret: opts.token_secret,
allow_unauthenticated: opts.allow_unauthenticated,
Expand All @@ -438,28 +450,15 @@ const runCommand = (opts, done) => {
logger.level = 'debug';
}

if (opts.project !== null) {
try {
// Try to get stats on dir, if successful, change directory to project
if (fs.statSync(path.join(opts.project, '.hz'))) {
process.chdir(opts.project);
}
} catch (e) {
console.error('Project specified but no .hz directory was found.');
console.error(e);
process.exit(1);
}
if (isDirectory(opts.project_path)) {
process.chdir(opts.project_path);
} else {
try {
// Try to get stats on dir, if it doesn't exist, statSync will throw
fs.statSync('.hz');
// Don't need to change directories as we assume we are in a Horizon app dir
} catch (e) {
console.error('Project not specified or .hz directory not found.\nTry changing to a project' +
' with a .hz directory,\nor specify your project path with the --project option');
console.error(e);
process.exit(1);
}
exitWithError(`${opts.project_path} is not a directory`);
}
if (!isDirectory('.hz')) {
const nicePathName = opts.project_path === '.' ?
'this directory' : opts.project_path;
exitWithError(`${nicePathName} doesn't contain an .hz directory`);
}

let http_servers, hz_instance;
Expand Down
8 changes: 8 additions & 0 deletions cli/src/utils/exit_with_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

function exitWithError(message) {
console.error(message);
process.exit(1);
}

module.exports = exitWithError;
14 changes: 14 additions & 0 deletions cli/src/utils/is_directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const path = require('path');
const fs = require('fs');

function isDirectory(dirname) {
try {
return fs.statSync(path.resolve(dirname)).isDirectory();
} catch (e) {
return false;
}
}

module.exports = isDirectory;
2 changes: 1 addition & 1 deletion cli/test/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function assertValidConfig(filepath) {
// Try parsing it
const configObject = toml.parse(tomlData);
// Need an uncommented project name
assert.property(configObject, 'project');
assert.property(configObject, 'project_name');
// Need an uncommented token_secret
assert.property(configObject, 'token_secret');
}
Expand Down
Loading

0 comments on commit 832cbc7

Please sign in to comment.