Only install node/npm/bower if not installed globally

This commit is contained in:
DamianEdwards 2014-07-08 12:43:56 -07:00
parent f290ec014f
commit 861e3df43b
5 changed files with 43 additions and 7 deletions

View File

@ -1,9 +1,13 @@
default currentDir = '${Directory.GetCurrentDirectory()}'
default nodeDir = '${Path.Combine(currentDir, "bin", "nodejs")}'
var bowerInstalled = '${Directory.Exists(Path.Combine(nodeDir, "node_modules", "bower"))}'
- // Install bower locally
npm npmCommand='install --prefix ${nodeDir} bower' if='!bowerInstalled' once='installBower'
var bowerGloballyInstalled = '${TestCommand("bower --version")}'
var bowerInstalled = '${Directory.Exists(Path.Combine(nodeDir, "node_modules", "bower"))}'
var bowerCmd = '${ bowerGloballyInstalled ? "bower" : Path.Combine(nodeDir, "node_modules", "bower", "bin", "bower") }'
- // Install bower locally if not already installed either globally or locally
npm npmCommand='install --prefix ${nodeDir} bower' if='!(bowerGloballyInstalled || bowerInstalled)' once='installBower'
- // Run bower
node nodeCommand='${Path.Combine(nodeDir, "node_modules", "bower", "bin", "bower")} ${bowerCommand}' workingdir='${bowerDir}'
exec program='${bowerCmd}' commandline='${bowerCommand}' if='bowerGloballyInstalled'
node nodeCommand='${bowerCmd} ${bowerCommand}' workingdir='${bowerDir}' if='!bowerGloballyInstalled'

View File

@ -115,6 +115,33 @@ functions @{
.Select(p => Path.GetDirectoryName(p))
.Distinct();
}
bool TestCommand(string program, string commandline = null) {
// Tests whether a given command succeeds at the command line.
// Useful for testing whether a given command is installed and on the path, e.g. node
var processStartInfo = new ProcessStartInfo {
UseShellExecute = false,
WorkingDirectory = Directory.GetCurrentDirectory(),
FileName = program,
Arguments = commandline,
};
try {
Log.Info(string.Format("Testing for command: {0} {1}", program, commandline));
var process = Process.Start(processStartInfo);
process.WaitForExit();
if (process.ExitCode == 0) {
Log.Info(" command found");
return true;
}
else {
Log.Warn(" command not found");
return false;
}
}
catch (Exception) {
Log.Warn(" command not found");
return false;
}
}
}
macro name='Exec' program='string' commandline='string'

View File

@ -10,13 +10,14 @@ default nodeVer = '0.10.28'
default npmVer = '1.4.9'
default nodeExeSha = '628FFD6C3577068C00CEC9F6F897F0EC8F5212D9'
default nodeInstallDir = '${Path.Combine(binDir, "nodejs")}'
default nodeGloballyInstalled = '${TestCommand("node --version")}'
var nodeExe = 'node.exe'
var npmZip = 'npm-${npmVer}.zip'
var nodeDist = 'http://nodejs.org/dist/'
var nodeUrl = '${nodeDist}v${nodeVer}/${nodeExe}'
var npmUrl = '${nodeDist}npm/${npmZip}'
var doInstall = '${!File.Exists(nodeInstallDir)}'
var doInstall = '${!nodeGloballyInstalled && !Directory.Exists(nodeInstallDir)}'
var nodeInstallExePath = '${Path.Combine(nodeInstallDir, nodeExe)}'
var npmInstallZipPath = '${Path.Combine(nodeInstallDir, npmZip)}'

View File

@ -1,6 +1,8 @@
default currentDir = '${Directory.GetCurrentDirectory()}'
default nodeDir = '${Path.Combine(currentDir, "bin", "node")}'
var nodeExePath = '${Path.Combine(nodeDir, "node.exe")}'
default nodeGloballyInstalled = '${TestCommand("node --version")}'
var nodeExePath = '${ nodeGloballyInstalled ? "node" : Path.Combine(nodeDir, "node.exe") }'
node-install once='installNode'
exec program='${nodeExePath}' commandline='${nodeCommand}'

View File

@ -1,7 +1,9 @@
default currentDir = '${Directory.GetCurrentDirectory()}'
default nodeDir = '${Path.Combine(currentDir, "bin", "nodejs")}'
default npmDir = '${currentDir}'
var npmCmd = '${Path.Combine(nodeDir, "npm.cmd")}'
var npmGloballyInstalled = '${TestCommand("npm --version")}'
var npmCmd = '${ npmGloballyInstalled ? "npm" : Path.Combine(nodeDir, "npm.cmd") }'
node-install once='installNode'
exec program='${npmCmd}' commandline='${npmCommand}' workingdir='${npmDir}'