Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make npm package and introduce 'autosave' bin

Closes #2
Closes #3
  • Loading branch information...
commit ab36628075bf95f208f3958bf4ef64fab5d3893b 1 parent d40d773
@NV authored
Showing with 119 additions and 59 deletions.
  1. +10 −1 README.mdown
  2. +19 −0 bin/autosave
  3. +73 −58 index.js
  4. +17 −0 package.json
View
11 README.mdown
@@ -1 +1,10 @@
-See [chrome-devtools-autosave](https://github.com/NV/chrome-devtools-autosave).
+A server for **[chrome-devtools-autosave](https://github.com/NV/chrome-devtools-autosave)**.
+
+## Installation
+
+1. Install [Node.js](http://nodejs.org/)
+2. Run `npm install -g autosave` in a terminal
+
+## Usage
+ $ autosave
+ DevTools Autosave is listening on http://127.0.0.1:9104
View
19 bin/autosave
@@ -0,0 +1,19 @@
+#!/usr/bin/env node
+
+var autosave = require('../index.js');
+var program = require('commander');
+
+program
+ .version('0.1.0')
+ .option('-p, --port [' + autosave.defaultPort + ']', 'set port to listen on', parseInt)
+ .option('-f, --config <routes.js>', 'set config file', String)
+ .parse(process.argv);
+
+var routes;
+if (program.config) {
+ routes = require(require('path').join(process.cwd(), program.config));
+} else {
+ routes = autosave.routes;
+}
+
+autosave.start(routes, program.port || autosave.defaultPort, autosave.defaultAddress);
View
131 index.js
@@ -6,77 +6,92 @@ var routes = [
to: '/'
}
];
+var port = 9104;
+var address = '127.0.0.1';
-var http = require('http');
-var fs = require('fs');
+function start(routes, port, address) {
-http.createServer(function(request, response) {
+ var fs = require('fs');
- function internalServerError(message) {
- response.writeHead(500);
- response.end(message);
- }
-
- if (request.headers['x-type'] == 'document') {
- internalServerError("Can’t add a new CSS rule. It is not yet supported.");
- return;
- }
+ require('http').createServer(function(request, response) {
- var url = request.headers['x-url'];
+ if (request.headers['x-type'] == 'document') {
+ internalServerError("Can’t add a new CSS rule. It is not yet supported.");
+ return;
+ }
- if (!url) {
- internalServerError('X-URL header is missing');
- return;
- }
+ var url = request.headers['x-url'];
- var matches;
- for (var i = 0; i < routes.length; i++) {
- var route = routes[i];
- if (route.from.test(url)) {
- matches = true;
- break;
+ if (!url) {
+ internalServerError('X-URL header is missing');
+ return;
}
- }
- if (!matches) {
- if (i === 1) {
- internalServerError('URL (' + url + ') doesn’t match RegExp ' + route.from);
- } else {
- internalServerError('URL (' + url + ') doesn’t match any of the following RegExps:\n' + routes.map(function(a) {
- return a.from;
- }).join('\n'));
+ var matches;
+ for (var i = 0; i < routes.length; i++) {
+ var route = routes[i];
+ if (route.from.test(url)) {
+ matches = true;
+ break;
+ }
}
- return;
- }
- var path = decodeURIComponent(url.replace(route.from, route.to));
- if (/\/[C-Z]:\//.test(path)) {
- // Oh, Windows.
- path = path.slice(1);
- }
-
- var chunks = [];
- request.on('data', function(chunk) {
- chunks.push(chunk);
- });
+ if (!matches) {
+ if (i === 1) {
+ internalServerError('URL (' + url + ') doesn’t match RegExp ' + route.from);
+ } else {
+ internalServerError('URL (' + url + ') doesn’t match any of the following RegExps:\n' + routes.map(function(a) {
+ return a.from;
+ }).join('\n'));
+ }
+ return;
+ }
- request.on('end', function() {
- var stream = fs.createWriteStream(path);
- for (var i = 0; i < chunks.length; i++) {
- stream.write(chunks[i]);
+ var path = decodeURIComponent(url.replace(route.from, route.to));
+ if (/\/[C-Z]:\//.test(path)) {
+ // Oh, Windows.
+ path = path.slice(1);
}
- stream.on('error', function(error) {
- console.error(error.message);
- internalServerError(error.message);
+
+ var chunks = [];
+ request.on('data', function(chunk) {
+ chunks.push(chunk);
});
- stream.on('close', function() {
- response.writeHead(200);
- response.end('OK\n');
- console.log('Saved a ' + request.headers['x-type'] + ' to ' + path);
+
+ request.on('end', function() {
+ var stream = fs.createWriteStream(path);
+ for (var i = 0; i < chunks.length; i++) {
+ stream.write(chunks[i]);
+ }
+ stream.on('error', function(error) {
+ console.error(error.message);
+ internalServerError(error.message);
+ });
+ stream.on('close', function() {
+ response.writeHead(200);
+ response.end('OK\n');
+ console.log('Saved a ' + request.headers['x-type'] + ' to ' + path);
+ });
+ stream.end();
});
- stream.end();
- });
-}).listen(9104, '127.0.0.1');
+ function internalServerError(message) {
+ response.writeHead(500);
+ response.end(message);
+ }
+
+ }).listen(port, address);
+
+ console.log('DevTools Autosave is listening on http://' + address + ':' + port);
+}
-console.log('DevTools Autosave is listening on http://127.0.0.1:9104');
+if (module.parent) {
+ // Loaded via module, i.e. require('index.js')
+ exports.start = start;
+ exports.routes = routes;
+ exports.defaultPort = port;
+ exports.defaultAddress = address;
+} else {
+ // Loaded directly, i.e. `node index.js`
+ start(routes, port, address);
+}
View
17 package.json
@@ -0,0 +1,17 @@
+{
+ "name": "autosave",
+ "version": "0.1.0",
+ "description": "A server for Chrome Devtools Autosave (https://github.com/NV/chrome-devtools-autosave)",
+ "keywords": ["devtools"],
+ "bin": {
+ "autosave": "./bin/autosave"
+ },
+ "author": "Nikita Vasilyev <me@elv1s.ru>",
+ "main": "index",
+ "engines": {
+ "node": ">= 0.4.x"
+ },
+ "dependencies": {
+ "commander": "0.5.x"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.