Skip to content

Instantly share code, notes, and snippets.

@tjanczuk
Created June 29, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjanczuk/3019587 to your computer and use it in GitHub Desktop.
Save tjanczuk/3019587 to your computer and use it in GitHub Desktop.
computeNodeJsVersion.js
var path = require('path')
, fs = require('fs');
var existsSync = fs.existsSync || path.existsSync;
var nodejsDir = path.resolve(process.env['programfiles(x86)'] || process.env['programfiles'], 'nodejs');
if (!existsSync(nodejsDir))
throw new Error('Unable to locate node.js installation directory at ' + nodejsDir);
var wwwroot = process.argv[2];
if (!existsSync(wwwroot))
throw new Error('Usage: node.exe computeNodeJsVersion.js <path_to_wwwroot>');
var packageJson = path.resolve(wwwroot, 'package.json');
if (!existsSync(packageJson)) {
console.log('The packge.json not found at ' + packageJson +'. No modifications performed.');
process.exit(0);
}
var json = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
if (typeof json !== 'object' || typeof json.engines !== 'object' || typeof json.engines.node !== 'string') {
console.log('The package.json at ' + packageJson
+ ' does not specify node.js engine constraints. No modifications performed.');
process.exit(0);
}
var iisnodeYml = path.resolve(wwwroot, 'iisnode.yml');
var yml = '';
if (existsSync(iisnodeYml)) {
yml = fs.readFileSync(iisnodeYml, 'utf8');
if (yml.match(/^ *nodeProcessCommandLine *:/m)) {
console.log('The iisnode.yml at ' + iisnodeYml
+ ' explicitly sets nodeProcessCommandLine. No modifications performed.');
process.exit(0);
}
}
// determine node.js versions available on the plarform
var versions = [];
fs.readdirSync(nodejsDir).forEach(function (dir) {
if (dir.match(/^\d+\.\d+\.\d+$/) && existsSync(path.resolve(nodejsDir, dir, 'node.exe')))
versions.push(dir);
});
// calculate actual node.js version to use for the application
var version = require('semver').maxSatisfying(versions, json.engines.node);
if (!version) {
console.log('None of the avilable node.js versions ' + JSON.stringify(versions)
+ ' matches the application\'s node.js version constraint ' + json.engines.node);
process.exit(-1);
}
else {
console.log('Selected node.js version ' + version +'.');
}
// save the version information to iisnode.yml
if (yml !== '')
yml += '\r\n';
yml += 'nodeProcessCommandLine: "' + path.resolve(nodejsDir, version, 'node.exe') + '" "' + path.resolve(nodejsDir, 'interceptor.js') + '"';
fs.writeFileSync(iisnodeYml, yml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment