Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#16 #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ module.exports = {
},
"rules": {
// TODO: change these rules to errors and fix codebase
"prefer-const": 1, // 4 errors
"no-unused-vars": 1, // 3 errors
"no-use-before-define": 1, // 7 errors
"one-var": 1, // 8 errors
"consistent-return": 1, // 8 errors
"no-param-reassign": 1, // 2 errors
"global-require": 1, // 2 errors
"import/no-dynamic-require": 1, // 1 error
"comma-dangle": ["error", "never"]
}
Expand Down
26 changes: 13 additions & 13 deletions lib/cli/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ const start = require('./start');
* Create a new app with folder structure and files
*/
const create = function (name) {
name = name || 'my-deployd-app';
if (test('-d', name)) {
return console.info(`${name} already exists in this directory`);
const newProjectName = name || 'my-deployd-app';
if (test('-d', newProjectName)) {
return console.info(`${newProjectName} already exists in this directory`);
}

mkdir('-p', name);
cp('-Rf', path.join(__dirname, '../createtemplate/*'), name);
mkdir('-p', `${name}/.dpd`);
mkdir('-p', `${name}/.dpd/pids`);
('').to(`${name}/.dpd/pids/mongod`);
mkdir('-p', newProjectName);
cp('-Rf', path.join(__dirname, '../createtemplate/*'), newProjectName);
mkdir('-p', `${newProjectName}/.dpd`);
mkdir('-p', `${newProjectName}/.dpd/pids`);
('').to(`${newProjectName}/.dpd/pids/mongod`);
('# npm\r\nnode_modules/\r\nnpm-debug.log\r\n\r\n# deployd\r\n.dpd/\r\ndata/')
.to(`${name}/.gitignore`);
rm(ls('-R', name).filter(p => path.basename(p) === 'PLACEHOLDER').map(p => `${name}/${p}`));
.to(`${newProjectName}/.gitignore`);
rm(ls('-R', newProjectName).filter(p => path.basename(p) === 'PLACEHOLDER').map(p => `${newProjectName}/${p}`));

process.chdir(name);
process.chdir(newProjectName);

console.log('dpd is installing the dependencies... please be patient (this may take a few minutes)');
const child = exec('npm install',
Expand All @@ -40,10 +40,10 @@ const create = function (name) {
console.log(stderr);
console.log(`npm install error: ${error}`);
} else if (program.dashboard || program.open) {
start(`${name}/app.dpd`);
start(`${newProjectName}/app.dpd`);
} else {
console.info('to start your app:');
console.info('\t$ cd', name);
console.info('\t$ cd', newProjectName);
console.info('\t$ dpd');
}
});
Expand Down
5 changes: 3 additions & 2 deletions lib/cli/keygen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const Keys = require('../keys');

/**
* Generate a key
*/
const keygen = function () {
const Keys = require('../keys'),
keys = new Keys();
const keys = new Keys();

keys.create((err, key) => {
if (err) return console.error(err);
Expand Down
5 changes: 3 additions & 2 deletions lib/cli/showkey.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const Keys = require('../keys');

/**
* Show current key
*/
const showkey = function () {
const Keys = require('../keys'),
keys = new Keys();
const keys = new Keys();

keys.getLocal((err, key) => {
if (err) return console.error(err);
Expand Down
172 changes: 80 additions & 92 deletions lib/cli/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,98 @@ const packageInfo = require('../../package');
const latestversionFile = path.join(__dirname, '../../.latestversion');
const createServer = require('./createserver');

function checkForUpdates() {
http.get('http://registry.npmjs.org/deployd-cli', (err, res, body) => {
if (!err) {
let json;
try {
json = JSON.parse(body);
} catch (ex) {
console.log('Could not parse body', body);
}

if (json && json['dist-tags'] && json['dist-tags'].latest) {
const latest = json['dist-tags'].latest;
fs.writeFile(latestversionFile, latest);
}
}
});
}

/**
* Start the server
*/
const start = function (file) {
let port = program.port,
host = program.host || '127.0.0.1',
dbname = program.dbname || '-deployd',
mongoPort = generatePort(),
env = program.environment || process.env.DPD_ENV || 'development',
retries = 0,
credentials = {};
let port = program.port;
const host = program.host || '127.0.0.1';
const dbname = program.dbname || '-deployd';
const mongoPort = program.mongoPort ? Number(program.mongoPort) : '27017';
const env = program.environment || process.env.DPD_ENV || 'development';
let retries = 0;
const credentials = {};


if (!port) {
port = 2403;
retries = env === 'development' && 5;
}

if (program.mongoPort) {
mongoPort = Number(program.mongoPort);
}

if (file) {
process.chdir(path.dirname(file));
}
function startup(err) {
if (err) {
console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)");
return stop(1);
}

const options = { port, env: 'development', db: { host, port: mongoPort, name: dbname } };

options.env = program.environment || process.env.DPD_ENV || options.env;
if (options.env !== 'development') console.log('starting in %s mode', options.env);

if (credentials !== undefined) options.db.credentials = credentials;

let dpd = createServer(options);

function onListening() {
console.info('listening on port', options.port);
const commands = repl(dpd);
if (program.dashboard) {
commands.dashboard();
} else if (program.open) {
commands.open();
}
}

function onError(err2) {
if (err2.code === 'EADDRINUSE') {
console.error();
console.error(`ERROR: port ${options.port} is already in use`);
if (retries > 0) {
options.port += 1;
console.log(`Trying again on port ${options.port}...`);
console.log();
retries -= 1;
dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
} else {
process.exit();
}
} else {
console.error(err2);
process.exit();
}
}

dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
dpd.deploydPath = program.deploydPath;
}

if (test('-f', 'app.dpd')) {
console.log(`deployd CLI version ${packageInfo.version}`);
console.log('starting deployd');
Expand Down Expand Up @@ -98,9 +164,9 @@ const start = function (file) {

if (program.host) {
if (program.auth) {
const auth = program.auth.split(':'),
username = auth[0],
password = auth[1];
const auth = program.auth.split(':');
const username = auth[0];
const password = auth[1];
setCredentials(username, password);
} else if (program.username || program.password) {
setCredentials(program.username, program.password);
Expand All @@ -116,84 +182,6 @@ const start = function (file) {
console.log('or use "dpd path/to/app.dpd" to start an app in another directory');
stop(1);
}

function startup(err) {
if (err) {
console.log("Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)");
return stop(1);
}

const options = { port, env: 'development', db: { host, port: mongoPort, name: dbname } };

options.env = program.environment || process.env.DPD_ENV || options.env;
if (options.env !== 'development') console.log('starting in %s mode', options.env);

if (credentials !== undefined) options.db.credentials = credentials;

let dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
dpd.deploydPath = program.deploydPath;

function onListening() {
console.info('listening on port', options.port);
const commands = repl(dpd);
if (program.dashboard) {
commands.dashboard();
} else if (program.open) {
commands.open();
}
}

function onError(err2) {
if (err2.code === 'EADDRINUSE') {
console.error();
console.error(`ERROR: port ${options.port} is already in use`);
if (retries > 0) {
options.port += 1;
console.log(`Trying again on port ${options.port}...`);
console.log();
retries -= 1;
dpd = createServer(options);
dpd.on('listening', onListening);
dpd.on('error', onError);
dpd.listen();
} else {
process.exit();
}
} else {
console.error(err2);
process.exit();
}
}
}
};

/**
* Port generation
*/
function generatePort() {
const portRange = [3000, 9000];
return Math.floor(Math.random() * (portRange[1] - portRange[0])) + portRange[0];
}

function checkForUpdates() {
http.get('http://registry.npmjs.org/deployd-cli', (err, res, body) => {
if (!err) {
let json;
try {
json = JSON.parse(body);
} catch (ex) {
console.log('Could not parse body', body);
}

if (json && json['dist-tags'] && json['dist-tags'].latest) {
const latest = json['dist-tags'].latest;
fs.writeFile(latestversionFile, latest);
}
}
});
}

module.exports = start;
13 changes: 7 additions & 6 deletions lib/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ Keys.prototype.generate = function () {
*/

Keys.prototype.create = function (fn) {
const key = this.generate(),
keys = this;
const key = this.generate();
const keys = this;

this.readFile((err, data) => {
if (err) return fn(err);

data[key] = true;
keys.writeFile(data, (errW) => {
const fileData = data;
fileData[key] = true;
keys.writeFile(fileData, (errW) => {
fn(errW, key);
});
});
Expand All @@ -54,8 +55,8 @@ Keys.prototype.create = function (fn) {

Keys.prototype.readFile = function (fn) {
fs.readFile(this.path, 'utf-8', (err, data) => {
let jsonData,
error;
let jsonData;
let error;

try {
jsonData = (data && JSON.parse(data)) || {};
Expand Down