Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .npirc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"targetUrl": {
"protocol": "http:",
"slashes": true,
"auth": null,
"host": "jquery.com",
"port": null,
"hostname": "jquery.com",
"hash": null,
"search": null,
"query": null,
"pathname": "/",
"path": "/",
"href": "http://jquery.com/"
},
"targetDir": "./test",
"proxyPort": "8000"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
* `-u`, `--target-url` Specify url to proxy. Default currently set to http://jquery.com/ for example purposes
* `-d`, `--target-dir` Specify directory containing files to inject. Default currently set to './test' for example purposes
* `-p`, `--port` Specify port for proxy server to listen on. Default is 8000.
* `-c`, `--create-rc` Creates an RC file in the local dir. Prints + Prompts w/ diff. Default is false.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
"homepage": "https://github.com/casecode/node-proxy-injector",
"dependencies": {
"cheerio": "^0.18.0",
"colors": "^1.0.3",
"commander": "^2.6.0",
"connect": "^3.3.4",
"diff": "^1.2.1",
"livereload": "^0.3.4",
"q": "^1.1.2"
"q": "^1.1.2",
"rc": "^0.5.5",
"readline-sync": "^0.4.10"
}
}
47 changes: 42 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env node
/*jshint node:true*/

var appname = 'npi'; // .npirc
var rc = require('rc');
var rcfile = '.' + appname + 'rc';
var program = require('commander');
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
var connect = require('connect');
var proxyInjector = require('./lib/proxy-injector');
var livereload = require('livereload');
Expand All @@ -16,6 +19,7 @@ program
.option('-u, --target-url [url]', 'The target url to proxy', 'http://jquery.com/')
.option('-d, --target-dir [path]', 'The path of the target directory to watch', './test')
.option('-p, --port <n>', 'The proxy port', parseInt, '8000')
.option('-c, --create-rc', 'Create .npirc file', false)
.parse(process.argv);

// resolve options args
Expand All @@ -25,16 +29,49 @@ if (targetUrl.substring(0, 4) !== "http") {
}
targetUrl = url.parse(targetUrl);

var targetDir = path.resolve(__dirname, program.targetDir);
var targetDir = program.targetDir;


// Config options
var options = {
var config = { // defaults:
targetUrl: targetUrl,
targetDir: targetDir, // directory containing scripts and stylesheets for injection
proxyPort: program.port // local proxy server port
};

// write rc or prompt w/ if already exist
if (program.createRc) {
var writeConf = function () {
fs.writeFileSync(rcfile, JSON.stringify(config, 2, ' '));
};

if ( ! fs.existsSync(rcfile) ) writeConf();
else {
require('colors');
var prompt = require('readline-sync');
var diff = require('diff');

diff.diffJson(
JSON.parse(fs.readFileSync(rcfile).toString()),
config
).forEach(function(part){
// green for additions, red for deletions
// grey for common parts
var color = part.added ? 'green' :
part.removed ? 'red' : 'grey';
process.stderr.write(part.value[color]);
});

console.log();

if (prompt.question('Overwrite? (y/n) :'.blue)
.toLowerCase() === 'y') writeConf();
}
}

// Config options
var options = rc(appname, config);
// note: defaults which are objects will be merged, not replaced
// views: {foo:'bar'}

var proxy = proxyInjector.createProxyServer(options);

var app = connect();
Expand Down