Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Hyder committed Dec 7, 2014
2 parents f7273aa + c64550b commit 9fbae95
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 147 deletions.
164 changes: 87 additions & 77 deletions lib/InstallService.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,117 @@

//dependencies
var process = require('process');
var path = require('path');
var util = require('util');
var fs = require('fs');

var InstallService = function(){};

InstallService.promptSchema = Object.freeze({
properties: {
siteName: {
description: 'Site Name',
default: 'My PencilBlue Site',
type: 'string'
},
siteRoot: {
description: 'Site Root',
default: 'http://localhost:8080',
type: 'string'
},
siteIP: {
description: 'Address to bind to',
default: '0.0.0.0',
type: 'string'
},
sitePort: {
description: 'Site Port',
default: 8080,
type: 'number'
},
mongoServer: {
description: 'MongoDB URL',
default: 'mongodb://127.0.0.1:27017/',
type: 'string'
},
dbName: {
description: 'Database Name',
default: 'pencilblue',
type: 'string'
},
bower: {
description: 'Do you want to install Bower components?',
default: 'y/N',
type: 'string'
}
}
});

InstallService.prototype.install = function(directory) {
if(!shell.which('git')) {
console.log('In order to install PencilBlue, Git must first be installed.');
shell.exit(1);
return;
}

var promptSchema = {
properties: {
siteName: {
description: 'site name',
default: 'My PencilBlue Site',
type: 'string'
},
siteRoot: {
description: 'site root',
default: 'http://localhost:8080',
type: 'string'
},
siteIP: {
description: 'site IP',
default: 'localhost',
type: 'string'
},
sitePort: {
description: 'site port',
default: 8080,
type: 'number'
},
mongoServer: {
description: 'MongoDB URL',
default: 'mongodb://127.0.0.1:27017/',
type: 'string'
},
dbName: {
description: 'database name',
default: 'pencilblue',
type: 'string'
},
bower: {
description: 'Do you want to install Bower components?',
default: 'y/N',
type: 'string'
}
}
};

//prompt with options
prompt.message = "PencilBlue".cyan;

prompt.start();
prompt.get(promptSchema, function(err, siteSettings) {
prompt.get(InstallService.promptSchema, function(err, siteSettings) {
if(err) {
throw err;
}

console.log('Installing PencilBlue to ' + directory + '/...');
//clone the repo
var installDir = path.join(process.cwd(), directory);
console.log('Installing PencilBlue to %s/...', installDir);
shell.exec('git clone https://github.com/pencilblue/pencilblue.git ' + directory);
shell.cd(directory);


//change down into installation directory to perform dependency install
console.log('Installing Node modules...');
shell.cd(directory);
shell.exec('npm install');

//verify bower settings
if(siteSettings.bower.toLowerCase() === 'y' || siteSettings.bower.toLowerCase() === 'yes') {
if(shell.which('bower')) {
console.log('Retrieving Bower components...');
shell.exec('bower install');
}
else {

var bowerComponentInstallCmd = 'bower install';
if(!shell.which('bower')) {
console.log('Installing Bower...');
shell.exec('npm install bower');

console.log('Retrieving Bower components...');
shell.exec('node_modules/.bin/bower install');
//set the local bower command
bowerComponentInstallCmd = 'node_modules/.bin/bower install';
}

//install bower components
console.log('Retrieving Bower components...');
shell.exec(bowerComponentInstallCmd);
}

console.log('Creating config.json...');

fs.readFile(process.cwd() + '/sample.config.json', function(err, data) {
if(err) {
throw err;
}

var config = JSON.parse(data);
delete config.CAN_BE_REMOVED;
config.siteName = siteSettings.siteName;
config.siteRoot = siteSettings.siteRoot;
config.siteIP = siteSettings.siteIP;
config.sitePort = siteSettings.sitePort;
config.db.servers[0] = siteSettings.mongoServer;
config.db.name = siteSettings.dbName;

fs.writeFile(process.cwd() + '/config.json', JSON.stringify(config, null, " "), function(err) {
if(err) {
throw err;
}
//create configuration. Step 1 is to read in the export from the sample configuration file
console.log('Creating config.js...');
var sampleFile = path.join(process.cwd(), '/sample.config.js')
var config = require(sampleFile);

//step 2 is to replace the properties
config.siteName = siteSettings.siteName;
config.siteRoot = siteSettings.siteRoot;
config.siteIP = siteSettings.siteIP;
config.sitePort = siteSettings.sitePort;
config.db.servers[0] = siteSettings.mongoServer;
config.db.name = siteSettings.dbName;

//step 3 is magic. We use a regex pattern to find the object exported
//by the "module.exports" statement. We replace the match with the
//serialized config object we set properties on in step 2.
var replacement = 'module.exports = ' + JSON.stringify(config, null, 4) + ';\n';
var configStr = fs.readFileSync(sampleFile).toString().replace(/module\.exports[.\s\S]*\};/, replacement);

console.log('PencilBlue successfully installed.');
shell.exit(1);
});
});
//step 4 we write the configuration to disk
fs.writeFileSync(path.join(process.cwd(), '/config.js'), configStr);
});
};

//exports
module.exports = InstallService;
183 changes: 116 additions & 67 deletions lib/LaunchService.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,124 @@

//dependencies
var shell = require('shelljs');
var prompt = require('prompt');
var util = require('util');
var process = require('process');
var path = require('path');
var fs = require('fs');

/**
*
* @class LaunchService
* @constructor
*/
var LaunchService = function(){};

LaunchService.prototype.startPB = function(method) {
fs.exists(process.cwd() + '/pencilblue.js', function(exists) {
if(!exists) {
console.log('You must be in the root directory of a PencilBlue installation to run the start command.');
return;
}
var exists = fs.existsSync(path.join(process.cwd(), '/pencilblue.js'));
if(!exists) {
console.error('You must be in the root directory of a PencilBlue installation to run the start command.');
process.exit(1);
}

//check for default case where no method is provided
method = method || 'node';

//launch pencilblue
LaunchService.launchByMethod(method, function(err, result) {
if (util.isError(err)) {
console.error(err);
process.exit(1);
}
else if (result.code != 0) {
console.error(util.format('Failed to start PencilBlue: %j', result));
process.exit(1);
}

//all good so we can kill our selves off
console.log('Successfully kicked off PencilBlue');
process.exit(0);
});
};

LaunchService.launchByMethod = function(method, cb) {

//verify method is supported
var launchFunction = LAUNCH_TYPES[method];
if (!launchFunction) {
return cb(new Error(util.format('Startup method [%s] is not supported.', method)), false);
}

//verify that the method is installed
if (LaunchService.isInstalled(method)) {
return cb(null, launchFunction());
}
else if (method === 'node') {
return cb(new Error('Node is missing from your path!'));
}

//not installed so prompt user
LaunchService.promptInstall(method, function(err, doInstall) {
if (util.isError(err)) {
return cb(err);
}

//perform install
var installResult = LaunchService.installModule(method);
if (!installResult || installResult.code != 0) {
return cb(new Error('Failed to install module '+method));
}

//we got through the install now finally launch the service
cb(null, launchFunction());
});
};

LaunchService.launchViaNodemon = function() {
return shell.exec('nodemon pencilblue.js');
};

if(method) {
switch(method) {
case 'nodemon':
if (!shell.which('nodemon')) {
console.log('nodemon is not installed.');

var nodemonPromptSchema = {
properties: {
install: {
description: 'Will you like to install it now?',
default: 'y/N',
type: 'string'
}
}
}
prompt.start();

prompt.get(nodemonPromptSchema, function(err, nodemonSettings) {
if (err) {
throw err;
}

if (nodemonSettings.install.toLowerCase() === 'y' || nodemonSettings.install.toLowerCase() === 'yes') {
console.log('Installing Nodemon...');
shell.exec('npm install -g nodemon');
return;
} else {
return;
}
});
}
shell.exec('nodemon pencilblue.js');
return;
case 'node':
if(!shell.which('node')) {
console.log('node is not installed.');
return;
}
shell.exec('node pencilblue.js');
return;
case 'forever':
if(!shell.which('forever')) {
console.log('forever is not installed.');
return;
}
shell.exec('forever start pencilblue.js');
return;
default:
console.log('method not supported.');
return;
}
}
LaunchService.launchViaNode = function() {
return shell.exec('node pencilblue.js');
};

if(shell.which('nodemon')) {
shell.exec('nodemon pencilblue.js');
}
else if(shell.which('node')) {
shell.exec('node pencilblue.js');
}
else {
console.log('node is not installed.');
}
});
LaunchService.launchViaForever = function() {
return shell.exec('forever start pencilblue.js');
};

LaunchService.isInstalled = function(method) {
return shell.which(method) ? true : false;
};

LaunchService.promptInstall = function(method, cb) {
var nodemonPromptSchema = {
properties: {
install: {
description: util.format('%s is not installed. Would you like to install it now?', method),
default: 'y/N',
type: 'string'
}
}
};

prompt.start();
prompt.get(nodemonPromptSchema, function(err, nodemonSettings) {
if (util.isError(err)) {
return cb(err);
}
cb(null, nodemonSettings.install.toLowerCase() === 'y' || nodemonSettings.install.toLowerCase() === 'yes');
});
};

LaunchService.installModule = function(method) {
console.log('Attempting to install %s', method);
return shell.exec('npm install -g ' + method);
};

var LAUNCH_TYPES = Object.freeze({
nodemon: LaunchService.launchViaNodemon,
node: LaunchService.launchViaNode,
forever: LaunchService.launchViaForever
});

module.exports = LaunchService;
Loading

0 comments on commit 9fbae95

Please sign in to comment.