Skip to content

Commit

Permalink
Add in-code documentation & refactor set up version
Browse files Browse the repository at this point in the history
  • Loading branch information
Herman Junge committed Mar 4, 2013
1 parent b0f36d0 commit c620638
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 106 deletions.
59 changes: 39 additions & 20 deletions lib/common.js
@@ -1,23 +1,42 @@
var common = module.exports;
/**
* Common
*
* The Main object of the prey client.
* Contains Values and Objects usable both in
* Configuration as Operation
*
*/

var join = require('path').join,
root_path = process.env.ROOT_PATH || join(__dirname, '..'),
version = require(join(root_path, 'package')).version,
program = require('commander'),
system = require(join(root_path, 'lib', 'system'));
// Module Requirements
var join = require('path').join
, root_path = process.env.ROOT_PATH || join(__dirname, '..')
, version = require(join(root_path, 'package')).version
, program = require('commander');

var config_file = 'prey.conf',
default_config_file = join(root_path, config_file + '.default'),
config_path = program.path || system.paths.config,
config = require('getset').load(join(config_path, config_file)),
user_agent = "Prey/" + version + " (Node.js, " + system.os_name + ")";
// System object varies upon the machine is running (i.e. OSX, Linux, Windowsº)
var system = require(join(root_path, 'lib', 'system'));

common.root_path = root_path;
common.version = version;
common.program = program;
common.os_name = system.os_name;
common.system = system;
common.config = config;
common.config_path = config_path;
common.default_config_file = default_config_file;
common.user_agent = user_agent;
// Other Variables
var config_file = 'prey.conf'
, default_config_file = join(root_path, config_file + '.default')
, config_path = program.path || system.paths.config
, user_agent = "Prey/" + version + " (Node.js, " + system.os_name + ")";

// An instance of `getset`, this object has the ability of
// synchronize itself on `sync` function
var config = require('getset').load(join(config_path, config_file))

// Common object creation
var common = {
config : config
, config_path : config_path
, default_config_file : default_config_file
, os_name : system.os_name
, program : program
, root_path : root_path
, system : system
, user_agent : user_agent
, version : version
}

module.exports = common;
153 changes: 79 additions & 74 deletions lib/conf/cli.js
@@ -1,21 +1,21 @@
#!/usr/bin/env node

var fs = require('fs'),
util = require('util'),
path = require('path'),
dialog = require('dialog'),
common = require('./../common'),
config = common.config,
system = common.system,
paths = system.paths,
hooks = require('./' + common.os_name),
ensure_dir = require('./../utils/ensure_dir'),
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'),
var fs = require('fs'),
util = require('util'),
path = require('path'),
dialog = require('dialog'),
common = require('./../common'),
config = common.config,
system = common.system,
paths = system.paths,
hooks = require('./' + common.os_name),
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'),
versions_list;

var argv = process.argv.splice(3),
Operetta = require('./../utils/operetta').Operetta,
cli = new Operetta(argv);
// Set up Operetta (Command line parser)
var argv = process.argv.splice(3),
Operetta = require('./../utils/operetta').Operetta,
cli = new Operetta(argv);

var package = require('./package'),
versions = require('./versions'),
Expand All @@ -25,52 +25,68 @@ var package = require('./package'),
prompt = require('./prompt'),
settings = require('./settings');

var debugging = process.env.DEBUG;
////////////////////////////////////////////////
// logger functions
////////////////////////////////////////////////

var debug = function(str) {
if (debugging) log(str);
// Debug logger
var debugging = process.env.DEBUG
, debug = function (msg) {
if (debugging) log(msg);
}

var log = function(msg){
// Normal logger
var log = function (msg) {
if (typeof msg == 'object')
msg = util.inspect(msg);

if (process.stdout.writable)
process.stdout.write(msg.toString() + "\n");
}

// process.title = 'prey config';

////////////////////////////////////////////////
// ensure stuff
// module main functions
////////////////////////////////////////////////

// syncs config with the one of the current installation
ensure_config = function(cb){
config.sync(common.default_config_file, cb);
}

ensure_logfile = function(cb){
fs.writeFile(paths.log_file, '', cb);
}
/**
*
* Sets up config and keys, and if all goes well,
* sets version as the active/current one
*
*/
var setUpVersion = function (version, cb) {
setUpConfig(isSetConfig);
// If /etc/prey or C:\Windows\Prey doesn't exist, create it.
// Normally this path should be created by the installer
function setUpConfig () {
log('Ensuring presence of config dir: ' + paths.config);
ensureDir(paths.config, ensuredDir);
}

////////////////////////////////////////////////
// main functions
////////////////////////////////////////////////
function ensureDir (dirPath, callback) {
fs.exists(dirPath, function(exists) {
if (exists) return callback();
return fs.mkdir(dirPath, callback);
});
}

// sets up config and keys, and if all goes well,
// sets version as the active/current one
var set_up_version = function(version, cb){
set_up_config(function(err){
function ensuredDir (err) {
// An error here is likely to happen if we run this not being root
if (err) return cb(err);
// Copy or sync ROOT_PATH/prey.conf.default to /etc/prey/prey.conf
log('Syncing config with ' + common.default_config_file);
config.sync(common.default_config_file, isSetConfig);
}

if (!paths.versions) // no version support
return cb();

// create symlink to set as current version
function isSetConfig (err){
if (err) return cb(err);
// If we haven't version support (i.e. couldn't find `versions` dir),
// just return, we already synced.
if (!paths.versions) return cb();
// Create symlink to set as current version
log('Setting up ' + version + ' as current...');
versions.set_current(version, cb)
})
versions.set_current(version, cb);
}
}

var unset_current = function(cb){
Expand All @@ -80,26 +96,10 @@ var unset_current = function(cb){
});
}

var set_up_config = function(cb){

// if /etc/prey or C:\Windows\Prey doesnt exist, create it
// normally this path should be created by the installer
log('Ensuring presence of config dir: ' + paths.config);
ensure_dir(paths.config, function(err){
if (err) return cb(err);

// copy or sync ROOT_PATH/prey.conf.default to /etc/prey/prey.conf
log('Syncing config with ' + common.default_config_file);
ensure_config(cb);
});

}

// runs pre_uninstall on current installation, then
// calls 'prey config activate' on the new installation,
// so that it performs the activation using its own paths and logic.
// if it fails, roll back by removing it and setting interval again.

var activate_new_version = function(version, cb){

system.unset_interval(function(err){
Expand Down Expand Up @@ -225,29 +225,34 @@ var show_gui_and_exit = function(){
// command line arguments handler
////////////////////////////////////////////////

var run = function(cb) {
var run = function (cb) {

// called after the files have been copied.
// should be called from /usr/lib/prey/versions/1.5.2
cli.command('activate', 'Completes installation, sets up config and execution.', function(cmd){
cmd.options(['-g', '--gui'], 'Display GUI configurator when done.')
cmd.start(function(values){

if (process.env.BUNDLE_ONLY)
return cb();
/**
* @map [./bin/prey] config activate [-g]
*
* @summary Called after the files have been copied.
* should be called from /usr/lib/prey/versions/1.5.2
*/
cli.command( 'activate'
, 'Completes installation, sets up config and execution.', function (cmd) {
cmd.options(['-g', '--gui'], 'Display GUI configurator when done.');
cmd.start(function (values) {
if (process.env.BUNDLE_ONLY) return cb();

var show_gui = values['-g'] === true;

set_up_version('this', function(err){
if (err) return cb(err);
setUpVersion('this', isSetVersion);

function isSetVersion (err) {
if (err) return cb(err);
// if gui was requested but we had an error, return
system.set_interval(60, function(err){
if (err || !show_gui) return cb(err);
system.set_interval(60, isSetInterval);
}

show_gui_and_exit();
});
});
function isSetInterval (err){
if (err || !show_gui) return cb(err);
show_gui_and_exit();
}
});
});

Expand Down
12 changes: 0 additions & 12 deletions lib/utils/ensure_dir.js

This file was deleted.

0 comments on commit c620638

Please sign in to comment.