Skip to content

Commit

Permalink
Merge pull request #5 from christopheranderson/dev
Browse files Browse the repository at this point in the history
Merge Dev for 0.2.0-alpha update
  • Loading branch information
Christopher Anderson committed Jan 15, 2016
2 parents 9665064 + 23e43f7 commit c9a2b7f
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 175 deletions.
28 changes: 14 additions & 14 deletions lib/run-demeteorizer.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
var demeteorizer = require('demeteorizer'),
_ = require('underscore'),
path = require('path'),
promise = require('bluebird'),
logger = require('./logger');
_ = require('underscore'),
path = require('path'),
promise = require('bluebird'),
logger = require('./logger');

demeteorizer = promise.promisify(demeteorizer);

module.exports = {
run: function(options){
run: function (options) {
// Fill in options with defaults
options = _.defaults(options || {}, {
var meteorOptions = _.defaults(options.meteor || {}, {
debug: false,
input: process.cwd(),
directory: path.join(process.cwd(), '.demeteorized'),
json: {},
architecture: null
});
logger.info('run-demeteorizer','Starting Demeteorizer');
logger.verbose('run-demeteorizer','Options: ' + JSON.stringify(options));

logger.info('run-demeteorizer', 'Starting Demeteorizer');
logger.verbose('run-demeteorizer', 'Options: ' + JSON.stringify(meteorOptions));

// Run demeteorizer, which disassembles our meteor app into a node app.
return demeteorizer(options)
.then(function() {
return demeteorizer(meteorOptions)
.then(function () {
logger.info('run-demeteorizer', 'Demeteorization Finished');
}).catch(function(err){
}).catch(function (err) {
logger.error('run-demeteorizer', 'Demeteorizer encountered an error');
return promise.reject(err);
});
}
}
}
18 changes: 9 additions & 9 deletions lib/run-deploy.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var fs = require('fs'),
_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
request = require('request');

module.exports = {
run: function(options) {
return new Promise(function(resolve, reject){
run: function (options) {
return new Promise(function (resolve, reject) {
var fileToStream = path.join(process.cwd(), '.demeteorized/bundle.zip');
var fileSize = fs.statSync(fileToStream).size;

var status = logger.newStream('Uploading zip...', fileSize);

// Read the .zip file into the request
Expand All @@ -21,11 +21,11 @@ module.exports = {
// https://github.com/projectkudu/kudu/wiki/REST-API#zip
.put('https://' + options.siteName + '.scm.azurewebsites.net/api/zip/site/wwwroot')
.auth(options.username, options.password, true))
.on('error', function(err) {
.on('error', function (err) {
logger.error('run-deploy', 'Error while uploading zip');
reject(err);
})
.on('end', function() {
.on('end', function () {
logger.info('run-deploy', 'Successfully uploaded zip');
resolve();
});
Expand Down
110 changes: 58 additions & 52 deletions lib/run-install.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,99 @@
var fs = require('fs'),
_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
spawn = require('child_process').spawn;

_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
spawn = require('child_process').spawn,
assert = require('assert');

var stat = Promise.promisify(fs.stat);

var pathToServer = './.demeteorized/bundle/programs/server';

module.exports = {
run: function() {
logger.info('run-install','Starting Installation');
return new Promise(function(resolve, reject) {
run: function (options) {
logger.info('run-install', 'Starting Installation');
return new Promise(function (resolve, reject) {
// Check that the file path exists
stat(pathToServer)
.catch(reject)
.then(function() {
logger.verbose('run-install','Changing directory to ', pathToServer);
process.chdir(pathToServer);
})
// Installs npm packages required by demeteorized app
.then(install)
.catch(reject)
// Copy the program.json file to the bundle directory (because meteor expects our Node app to start in the server folder...)
.then(copyProgramsJson)
.catch(reject)
.then(function(){
// Jump down to bundle
// TODO: this is pretty hacky
logger.verbose('run-install','Changing directory to ', path.join(pathToServer, '../..'));
process.chdir('../..');
})
// Copy the Web.config file into the root directory so IIS Node will use "main.js"
.then(copyWebConfig)
.catch(reject)
.then(function() {
// Return to where we started
// TODO: this is pretty hacky too
logger.verbose('run-install','Changing directory to ', path.join(pathToServer, '../../../..'))
process.chdir('../..');
resolve();
});
.catch(reject)
.then(function () {
logger.verbose('run-install', 'Changing directory to ', pathToServer);
process.chdir(pathToServer);
})
// Installs npm packages required by demeteorized app
.then(install)
.catch(reject)
// Copy the program.json file to the bundle directory (because meteor expects our Node app to start in the server folder...)
.then(copyProgramsJson)
.catch(reject)
.then(function () {
// Jump down to bundle
// TODO: this is pretty hacky
logger.verbose('run-install', 'Changing directory to ', path.join(pathToServer, '../..'));
process.chdir('../..');
return options.pathToWebConfig;
})
// Copy the Web.config file into the root directory so IIS Node will use "main.js"
.then(copyWebConfig)
.catch(reject)
.then(function () {
// Return to where we started
// TODO: this is pretty hacky too
logger.verbose('run-install', 'Changing directory to ', path.join(pathToServer, '../../../..'))
process.chdir('../..');
resolve();
});
});
}
}
}

var install = function() {
return new Promise(function(resolve, reject) {
var install = function () {
return new Promise(function (resolve, reject) {
// Run npm install
logger.verbose('run-install','Installing packages for server');
logger.verbose('run-install', 'Installing packages for server');
var isWin = /^win/.test(process.platform);
var child = spawn(isWin ? 'cmd' : 'sh', [isWin?'/c':'-c', 'npm', 'install']);
var child = spawn(isWin ? 'cmd' : 'sh', [isWin ? '/c' : '-c', 'npm', 'install']);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('error', function(err) {
child.on('error', function (err) {
logger.error('run-install', err);
reject(err);
});
child.on('exit', function(code) {
if(code != 0) return reject(new Error('npm install failed, see npm-debug.log for more details'));
child.on('exit', function (code) {
if (code != 0) return reject(new Error('npm install failed, see npm-debug.log for more details'));
resolve();
});
});
}

var copyFile = function(source, target) {
var copyFile = function (source, target) {
logger.verbose('run-install','Copying: ' + source + ' to: ' + target);
// Took some ideas from: http://stackoverflow.com/a/14387791/3662111
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
rd.on("error", function (err) {
reject(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
wr.on("error", function (err) {
reject(err);
});
wr.on("close", function(ex) {
wr.on("close", function (ex) {
resolve();
});
var status = logger.newStream('Moving file - ' + path.basename(source), fs.statSync(source).size);
rd.pipe(status).pipe(wr);
})
}

var copyWebConfig = function() {
return copyFile(path.join(__dirname, '../resources/web.config'), path.join(process.cwd(), 'web.config'));
var copyWebConfig = function (pathToWebConfig) {
if(pathToWebConfig) logger.verbose('run-install','Custom Web.config! Relative path to Web.config: ' + pathToWebConfig);
var _pathToWebConfig = pathToWebConfig || path.join(__dirname, '../resources/web.config');
assert(fs.existsSync(_pathToWebConfig), 'Error: The specified web.config does not exist: ' + _pathToWebConfig);
return copyFile(_pathToWebConfig, path.join(process.cwd(), 'web.config'));
}

var copyProgramsJson = function() {
var copyProgramsJson = function () {
return copyFile(path.join(process.cwd(), 'program.json'), path.join(process.cwd(), '../../program.json'));
}
77 changes: 77 additions & 0 deletions lib/run-precheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Promise = require('bluebird'),
logger = require('./logger'),
fs = require('fs'),
path = require('path'),
semver = require('semver');

var exec = Promise.promisify(require('child_process').exec);

var versionChecks = [{
name: 'node',
cmd: 'node -v',
versions: '0.10.40'
},
{
name: 'python',
cmd: 'python ' + path.join(__dirname, '../version.py'), // for some reason, python --version doesn't return on stdout
versions: '2.7.x'
},
{
name: 'meteor',
cmd: 'meteor --version',
versions: '>0.8.1 || 1.x'
}]

module.exports = {
run: function(options){
logger.level = 'silly';
return new Promise(function(resolve, reject){
Promise.all(versionChecks.map(function(check){
return validateVersion(check.name, check.cmd, check.versions);
}))
.catch(reject)
.then(checkForVCR)
.catch(reject)
.then(resolve);
});
}
}

var validateVersion = function(name, cmd, versions) {
return new Promise(function(resolve, reject) {
exec(cmd)
.catch(reject)
.then(function(result){
logger.silly(cmd + ' -> stdout: ' + result);
var matches = result.match(/(\d+\.\d+\.\d+)/g);
var curr = matches ? matches[0] : null;
if(curr && semver.satisfies(curr, versions)){
logger.verbose('run-precheck', name + ' has a valid version. Current version: ' + curr + ' Valid version(s): ' + versions);
resolve();
} else {
reject(name + ' has an invalid version. Current version: ' + curr + ' Valid version(s): ' + versions);
}
})
.then(resolve, reject);
})
}

var checkForVCR = function() {
return new Promise(function(resolve, reject) {
if(!process.env.windir) return resolve('Could not find windows directory');
fs.readdir(path.resolve(process.env.windir, 'System32'), function(err, dirs){
if(err) reject(err);
dirs = dirs.filter(function(i) {
if(i.indexOf('msvcr') > -1) {
return i;
}
});
if(dirs.length > 0) {
logger.verbose('run-precheck', 'Has a version of MSVCR installed');
return resolve();
} else {
return reject('You need to install a Microsoft Visual C++ Redistributable. Consult node-gyp for more information. Installation will likely fail.');
}
})
})
}
22 changes: 11 additions & 11 deletions lib/run-zip.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
var fs = require('fs'),
_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
_ = require('underscore'),
path = require('path'),
Promise = require('bluebird'),
logger = require('./logger'),
archiver = require('archiver');


module.exports = {
run: function() {
return new Promise(function(resolve, reject){
run: function () {
return new Promise(function (resolve, reject) {
logger.info('run-zip', 'Creating bundle.zip');

// Create output stream to zip file
var output = fs.createWriteStream(path.join(process.cwd(), '.demeteorized/bundle.zip'));
output.on('error', function(err){
output.on('error', function (err) {
logger.error('run-zip', 'Error while writing to bundle.zip');
reject(err);
});
output.on('close', function(){
output.on('close', function () {
logger.verbose('run-zip', archive.pointer() + ' total bytes');
logger.verbose('run-zip', 'bundle.zip finished being written to');
resolve();
});

var archive = archiver('zip', {});
logger.verbose('run-zip', 'Current Directory: ' + process.cwd());
logger.verbose('run-zip', 'Target Directory for bundle' + (path.join(process.cwd(), '.demeteorized/bundle')));

// Enqueues the bundle directory for file zipping
archive.directory(path.join(process.cwd(), '.demeteorized/bundle'), false);
archive.on('error', function(err) {
archive.on('error', function (err) {
logger.error('run-zip', 'Error while creating archive');
reject(err);
});
Expand All @@ -40,7 +40,7 @@ module.exports = {
archive.pipe(output);

// Signals archive that we're done queueing files
archive.finalize();
archive.finalize();
});
}
}
Loading

0 comments on commit c9a2b7f

Please sign in to comment.