#!/usr/bin/env node

console.log('running better-npm-run in', process.cwd());
var join = require('path').join;
var fullPackagePath = join(process.cwd(), 'package.json');
var pkg = require(fullPackagePath);
var exec = require('./lib/exec.js')

if (!pkg.scripts) {
	process.stderr.write('ERROR: No scripts found!');
	process.exit(1);
}
if (!pkg.betterScripts) {
	process.stderr.write('ERROR: No betterScripts found!');
	process.exit(1);
}
if (!process.argv[2]) {
	process.stderr.write('ERROR: No script name provided!');
	process.exit(1);
}
if (!pkg.betterScripts[process.argv[2]]) {
	process.stderr.write('ERROR: No betterScript with name "'+process.argv[2]+'" was found!');
	process.exit(1);
}

console.log('Executing script: ' + process.argv[2] + '\n');

exec(pkg.betterScripts[process.argv[2]], function (error, stdout, stderr) {
  process.stderr.write(stderr);
  process.stdout.write(stdout);
  if(error !== null) {
    console.log('exec error: '+error);
  }
});

@@ -12,19 +12,17 @@
 env node
-%0D%0A%0D
+%0A
 %0Aconsole
@@ -70,17 +70,16 @@
 .cwd());
-%0D
 %0Avar joi
@@ -103,17 +103,16 @@
 ').join;
-%0D
 %0Avar ful
@@ -162,17 +162,16 @@
 .json');
-%0D
 %0Avar pkg
@@ -198,17 +198,16 @@
 gePath);
-%0D
 %0Avar exe
@@ -234,19 +234,17 @@
 xec.js')
-%0D%0A%0D
+%0A
 %0Aif (!pk
@@ -247,33 +247,32 @@
 (!pkg.scripts) %7B
-%0D
 %0A%09process.stderr
@@ -298,33 +298,32 @@
 cripts found!');
-%0D
 %0A%09process.exit(1
@@ -316,36 +316,34 @@
 process.exit(1);
-%0D
 %0A%7D
-%0D
 %0Aif (!pkg.better
@@ -352,17 +352,16 @@
 ripts) %7B
-%0D
 %0A%09proces
@@ -401,33 +401,32 @@
 cripts found!');
-%0D
 %0A%09process.exit(1
@@ -423,28 +423,26 @@
 ess.exit(1);
-%0D
 %0A%7D
-%0D
 %0Aif (!proces
@@ -449,25 +449,24 @@
 s.argv%5B2%5D) %7B
-%0D
 %0A%09process.st
@@ -507,25 +507,24 @@
 provided!');
-%0D
 %0A%09process.ex
@@ -529,20 +529,18 @@
 exit(1);
-%0D
 %0A%7D
-%0D
 %0Aif (!pk
@@ -574,17 +574,16 @@
 v%5B2%5D%5D) %7B
-%0D
 %0A%09proces
@@ -666,17 +666,16 @@
 ound!');
-%0D
 %0A%09proces
@@ -688,14 +688,11 @@
 (1);
-%0D
 %0A%7D
-%0D%0A%0D
+%0A
 %0Acon
@@ -751,11 +751,9 @@
 n');
-%0D%0A%0D
+%0A
 %0Aexe
@@ -824,17 +824,16 @@
 tderr) %7B
-%0D
 %0A  proce
@@ -856,17 +856,16 @@
 stderr);
-%0D
 %0A  proce
@@ -888,17 +888,16 @@
 stdout);
-%0D
 %0A  if(er
@@ -911,17 +911,16 @@
  null) %7B
-%0D
 %0A    con
@@ -954,16 +954,13 @@
 or);
-%0D
 %0A  %7D
-%0D
 %0A%7D);
-%0D
 %0A

convert from dos line endings to unix