tooltwist
Version:
Tooltwist Command Line Interface
295 lines (258 loc) • 8.92 kB
JavaScript
var os = require('os'),
fs = require('fs'),
Log = require('./log'),
Config = require('./config'),
exec = require('child_process').exec;
/**
* Directory to the imageMagick home directory
*/
exports.check = function(callback) {
Log.debug();
Log.debug('prerequisites.check()')
// See what operating system we are using.
// Currently Windows is not supported
// http://nodejs.org/api/os.html
Log.info();
Log.info('Checking the OS and Prerequisites.')
var platform = os.platform();
Log.debug('os.platform=', os.platform())
Log.debug('os.type=', os.type())
//platform = 'win32'
var isWin = /^win/.test(platform);
if (isWin) {
Log.error('\n\nFatal Error:')
Log.error('Sorry, Windows platforms are not supported by this command.\n')
return callback(false);
}
// // If a custom template directory was provided, check it exists
// Log.info();
// Log.info('See if there is a template override directory.')
// if (templatesDir) {
//
// // Check the template directory exists
// try {
// // Query the entry
// if ( !fs.lstatSync(templatesDir).isDirectory()) {
// Log.error('\n\nFatal Error:')
// Log.error('Template directory not found: ' + templatesDir + '\n')
// return callback(false);
// }
// Log.info('\nWhere available will use templates in ' + templatesDir)
// }
// catch (e) {
// Log.error('\nError: Could not access template directory: ' + templatesDir + '\n')
// return callback(false);
// }
// } else {
//
// // See if there is a 'tooltwist.templates' directory
// var defaultCustomTemplateDirectory = Config.TEMPLATES_DIR;
// try {
// // Query the entry
// if (fs.lstatSync(Config.TEMPLATES_DIR).isDirectory()) {
// Log.info();
// Log.info('Where available will use templates in ' + Config.TEMPLATES_DIR)
// _customTemplatesDir = Config.TEMPLATES_DIR;
// }
// }
// catch (e) {
// // Do nothing
// }
// }
// Start the browser for this operating system
// open('http://www.tooltwist.com')
// Check we have the required software installed
var missingExecutables = '';
var fatalError = false;
// Check the dependant packages
Log.info();
Log.info('Checking correct commands are available.')
checkNode();
// Node
function checkNode() {
Log.info(' test: node --version')
var code = exec('node --version', function(error, stdout, stderr) {
if (error !== null) {
Log.info(' [failed]')
Log.info('Failed to run node! How is this possible?')
process.exit(1);
}
// Check the version of Node
else if (stdout.search(/v(\d+\.\d+\.[\d_]+)/) >= 0) {
var REQUIRED_VERSION = '0.10'
var currentVersion = RegExp.$1
if ( !checkVersion(currentVersion, REQUIRED_VERSION)) {
Log.info(" [version: " + currentVersion + "]")
missingExecutables += "- Node version " + REQUIRED_VERSION + " or higher is required. Please update to a newer version.\n";
missingExecutables += " See http://nodejs.org\n";
fatalError = true;
}
// Failed to parse the output
} else {
Log.warn('\nWARNING: Cannot understand output of node --version:')
Log.warn(stdout + stderr)
Log.warn('(will assume we have the right version and continue)\n')
}
checkJava();
});
}
// Java
function checkJava() {
Log.info(' test: javac -version')
var code = exec('javac -version', function(error, stdout, stderr) {
if (error !== null) {
Log.info(' [failed]')
missingExecutables += "- Java JDK has not been installed (can't run \"javac -version\").\n";
missingExecutables += " See http://docs.oracle.com/javase/7/docs/webnotes/install\n";
missingExecutables += " or http://openjdk.java.net/install/\n";
fatalError = true;
// Check the version matching 'javac (<integer>.<integer>.<integerOrUnderscore>)'
} else if (stderr.search(/javac (\d+\.\d+\.[\d_]+)/) >= 0) {
var REQUIRED_VERSION = '1.7'
var currentVersion = RegExp.$1
if ( !checkVersion(currentVersion, REQUIRED_VERSION)) {
Log.info(" [version: " + currentVersion + "]")
missingExecutables += "- Java version " + REQUIRED_VERSION + " or higher is required. Please update to a newer version.\n";
missingExecutables += " See http://docs.oracle.com/javase/7/docs/webnotes/install";
missingExecutables += " or http://openjdk.java.net/install\n";
fatalError = true;
}
// Check for 'javac <integer>'
} else if (stdout.search(/javac (\d+)/) >= 0) {
var MINIMUM_VERSION = 7
var currentVersion = RegExp.$1
if ( !checkVersion(currentVersion, MINIMUM_VERSION)) {
Log.info(" [version: " + currentVersion + "]")
missingExecutables += "- Java version " + MINIMUM_VERSION + " or higher is required. Please update to a newer version.\n";
missingExecutables += " See http://docs.oracle.com/javase/7/docs/webnotes/install";
missingExecutables += " or http://openjdk.java.net/install\n";
fatalError = true;
}
// Failed to parse the output
} else {
Log.warn('\nWARNING: Cannot understand output of "javac -version":')
Log.warn(stdout + stderr)
Log.warn('(will assume we have the right version and continue)\n')
}
// Should check ImageMagic if prior to v8.4
//ZZZZZ
// checkImageMagick();
checkGit();
});
}
// ImageMagick
function checkImageMagick() {
Log.info(' test: convert -version')
var code = exec('convert -version', function(error, stdout, stderr) {
if (error !== null) {
Log.info(' [failed]')
missingExecutables += "- ImageMagick has not been installed (can't run \"convert -version\").\n";
missingExecutables += " See http://www.imagemagick.org\n";
checkGit();
return;
}
// Find the location of 'convert'
findPath('convert', function(path){
if (path == null) {
Log.error('Could not find path to "convert" command')
process.exit(1);
}
Log.debug('convert found at ' + path)
var suffix = '/bin/convert'
if ( !endsWith(path, suffix)) {
Log.error('Expected "convert" to be in bin directory: ' + path)
process.exit(1)
}
// Remember directory to use as $IMAGEMAGICK_HOME
var imageMagickHome = path.substring(0, path.length - suffix.length);
Config.setImageMagickHome(imageMagickHome);
checkGit();
});
});
}
// Git
function checkGit() {
Log.info(' test: git --version')
var code = exec('git --version', function(error, stdout, stderr) {
if (error !== null) {
Log.info(' [failed]')
missingExecutables += "- Git has not been installed (can't run \"git --version\").\n";
missingExecutables += " See http://git-scm.com/downloads\n";
missingExecutables += " and https://help.github.com/articles/set-up-git\n";
fatalError = true;
}
checkRuby();
});
}
// Ruby
function checkRuby() {
Log.info(' test: ruby --version')
var code = exec('ruby --version', function(error, stdout, stderr) {
if (error !== null) {
Log.info(' [failed]')
missingExecutables += "- Ruby has not been installed (can't run \"ruby --version\").\n";
missingExecutables += " See https://www.ruby-lang.org/en/installation\n";
}
finished();
});
}
function finished() {
Log.debug('missingExecutables=', missingExecutables)
Log.debug('fatalError=', fatalError)
if (missingExecutables == '') {
Log.info('Pre-requisites ok.')
Log.info();
return callback(true);
}
Log.error('\nError:\nPre-requisite software packages may be missing:\n');
Log.error(missingExecutables);
var isOk = !fatalError;
return callback(isOk);
}
/**
* Check that version is at least as high as major.minor
*/
function checkVersion(version, minimumVersion) {
// Get the minimum major.minor version numbers
var major, minor;
if (typeof(minimumVersion) === 'number') {
major = minimumVersion
minor = 0
} else if (minimumVersion.search(/^(\d+).(\d+)$/) >= 0) {
major = parseInt(RegExp.$1)
minor = parseInt(RegExp.$2)
} else {
throw new Error('Internal error: unrecognised minimumVersion: ' + minimumVersion)
}
// Check the version
if (version.search(/^(\d+).(\d+)([\.\d_]+)$/) >= 0) {
var v1 = parseInt(RegExp.$1);
var v2 = parseInt(RegExp.$2);
if (v1 < major || (v1 == major && v2 < minor)) {
return false;
}
return true;
} else if (version.search(/^(\d+)$/) >= 0) {
var v1 = parseInt(RegExp.$1);
if (v1 < major) {
return false;
}
return true;
} else {
throw new Error('Internal error: expected valid version number: ' + version)
}
}
};
/**
* Find the location of an executable
*/
function findPath(executable, callback) {
//ZZZZ
return callback('/usr/local/bin/convert')
}
/**
* See if a string ends with a particular suffix.
*/
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}