Permalink
Browse files

[doc][test] Docs and Tests!

  • Loading branch information...
1 parent dfd54a6 commit 4a4efc5de40fd2a62e1c9be4154ba9d11fa837b7 @bmeck committed Oct 17, 2012
View
@@ -13,10 +13,11 @@ var fs = require('fs');
// script.on('exit', process.exit.bind(process));
// });
//
-function getNPMSpawnOptions(dir, lifecycle, options, callback) {
+exports.getSpawnOptions = function getNPMSpawnOptions(dir, lifecycle, options, callback) {
//
// Find our root package directory, need to know this for various env stuff (path in particular)
//
+ options = options || {};
findPackage(dir, function (err, packageFile) {
if (err) {
callback(err, null);
@@ -42,7 +43,7 @@ function getNPMSpawnOptions(dir, lifecycle, options, callback) {
//
// Generate general options
//
- getBasicSpawnOptions(lifecycle, packageJSON, root, dir, function (err, spawnOptions) {
+ getBasicSpawnOptions(lifecycle, packageJSON, root, dir, options.defaultScript, function (err, spawnOptions) {
if (err) {
callback(err, null);
return;
@@ -83,18 +84,15 @@ function getPathVariable() {
}
function semverMatchesPackage(configKey, packageJSON) {
- return configKey === packageJSON.name || configKey === packageJSON.name + '@' + packageJSON.version;
+ var name = packageJSON.name;
+ return configKey === name || configKey === name + '@' + packageJSON.version;
}
-function getBasicSpawnOptions(lifecycle, packageJSON, root, cwd, callback) {
+function getBasicSpawnOptions(lifecycle, packageJSON, root, cwd, defaultScript, callback) {
if (typeof packageJSON !== 'object' || packageJSON === null || Array.isArray(packageJSON)) {
callback(new Error('package.json is not an object'));
return;
}
- if (!packageJSON.scripts || !packageJSON.scripts[lifecycle]) {
- callback(new Error('no script for ' + lifecycle));
- return;
- }
//
// Convert relevant info into an flat env object
//
@@ -109,13 +107,23 @@ function getBasicSpawnOptions(lifecycle, packageJSON, root, cwd, callback) {
//
var hookFile = path.join(root, 'node_modules', '.hooks', lifecycle);
fs.stat(hookFile, function (err, stat) {
- var script = packageJSON.scripts[lifecycle];
+ var script = packageJSON.scripts && packageJSON.scripts[lifecycle] || defaultScript;
//
// Only run a hook if it is executable
//
if (!err && (process.platform === 'windows' || (stat.mode & parseInt(111, 8)))) {
- script += ' && ' + hookFile;
+ if (script) script += ' && ' + hookFile;
+ else script = hookFile;
}
+ //
+ // Complain if we aren't going to run anything
+ //
+ //if (!script) {
+ // err = new Error('ENOSCRIPT: ' + lifecycle);
+ // err.code = 'ENOSCRIPT';
+ // callback(err);
+ // return;
+ //}
var argv = getRunnerArgs([script]);
callback(null, [argv[0], argv[1], {
env: env,
@@ -139,7 +147,7 @@ function concatPaths(path1, path2) {
function getRunnerArgs(argv) {
var shell;
- argv = [ [argv.map(String).join(' ')] ];
+ argv = [ argv.join(' ') ];
if (process.platform === 'windows') {
shell = 'cmd.exe';
}
View
@@ -4,7 +4,7 @@
"description": "NPM run-script spawning options generator",
"main": "lib/npm-script.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
+ "test": "for SCRIPT in test/*.js; do node $SCRIPT; done"
},
"repository": {
"type": "git",
@@ -16,5 +16,9 @@
"runner"
],
"author": "bradleymeck",
- "license": "MIT"
+ "license": "MIT",
+ "dependencies": {
+ "merge-recursive": "0.0.3",
+ "suspawn": "~0.1.1"
+ }
}
View
@@ -0,0 +1,37 @@
+# npm-script
+
+Generate spawn options for running package.json scripts
+
+## Example
+
+Emulates `npm run-script $SCRIPT_NAME`
+
+**NOTE: ** Does not add in the log wrapping like NPM
+
+```javascript
+var lifecycle = process.argv[2];
+getNPMSpawnOptions(process.cwd(), lifecycle, {
+ defaultScript: {
+ start: 'node server.js',
+ preinstall: '[ -f wscript] && (node-waf clean || true; node-waf configure build)'
+ }[lifecycle],
+ env: {
+ PATH: process.env.PATH
+ }
+}, function (err, spawnOptions) {
+ var script = require('child_process').spawn.apply(null, spawnOptions);
+ script.stderr.pipe(process.stderr);
+ script.stdout.pipe(process.stdout);
+ script.on('exit', process.exit.bind(process));
+});
+```
+
+## Differences from NPM
+
+1. Does not add in defaults for scripts like `npm run-scripts start` (use options.defaultScript)
+2. Does not auto add in the current path, set it in options.env (will append if already has a value)
+3. Does not change users for you, use something like `suspawn`
+
+## See
+
+`npm help scripts`
View
@@ -0,0 +1,25 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-without-config';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, '');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+ }
+);
@@ -0,0 +1,28 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-without-scripts-or-hooks';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, '');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+
+ assert.equal(env.npm_config_npm_script_config_value, void 0);
+ assert.equal(env.npm_package_config_config_value, 'in package.json');
+ }
+);
@@ -0,0 +1,28 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-without-scripts-or-hooks';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('overriding-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, '');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+
+ assert.equal(env.npm_config_npm_script_config_value, 'in config');
+ assert.equal(env.npm_package_config_config_value, 'in config');
+ }
+);
View
@@ -0,0 +1,26 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-without-config';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json')),
+ defaultScript: 'node server.js'
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, 'node server.js');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+ }
+);
@@ -0,0 +1,3 @@
+{
+
+}
@@ -0,0 +1 @@
+123
@@ -0,0 +1,5 @@
+{
+ "npm-script": {
+ "config-value": "in config"
+ }
+}
@@ -0,0 +1,4 @@
+{
+ "name": "npm-script",
+ "config": {"config-value": "in package.json"}
+}
@@ -0,0 +1 @@
+console.log('DEFAULT SCRIPT.');
@@ -0,0 +1,7 @@
+{
+ "name": "npm-script",
+ "scripts": {
+ "start": "env"
+ },
+ "config": {"config-value": "in package.json"}
+}
@@ -0,0 +1 @@
+console.log('DEFAULT SCRIPT.');
@@ -0,0 +1,7 @@
+{
+ "name": "npm-script",
+ "scripts": {
+ "start": "env"
+ },
+ "config": {"config-value": "in package.json"}
+}
@@ -0,0 +1 @@
+console.log('DEFAULT SCRIPT.');
@@ -0,0 +1,3 @@
+{
+ "name": "npm-script"
+}
@@ -0,0 +1,4 @@
+{
+ "name": "npm-script",
+ "config": {"config-value": "in package.json"}
+}
@@ -0,0 +1 @@
+console.log('DEFAULT SCRIPT.');
View
@@ -0,0 +1,4 @@
+var path = require('path');
+exports.getFixture = function (fixturePath) {
+ return path.join(__dirname, '..', 'fixtures', fixturePath);
+}
View
@@ -0,0 +1,16 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'invalid-package';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.notEqual(err, null);
+ }
+);
View
@@ -0,0 +1,28 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-with-hooks';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, helpers.getFixture(__base) + '/node_modules/.hooks/start');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+
+ assert.equal(env.npm_config_npm_script_config_value, void 0);
+ assert.equal(env.npm_package_config_config_value, 'in package.json');
+ }
+);
@@ -0,0 +1,28 @@
+var npmScript = require('../');
+
+var assert = require('assert');
+var helpers = require('./helpers');
+
+var __base = 'package-with-scripts-and-hooks';
+npmScript.getSpawnOptions(
+ helpers.getFixture(__base),
+ 'start',
+ {
+ config: require(helpers.getFixture('default-config.json'))
+ },
+ function (err, spawnOptions) {
+ assert.ifError(err);
+ var script = spawnOptions[1].pop();
+ var env = spawnOptions[2].env;
+ var cwd = spawnOptions[2].cwd;
+ assert.equal(script, require(helpers.getFixture(__base + '/package.json')).scripts.start + ' && ' + helpers.getFixture(__base) + '/node_modules/.hooks/start');
+ assert.equal(cwd, helpers.getFixture(__base));
+ assert.equal(env.npm_lifecycle_event, 'start');
+ assert.equal(env.npm_config_root, cwd);
+ assert.equal(env.npm_package_name, require(helpers.getFixture(__base + '/package.json')).name);
+ assert.notEqual(env.PATH.indexOf(helpers.getFixture(__base + '/node_modules/.bin')), -1);
+
+ assert.equal(env.npm_config_npm_script_config_value, void 0);
+ assert.equal(env.npm_package_config_config_value, 'in package.json');
+ }
+);
Oops, something went wrong.

0 comments on commit 4a4efc5

Please sign in to comment.