diff --git a/README.md b/README.md index b46b793..6af1ef5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +__THIS PROJECT IS NO LONGER MAINTAINED. Please seek other alternatives.__ + +* the project cannot be updated on npm due to camelCased title +* i no longer do Windows +* both iojs and node.js v0.12 have execSync function +* older node users can try sync-exec from npm (suggested by one of the commentors) + # execSync Executes shell commands synchronously. @@ -8,10 +15,18 @@ production servers. ## Install __Windows__ requires Python and Visual Studio 2012 (Express) installed for -node to build. See [node-gyp installation](https://github.com/TooTallNate/node-gyp#installation) +node to build. See [node-gyp installation](https://github.com/TooTallNate/node-gyp#installation). +Pre-built binaries for node v0.8 and node v0.10 are packaged. They should work and if not try manually +building. npm install execSync +Sometimes a manual build is necessary on Windows even with all the tools in place, replace Visual Studio version +with '2010' or '2012' based on the version installed. + + npm install node-gyp -g + node-gyp rebuild --msvs_version=2012 + ## Usage Require it diff --git a/index.js b/index.js index 60384a0..57c0d4b 100644 --- a/index.js +++ b/index.js @@ -8,13 +8,24 @@ var fs = require('fs'); var isWindows = require('os').platform().indexOf('win') === 0; var shell; -if (isWindows && !fs.existsSync(__dirname + '/build/Release/shell.node')) { - try { - shell = require('./win32/shell'); - } catch (err) { - throw new Error('execSync incompatible with installed nodejs'); +if (isWindows) { + if (!fs.existsSync(__dirname + '/build/Release/shell.node')) { + var nodeVersion = process.version; + var shellLib = 'v0.8/shell'; + if (nodeVersion.indexOf('v0.10') === 0) + shellLib = 'v0.10/shell'; + + try { + //! Loading an add-on built for another version of nodeJs may + //! cause seg faults. Windows + open source is not always pleasant + shell = require('./win32/' + shellLib); + } catch (err) { + console.error(err); + throw new Error('execSync incompatible with installed nodejs'); + } } -} +} + if (!shell) { shell = require('./build/Release/shell'); } @@ -25,7 +36,7 @@ if (!shell) { function run(cmd) { try { if (isWindows) - cmd = 'cmd /C ' + cmd; + cmd = 'cmd /C ' + cmd; var code = shell.exec(cmd); return code; } catch (err) { diff --git a/install.js b/install.js new file mode 100644 index 0000000..4d78a5b --- /dev/null +++ b/install.js @@ -0,0 +1,28 @@ +var spawn = require('child_process').spawn; +var fs = require('fs'); +var version = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version; +var verbose = process.env['npm_package_config_verbose'] != null ? process.env['npm_package_config_verbose'] === 'true' : false; + +console.log('[execsync v%s] Attempting to compile native extensions.', version); + +var gyp = spawn('node-gyp', ['rebuild'], {cwd: __dirname}); +gyp.stdout.on('data', function(data) { + if (verbose) process.stdout.write(data); +}); +gyp.stderr.on('data', function(data) { + if (verbose) process.stdout.write(data); +}); +gyp.on('error', function(err) { + console.error(err); +}); +gyp.on('close', function(code) { + if (code !== 0) { + console.log("[execSync v%s]", version); + console.log(' Native code compile failed!!'); + if (require('os').platform().indexOf('win') === 0) { + console.log(' Will try to use win32 extension.'); + } + } else { + console.log('[execSync v%s] Native extension compilation successful!', version); + } +}); \ No newline at end of file diff --git a/package.json b/package.json index 6ae1756..a189a5d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Mario Gutierrez ", "name": "execSync", "description": "Node's missing execSync.", - "version": "1.0.1-pre", + "version": "1.0.4", "license": "MIT", "dependencies": { "temp": "~0.5.1" @@ -13,9 +13,14 @@ }, "optionalDependencies": {}, "engines": { - "node": "*" + "node": "<0.11" }, "scripts": { - "test": "mocha test" + "test": "mocha test/unixSpec", + "install": "node install.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/mgutz/execSync.git" } } diff --git a/test/unixSpec.js b/test/unixSpec.js index 49ee9df..e2ae045 100644 --- a/test/unixSpec.js +++ b/test/unixSpec.js @@ -12,7 +12,6 @@ describe('execSync', function() { it ('should perform git operations', function() { run('rm -rf tmp'); - run('mkdir -p tmp'); run('git clone git://github.com/mgutz/execSync tmp'); run('cd tmp'); run('git pull origin master') @@ -22,7 +21,7 @@ describe('execSync', function() { assert.equal(result.code, 0); }); - it ('should execute and get everything', function() { + it ('should capture stdout, stderr and exit code', function() { var result = sh.exec('echo my_bad 1>&2; echo foo; echo your_bad 1>&2; exit 42'); assert.equal(result.stdout, 'my_bad\nfoo\nyour_bad\n'); assert.equal(result.code, 42); diff --git a/win32/v0.10/shell.node b/win32/v0.10/shell.node new file mode 100644 index 0000000..f715c96 Binary files /dev/null and b/win32/v0.10/shell.node differ diff --git a/win32/shell.node b/win32/v0.8/shell.node similarity index 100% rename from win32/shell.node rename to win32/v0.8/shell.node