Skip to content

Commit

Permalink
Merge pull request #18 from pencilblue/story/DynamicConfig
Browse files Browse the repository at this point in the history
Updated the cli to produce a config.js file instead of json file
  • Loading branch information
brianhyder committed Dec 7, 2014
2 parents 2ca5fe7 + e7d043f commit c64550b
Showing 1 changed file with 87 additions and 77 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;

0 comments on commit c64550b

Please sign in to comment.