Skip to content

Commit af8fe24

Browse files
NicolasRitouetandreialecu
authored andcommitted
Refactor: extract all commands from bin file (#2)
* Chore: Use next version of deployd Core * Extract commands into their own modules * Fix issue with createserver * Fix: resources listing under repl * Remove latestversion file
1 parent f6c017d commit af8fe24

File tree

18 files changed

+373
-315
lines changed

18 files changed

+373
-315
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/node_modules
2-
/.latestversion
2+
.latestversion
33
npm-debug.log

bin/dpd

100644100755
Lines changed: 14 additions & 297 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
#!/usr/bin/env node
22

33
/**
4-
* Dependencies
4+
* External dependencies
55
*/
66

7-
var program = require('commander')
8-
, repl = require('../lib/client/repl')
9-
, shelljs = require('shelljs/global')
10-
, mongod = require('../lib/util/mongod')
11-
, path = require('path')
12-
, fs = require('fs')
13-
, tty = require('tty')
14-
, request = require('request')
15-
, packageInfo = require('../package')
16-
, latestversionFile = path.join(__dirname, '../.latestversion')
17-
, Step = require('step')
18-
, open = require('../lib/util/open')
19-
, semver = require('semver')
20-
, resolve = require('resolve').sync
21-
, upgrade = require('doh').upgrade
22-
, exec = require('child_process').exec;
7+
var cli = require('../lib/cli');
238

24-
var basedir = process.cwd();
25-
26-
/**
27-
* Get the version number from the package.json
28-
*/
299

30-
program
10+
cli.program
3111
.version(require('../package').version)
3212
.option('-m, --mongod [path]', 'path to mongod executable (defaults to `mongod`)')
3313
.option('-p, --port [port]', 'port to host server (defaults to 2403)')
@@ -39,300 +19,37 @@ program
3919
.option('-P, --mongoPort [mongoPort]', 'mongodb port to connect to')
4020
.option('-n, --dbname [dbname]', 'name of the mongo database')
4121
.option('-a, --auth', 'prompts for mongo server credentials')
42-
.option(' --deploydPath [deploydPath]', 'allow overriding the path to deployd main script')
43-
;
44-
45-
/**
46-
* Commands
47-
*/
22+
.option(' --deploydPath [deploydPath]', 'allow overriding the path to deployd main script');
4823

49-
program
24+
cli.program
5025
.command('create [project-name]')
5126
.description('\tcreate a project in a new directory\n\teg. `dpd create my-app`')
52-
.action(function(name) {
53-
name = name || 'my-deployd-app';
54-
if (test('-d', name)) {
55-
return console.info(name + " already exists in this directory");
56-
}
57-
58-
mkdir('-p', name);
59-
cp('-Rf', path.join(__dirname, '/createtemplate/*'), name);
60-
mkdir('-p', name + '/.dpd');
61-
mkdir('-p', name + '/.dpd/pids');
62-
('').to(name + '/.dpd/pids/mongod');
63-
rm(ls('-R', name).filter(function(p) {
64-
return path.basename(p) === 'PLACEHOLDER';
65-
}).map(function(p) { return name + '/' + p}));
66-
67-
process.chdir(name);
68-
69-
console.log('Running npm install... please wait')
70-
child = exec('npm install',
71-
function (error, stdout, stderr) {
72-
if (error !== null) {
73-
console.log(stderr);
74-
console.log('npm install error: ' + error);
75-
} else {
76-
if (program.dashboard || program.open) {
77-
start(name + '/app.dpd');
78-
} else {
79-
console.info('to start your app:');
80-
console.info('\t$ cd', name);
81-
console.info('\t$ dpd');
82-
}
83-
}
84-
});
85-
86-
});
87-
88-
function start(file) {
89-
var port = program.port
90-
, host = program.host || '127.0.0.1'
91-
, dbname = program.dbname || '-deployd'
92-
, mongoPort = generatePort()
93-
, env = program.environment || process.env.DPD_ENV || 'development'
94-
, retries = 0
95-
, credentials
96-
;
97-
98-
if (!port) {
99-
port = 2403;
100-
retries = env === 'development' && 5;
101-
}
102-
103-
if (program.mongoPort) {
104-
mongoPort = Number(program.mongoPort);
105-
}
106-
107-
if (file) {
108-
process.chdir(path.dirname(file));
109-
}
110-
if (test('-f', 'app.dpd')) {
111-
console.log("deployd CLI version " + packageInfo.version);
112-
console.log("starting deployd");
113-
114-
if (fs.existsSync(latestversionFile)) {
115-
var latest = fs.readFileSync(latestversionFile, 'utf-8');
116-
if (latest && semver.gt(latest, packageInfo.version)) {
117-
console.log("deployd CLI v" + latest + " is available.");
118-
console.log();
119-
}
120-
}
121-
checkForUpdates();
122-
123-
if (!test('-d', './.dpd')) mkdir('-p', './.dpd');
124-
if (!test('-d', './.dpd/pids')) mkdir('-p', './.dpd/pids');
125-
if (!test('-d', './data')) mkdir('-p', './data');
126-
127-
if (program.auth && program.host === undefined) {
128-
console.error("Authentication requires the '-h' host flag... exiting.");
129-
process.exit();
130-
}
131-
132-
if (program.host) {
133-
if (program.auth) {
134-
Step(function () {
135-
var next = this;
136-
credentials = {};
137-
program.prompt('username: ', function(username){
138-
if (username && username !== '') {
139-
credentials.username = username;
140-
next();
141-
} else {
142-
console.error('Username cannot be blank.');
143-
process.exit();
144-
}
145-
});
146-
},
147-
function () {
148-
var next = this;
149-
program.password('Password: ', function(pass){
150-
if (pass && pass !== '') {
151-
credentials.password = pass;
152-
next();
153-
} else {
154-
console.error('Password cannot be blank.');
155-
process.exit();
156-
}
157-
});
158-
},
159-
startup
160-
);
161-
} else {
162-
startup();
163-
}
164-
} else {
165-
mongod.restart(program.mongod || 'mongod', env, mongoPort, startup);
166-
}
167-
168-
} else {
169-
console.log("This directory does not contain a Deployd app!");
170-
console.log("Use \"dpd create <appname>\" to create a new app");
171-
console.log("or use \"dpd path/to/app.dpd\" to start an app in another directory");
172-
stop(1);
173-
}
174-
175-
function startup (err) {
176-
if (err) {
177-
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)");
178-
return stop(1);
179-
}
180-
181-
var options = {port: port, env: 'development', db: {host: host, port: mongoPort, name: dbname}};
27+
.action(cli.create);
18228

183-
options.env = program.environment || process.env.DPD_ENV || options.env;
184-
if(options.env !== 'development') console.log('starting in %s mode', options.env);
185-
186-
if(credentials !== undefined) options.db.credentials = credentials;
187-
var dpd = createServer(options);
188-
dpd.on('listening', onListening);
189-
dpd.on('error', onError);
190-
dpd.listen();
191-
dpd.deploydPath = program.deploydPath;
192-
193-
function onListening () {
194-
console.info('listening on port', options.port);
195-
var commands = repl(dpd);
196-
if (program.dashboard) {
197-
commands.dashboard();
198-
} else if (program.open) {
199-
commands.open();
200-
}
201-
}
202-
203-
function onError (err) {
204-
if (err.code === "EADDRINUSE") {
205-
console.error();
206-
console.error("ERROR: port " + options.port + " is already in use");
207-
if (retries > 0) {
208-
options.port++;
209-
console.log("Trying again on port " + options.port + "...");
210-
console.log();
211-
retries--;
212-
dpd = createServer(options);
213-
dpd.on('listening', onListening);
214-
dpd.on('error', onError);
215-
dpd.listen();
216-
} else {
217-
process.exit();
218-
}
219-
} else {
220-
console.error(err);
221-
process.exit();
222-
}
223-
}
224-
}
225-
}
226-
227-
228-
program
29+
cli.program
22930
.command('keygen')
23031
.description('\tgenerate a key for remote access (./.dpd/keys.json)')
231-
.action(function() {
232-
var Keys = require('../lib/keys')
233-
, keys = new Keys();
234-
235-
keys.create(function(err, key) {
236-
if(err) return console.error(err);
237-
console.log('created key', key.substr(0, 16) + '...');
238-
});
239-
});
32+
.action(cli.keygen);
24033

241-
program
34+
cli.program
24235
.command('showkey')
24336
.description('\tshows current key for connecting to remote dashboard (./.dpd/keys.json)')
244-
.action(function() {
245-
var Keys = require('../lib/keys')
246-
, keys = new Keys();
37+
.action(cli.showkey);
24738

248-
keys.getLocal(function(err, key) {
249-
if(err) return console.error(err);
250-
if(!key) {
251-
console.log('No key file found. Run the following to create one:');
252-
console.log();
253-
console.log('dpd keygen');
254-
console.log();
255-
return;
256-
}
257-
console.log("Copy this key for use in remote dashboard");
258-
console.log();
259-
console.log(key);
260-
console.log();
261-
});
262-
});
26339

264-
265-
program
40+
cli.program
26641
.command('*')
26742
.description('\t[default] start the server in the current project in development mode\n' +
26843
'\twith an interactive shell/repl for interacting with the running server\n' +
26944
'\te.g. dpd (starts server in current directory),\n' +
27045
'\t dpd my-app/app.dpd (starts app from file)')
271-
.action(start);
272-
46+
.action(cli.start);
27347

274-
function stop(code) {
275-
var fn = function() {
276-
exit(code);
277-
};
278-
279-
if (program.wait) {
280-
process.stdin.resume();
281-
process.stdin.setRawMode(true);
282-
process.stdout.write('\nPress any key to continue...\n');
283-
process.stdin.on('keypress', fn);
284-
} else {
285-
fn();
286-
}
287-
}
28848

28949
/**
29050
* Parse the arguments
29151
*/
29252

293-
program.parse(process.argv);
294-
295-
if(program.args.length === 0) start();
296-
297-
/**
298-
* Port generation
299-
*/
300-
301-
function generatePort() {
302-
var portRange = [ 3000, 9000 ];
303-
return Math.floor(Math.random() * (portRange[1] - portRange[0])) + portRange[0];
304-
}
305-
306-
function checkForUpdates() {
307-
request('http://registry.npmjs.org/deployd-cli', function(err, res, body) {
308-
if (!err) {
309-
var json;
310-
try {
311-
json = JSON.parse(body);
312-
} catch (ex) {}
313-
314-
if (json && json['dist-tags'] && json['dist-tags'].latest) {
315-
var latest = json['dist-tags'].latest;
316-
fs.writeFile(latestversionFile, latest);
317-
}
318-
}
319-
});
320-
}
321-
322-
function createServer(config) {
323-
var deploydpath;
324-
try {
325-
deploydpath = program.deploydPath || resolve('deployd', {basedir: process.cwd()});
326-
} catch (e) {
327-
console.log('Unable to find local deployd');
328-
process.exit(99);
329-
}
330-
331-
var Server = require(deploydpath);
332-
333-
var server = new Server(config);
334-
server.options = config;
335-
upgrade(server);
53+
cli.program.parse(process.argv);
33654

337-
return server;
338-
}
55+
if (cli.program.args.length === 0) cli.start();

bin/dpdebug

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)