From 5e286098d12f0a2f46dffe07785820b9a1005255 Mon Sep 17 00:00:00 2001 From: SirStendec Date: Sun, 8 Feb 2015 04:06:56 -0500 Subject: [PATCH] Moved the development server into the gulpfile and added it to the "gulp watch" command for easier development. Updated readme to reflect changes. --- README.md | 18 +++++++-------- gulpfile.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 3 --- test/server.js | 47 ---------------------------------------- 4 files changed, 66 insertions(+), 61 deletions(-) delete mode 100644 test/server.js diff --git a/README.md b/README.md index c35386df..8b213bd5 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,14 @@ files for changes and re-build automatically with ```gulp watch``` FrankerFaceZ comes with a local development server that listens on port 8000 and it serves up local development copies of files, falling back to the CDN when a local copy of a file isn't present. To start the server, -run ```npm test``` +run ```gulp server``` + +For convenience, the server is run automatically along with ```gulp watch``` -At this time, you will also need to use the included version of the Chrome -extension. Remove any existing copy of FrankerFaceZ from your browser and load -the unpacked extension in the ```Chrome Extension``` folder. - -Once you're using that extension, use the command ```/ffz developer_mode on``` -or ```/ffz developer_mode off``` in Twitch chat to toggle developer mode on or -off. You must then refresh the page for changes to take effect. If FFZ is not -working or the command otherwise fails to work, you can open the JavaScript -console on twitch.tv and run ```localStorage.ffzDebugMode = true;``` or +Use the command ```/ffz developer_mode on``` or ```/ffz developer_mode off``` +in Twitch chat to toggle developer mode on or off. You must then refresh the +page for changes to take effect. If FFZ is not working or the command otherwise +fails to work, you can open the JavaScript console on twitch.tv and run +```localStorage.ffzDebugMode = true;``` or ```localStorage.ffzDebugMode = false;``` to enable or disable the feature. \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index c3fafee4..3b70c843 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,3 +1,4 @@ +// Dependencies var fs = require('fs'), gulp = require('gulp'), browserify = require('gulp-browserify'), @@ -9,6 +10,16 @@ var fs = require('fs'), rename = require('gulp-rename'), uglify = require('gulp-uglify'); +// Server Dependencies +var http = require("http"), + path = require("path"), + request = require("request"), + url = require("url"); + +var server_version = "0.1.1"; + + +// Tasks gulp.task('clean', function() { return gulp.src('build', {read:false}) @@ -33,8 +44,54 @@ gulp.task('scripts', ['prepare'], function() { .on('error', util.log); }); -gulp.task('watch', ['default'], function() { +gulp.task('watch', ['default', 'server'], function() { gulp.watch('src/**/*', ['default']); }); gulp.task('default', ['scripts']); + + +// Server + +gulp.task('server', function() { + http.createServer(function(req, res) { + var uri = url.parse(req.url).pathname, + lpath = path.join(uri).split(path.sep); + + if ( uri == "/dev_server" ) { + util.log("[" + util.colors.cyan("HTTP") + "] " + util.colors.green("200") + " GET " + util.colors.magenta(uri)); + res.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}); + return res.end(JSON.stringify({path: process.cwd(), version: server_version})); + } + + if ( ! lpath[0] ) + lpath.shift(); + + if ( lpath[0] == "script" ) + lpath.shift(); + else + lpath.splice(0, 0, "cdn"); + + var file = path.join(process.cwd(), lpath.join(path.sep)); + + fs.exists(file, function(exists) { + if ( ! exists ) { + util.log("[" + util.colors.cyan("HTTP") + "] " + util.colors.bold.blue("CDN") + " GET " + util.colors.magenta(uri)); + return request.get("http://cdn.frankerfacez.com/" + uri).pipe(res); + } + + if ( fs.lstatSync(file).isDirectory() ) { + util.log("[" + util.colors.cyan("HTTP") + "] " + util.colors.red("403") + " GET " + util.colors.magenta(uri)); + res.writeHead(403, {"Access-Control-Allow-Origin": "*"}); + res.write('403 Forbidden'); + return res.end(); + } + + util.log("[" + util.colors.cyan("HTTP") + "] " + util.colors.green("200") + " GET " + util.colors.magenta(uri)); + res.writeHead(200, {"Access-Control-Allow-Origin": "*"}); + fs.createReadStream(file).pipe(res); + }); + + }).listen(8000); + util.log("[" + util.colors.cyan("HTTP") + "] Listening on Port: " + util.colors.magenta("8000")); +}); \ No newline at end of file diff --git a/package.json b/package.json index fbd01250..b6bed7ac 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,6 @@ "gulp-util": "^3.0.2", "request": "^2.51.0" }, - "scripts": { - "test": "node test/server.js" - }, "repository": { "type": "git", "url": "https://bitbucket.org/stendec/frankerfacez.git" diff --git a/test/server.js b/test/server.js deleted file mode 100644 index 3bc0d929..00000000 --- a/test/server.js +++ /dev/null @@ -1,47 +0,0 @@ -var version = "0.1.0"; - -var fs = require("fs"), - http = require("http"), - path = require("path"), - request = require("request"), - url = require("url"); - -http.createServer(function(req, res) { - var uri = url.parse(req.url).pathname, - lpath = path.join(uri).split(path.sep); - - if ( uri == "/dev_server" ) { - console.log("[200] GET " + uri); - res.writeHead(200, {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}); - return res.end(JSON.stringify({path: process.cwd(), version: version})); - } - - if ( ! lpath[0] ) - lpath.shift(); - - if ( lpath[0] == "script" ) - lpath.shift(); - else - lpath.splice(0, 0, "cdn"); - - var file = path.join(process.cwd(), lpath.join(path.sep)); - - fs.exists(file, function(exists) { - if ( ! exists ) { - console.log("[CDN] GET " + uri); - return request.get("http://cdn.frankerfacez.com/" + uri).pipe(res); - } - - if ( fs.lstatSync(file).isDirectory() ) { - console.log("[403] GET " + uri); - res.writeHead(403, {"Access-Control-Allow-Origin": "*"}); - res.write('403 Forbidden'); - return res.end(); - } - - console.log("[200] GET " + uri); - res.writeHead(200, {"Access-Control-Allow-Origin": "*"}); - fs.createReadStream(file).pipe(res); - }); - -}).listen(8000); \ No newline at end of file