From 65a5b01cd73a5ad07e983b02042e170ef8bc7aea Mon Sep 17 00:00:00 2001 From: Benjamin Orozco Date: Mon, 15 Jun 2015 14:23:17 -0500 Subject: [PATCH] Initial commit --- .gitignore | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 28 +++++++++++++++++++++ lib/exec.js | 30 ++++++++++++++++++++++ package.json | 22 ++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/exec.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d78d36 --- /dev/null +++ b/.gitignore @@ -0,0 +1,71 @@ +.idea +*.DS_Store + +node_modules +dist +.tmp +.sass-cache +bower_components +cache + +######################################### +# https://gist.github.com/octocat/9257657 +# http://symfony.es/documentacion/como-configurar-bien-el-archivo-gitignore-para-las-aplicaciones-symfony2/ +######################################### + +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +Desktop.ini +$RECYCLE.BIN/ +.AppleDouble +.LSOverride +ehthumbs.db +Thumbs.db + +# Editores # +############ +.*.sw[a-z] +*.un~ +Session.vim +.netrwhist +*.tmp +*.bak +*.swp +/*.sublime-project +*.sublime-workspace +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache diff --git a/README.md b/README.md new file mode 100644 index 0000000..266ecea --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +# Intro + +Better NPM scripts runner + +- Avoid hard-coded commands in package.json +- Cross-platform compatibility, originally needed by: + - https://github.com/formly-js/angular-formly/issues/305 + - https://github.com/npm/npm/issues/2800 + +# Usage in package.json + +From this: +``` +{ + "scripts": { + "build:dist": "NODE_ENV=development webpack --config $npm_package_webpack --progress --colors", + "test": "NODE_ENV=production karma start" + } +} +``` + +To this: +``` +{ + "devDependencies": { + "better-npm-run": "~0.0.1" + }, + "scripts": { + "build:dist": "better-npm-run build:dist", + "test": "better-npm-run test" + }, + "betterScripts": { + "build:dist": { + "command": "webpack --config $npm_package_webpack --progress --colors", + "env": { + "NODE_ENV": "development" + } + }, + "build:prod": { + "command": "webpack --config $npm_package_webpack --progress --colors", + "env": { + "NODE_ENV": "production" + } + }, + "build": [ + { + "run": "build:dist" + }, + { + "run": "build:prod" + } + ], + "test": { + "command": "karma start", + "env": { + "NODE_ENV": "test" + } + } + }, + +} +``` + +# Inspired by + +- https://www.npmjs.com/package/with-package \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..b7f1c66 --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +#!/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]]); \ No newline at end of file diff --git a/lib/exec.js b/lib/exec.js new file mode 100644 index 0000000..b5fdd03 --- /dev/null +++ b/lib/exec.js @@ -0,0 +1,30 @@ +var child_exec = require('child_process').exec; + +module.exports = function exec(script) { + + var command_line = script.command; + + Object.keys(script.env).forEach(function (key) { + var value = script.env[key]; + + if(process.platform === 'win32') { + command_line = 'set ' + key + '=' + script.env[key] + '&& ' + command_line; + } else { + command_line = key + '=' + script.env[key] + ' ' + command_line; + } + }); + + console.log('to be executed:' + command_line); + var command = child_exec(command_line); + + command.stdout.on('data', function(data) { + process.stdout.write(data); + }); + command.stderr.on('data', function(data) { + process.stderr.write(data); + }); + command.on('error', function(err) { + process.stderr.write(err); + }); + +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..148a509 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "better-npm-run", + "description": "Better NPM scripts runner", + "version": "0.0.1", + "license": "MIT", + "author": "Benjamin Orozco ", + "contributors": [ + "Benjamin Orozco ", + "Kent C. Dodds " + ], + "repository": { + "type": "git", + "url": "git://github.com/benoror/better-npm-run.git" + }, + "main": "index.js", + "bin": { + "better-npm-run": "index.js" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + } +}