mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-06-13 08:31:11 +08:00
190 lines
5.3 KiB
JavaScript
Executable File
190 lines
5.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Dependencies
|
|
*/
|
|
|
|
var program = require('commander')
|
|
, deployd = require('../')
|
|
, repl = require('../lib/client/repl')
|
|
, shelljs = require('shelljs/global')
|
|
, mongod = require('../lib/util/mongod')
|
|
, path = require('path')
|
|
, fs = require('fs')
|
|
, tty = require('tty')
|
|
, remote = require('../lib/client/remote')
|
|
, request = require('request')
|
|
, package = require('../package')
|
|
, latestversionFile = path.join(__dirname, '../.latestversion');
|
|
|
|
/**
|
|
* Get the version number from the package.json
|
|
*/
|
|
|
|
program
|
|
.version(require('../package').version)
|
|
.option('-m, --mongod [path]', 'path to mongod executable (defaults to `mongod`)')
|
|
.option('-p, --port [port]', 'port to host server (defaults to 2403)')
|
|
.option('-w, --wait', 'wait for input before exiting')
|
|
.option('-d, --dashboard', 'start the dashboard immediately')
|
|
.option('-o, --open', 'open in a browser')
|
|
.option('-e, --environment [env]', 'defaults to development')
|
|
|
|
/**
|
|
* Commands
|
|
*/
|
|
|
|
program
|
|
.command('create [project-name]')
|
|
.description('\tcreate a project in a new directory\n\teg. `dpd create my-app`')
|
|
.action(function(name) {
|
|
var name = name || 'my-deployd-app';
|
|
mkdir('-p', name);
|
|
mkdir('-p', name + '/public');
|
|
mkdir('-p', name + '/data');
|
|
mkdir('-p', name + '/.dpd');
|
|
mkdir('-p', name + '/.dpd/pids');
|
|
('').to(name + '/.dpd/pids/mongod');
|
|
("{}").to(name + '/app.dpd');
|
|
console.info('to start your app:');
|
|
console.info('\t$ cd', name);
|
|
console.info('\t$ dpd');
|
|
});
|
|
|
|
function start(file) {
|
|
var port = program.port || 2403
|
|
, mongoPort = generatePort();
|
|
if (file) {
|
|
process.chdir(path.dirname(file));
|
|
}
|
|
if (test('-f', 'app.dpd')) {
|
|
console.log("starting deployd v" + package.version + "...");
|
|
|
|
if (fs.existsSync(latestversionFile)) {
|
|
var latest = fs.readFileSync(latestversionFile, 'utf-8');
|
|
if (latest && latest !== package.version) {
|
|
console.log("deployd v" + latest + " is available. Use dpd-update command to install");
|
|
}
|
|
}
|
|
checkForUpdates();
|
|
|
|
if (!test('-d', './.dpd')) mkdir('-p', './.dpd');
|
|
if (!test('-d', './.dpd/pids')) mkdir('-p', './.dpd/pids');
|
|
if (!test('-d', './data')) mkdir('-p', './data');
|
|
|
|
mongod.restart(program.mongod || 'mongod', process.env.DPD_ENV || 'development', mongoPort, function(err) {
|
|
if (err) {
|
|
console.log("Failed to start MongoDB");
|
|
return stop(1);
|
|
}
|
|
var options = {port: port, env: 'development', db: {host: '127.0.0.1', port: mongoPort, name: '-deployd'}}
|
|
|
|
options.env = program.environment || process.env.DPD_ENV || options.env;
|
|
if(options.env !== 'development') console.log('starting in %s mode', options.env);
|
|
|
|
var dpd = deployd(options);
|
|
dpd.listen();
|
|
dpd.on('listening', function () {
|
|
|
|
console.info('listening on port', port);
|
|
var commands = repl(dpd);
|
|
if (program.dashboard) {
|
|
commands.dashboard();
|
|
} else if (program.open) {
|
|
commands.open();
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
console.log("This directory does not contain a Deployd app!");
|
|
console.log("Use \"dpd create <appname>\" to create a new app");
|
|
console.log("or use \"dpd path/to/app.dpd\" to start an app in another directory");
|
|
stop(1);
|
|
}
|
|
}
|
|
|
|
|
|
program
|
|
.command('keygen')
|
|
.description('\tgenerate a key for remote access (./.dpd/keys.json)')
|
|
.action(function() {
|
|
var Keys = require('../lib/keys')
|
|
, keys = new Keys();
|
|
|
|
keys.create(function(err, key) {
|
|
if(err) return console.error(err);
|
|
console.log('created key', key.substr(0, 16) + '...');
|
|
});
|
|
});
|
|
|
|
program
|
|
.command('remote [host]')
|
|
.description('\tconnect to a remote host using a key in the current directory (./.dpd/keys.json)\n' +
|
|
'\tthe key must be present on the remote server to connect')
|
|
.action(function(host) {
|
|
remote.createRemote(host, program.port || 3000);
|
|
})
|
|
|
|
program
|
|
.command('*')
|
|
.description('\t[default] start the server in the current project in development mode\n' +
|
|
'\twith an interactive shell/repl for interacting with the running server\n' +
|
|
'\te.g. dpd (starts server in current directory),\n' +
|
|
'\t dpd my-app/app.dpd (starts app from file)')
|
|
.action(start);
|
|
|
|
|
|
function stop(code) {
|
|
var fn = function() {
|
|
exit(code);
|
|
};
|
|
|
|
if (program.wait) {
|
|
process.stdin.resume();
|
|
process.stdin.setRawMode(true);
|
|
process.stdout.write('\nPress any key to continue...\n');
|
|
process.stdin.on('keypress', fn);
|
|
} else {
|
|
fn();
|
|
}
|
|
}
|
|
|
|
process.on('uncaughtException', function(err) {
|
|
console.log(err);
|
|
console.log(err.stack);
|
|
stop(1);
|
|
});
|
|
|
|
|
|
/**
|
|
* Parse the arguments
|
|
*/
|
|
|
|
program.parse(process.argv);
|
|
|
|
if(program.args.length === 0) start();
|
|
|
|
/**
|
|
* Port generation
|
|
*/
|
|
|
|
function generatePort() {
|
|
var portRange = [ 3000, 9000 ];
|
|
return Math.floor(Math.random() * (portRange[1] - portRange[0])) + portRange[0];
|
|
}
|
|
|
|
function checkForUpdates() {
|
|
request('http://registry.npmjs.org/deployd', function(err, res, body) {
|
|
if (!err) {
|
|
var json;
|
|
try {
|
|
json = JSON.parse(body);
|
|
} catch (ex) {}
|
|
|
|
if (json && json['dist-tags'] && json['dist-tags'].latest) {
|
|
var latest = json['dist-tags'].latest;
|
|
fs.writeFile(latestversionFile, latest);
|
|
}
|
|
}
|
|
});
|
|
} |