server.js: check if working directory contains necessary files

a warning gets logged on the console if any of the following is missing:

* index.html file
* st/ directory
* js/ directory
This commit is contained in:
mkroehnert 2012-12-10 21:45:41 +01:00
parent 4f40a30718
commit 806975dff8
2 changed files with 51 additions and 17 deletions

View File

@ -25,6 +25,15 @@ initialize
fs := self require: 'fs'.
util := self require: 'util'.
url := self require: 'url'
!
checkDirectoryLayout
(path existsSync: self basePath, 'index.html') ifFalse: [
console warn: 'Warning: project directory does not contain index.html'].
(path existsSync: self basePath, 'st') ifFalse: [
console warn: 'Warning: project directory is missing an "st" directory'].
(path existsSync: self basePath, 'js') ifFalse: [
console warn: 'Warning: roject directory is missing a "js" directory'].
! !
!FileServer methodsFor: 'private'!
@ -577,14 +586,16 @@ mimeTypeFor: aString
!FileServer class methodsFor: 'initialization'!
main
| arguments portOption port|
| fileServer arguments portOption port|
fileServer := self new.
fileServer checkDirectoryLayout.
arguments := process argv.
portOption := arguments at: 3 ifAbsent: [nil].
port := arguments at: 4 ifAbsent: [nil].
('-p' = portOption and: [port notNil]) ifTrue: [
FileServer port: port.
fileServer port: port.
].
^self new startOn: self port
^fileServer start
! !

View File

@ -369,15 +369,18 @@ function Smalltalk(){
st.removeMethod = function(method) {
var protocol = method.category;
var shouldDeleteProtocol;
var klass = method.methodClass;
var methods = klass.fn.prototype.methods;
delete klass.fn.prototype[method.selector._asSelector()];
delete klass.fn.prototype.methods[method.selector];
delete klass.fn.prototype[method.selector._asSelector()];
delete methods[method.selector];
for(var i=0; i<klass.fn.prototype.methods; i++) {
if(klass.fn.prototype.methods[i].category == protocol) {
shouldDeleteProtocol = true;
var selectors = Object.keys(methods);
var shouldDeleteProtocol = true;
for(var i= 0, l = selectors.length; i<l; i++) {
if(methods[selectors[i]].category === protocol) {
shouldDeleteProtocol = false;
break;
};
};
if(shouldDeleteProtocol) {
@ -13956,6 +13959,24 @@ referencedClasses: []
}),
smalltalk.FileServer);
smalltalk.addMethod(
"_checkDirectoryLayout",
smalltalk.method({
selector: "checkDirectoryLayout",
category: 'initialization',
fn: function (){
var self=this;
((($receiver = smalltalk.send(self['@path'], "_existsSync_", [smalltalk.send(smalltalk.send(self, "_basePath", []), "__comma", ["index.html"])])).klass === smalltalk.Boolean) ? (! $receiver ? (function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: project directory does not contain index.html"]);})() : nil) : smalltalk.send($receiver, "_ifFalse_", [(function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: project directory does not contain index.html"]);})]));
((($receiver = smalltalk.send(self['@path'], "_existsSync_", [smalltalk.send(smalltalk.send(self, "_basePath", []), "__comma", ["st"])])).klass === smalltalk.Boolean) ? (! $receiver ? (function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: project directory is missing an \x22st\x22 directory"]);})() : nil) : smalltalk.send($receiver, "_ifFalse_", [(function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: project directory is missing an \x22st\x22 directory"]);})]));
((($receiver = smalltalk.send(self['@path'], "_existsSync_", [smalltalk.send(smalltalk.send(self, "_basePath", []), "__comma", ["js"])])).klass === smalltalk.Boolean) ? (! $receiver ? (function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: roject directory is missing a \x22js\x22 directory"]);})() : nil) : smalltalk.send($receiver, "_ifFalse_", [(function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_warn_", ["Warning: roject directory is missing a \x22js\x22 directory"]);})]));
return self;},
args: [],
source: "checkDirectoryLayout\x0a\x09(path existsSync: self basePath, 'index.html') ifFalse: [\x0a\x09\x09console warn: 'Warning: project directory does not contain index.html'].\x0a\x09(path existsSync: self basePath, 'st') ifFalse: [\x0a\x09\x09console warn: 'Warning: project directory is missing an \x22st\x22 directory'].\x0a\x09(path existsSync: self basePath, 'js') ifFalse: [\x0a\x09\x09console warn: 'Warning: roject directory is missing a \x22js\x22 directory'].",
messageSends: ["ifFalse:", "existsSync:", ",", "basePath", "warn:"],
referencedClasses: []
}),
smalltalk.FileServer);
smalltalk.addMethod(
"_handleGETRequest_respondTo_",
smalltalk.method({
@ -14160,11 +14181,10 @@ selector: "start",
category: 'starting',
fn: function (){
var self=this;
var server=nil;
(function($rec){smalltalk.send($rec, "_on_do_", ["error", (function(error){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_log_", [smalltalk.send("Error starting server: ", "__comma", [error])]);})]);smalltalk.send($rec, "_on_do_", ["listening", (function(){return smalltalk.send((typeof console == 'undefined' ? nil : console), "_log_", [smalltalk.send("Starting file server on port ", "__comma", [smalltalk.send(smalltalk.send(self, "_port", []), "_asString", [])])]);})]);return smalltalk.send($rec, "_listen_", [smalltalk.send(self, "_port", [])]);})(smalltalk.send(self['@http'], "_createServer_", [(function(request, response){return smalltalk.send(self, "_handleRequest_respondTo_", [request, response]);})]));
return self;},
args: [],
source: "start\x0a\x09| server |\x0a\x09(http createServer: [:request :response |\x0a\x09 self handleRequest: request respondTo: response])\x0a\x09 on: 'error' do: [:error | console log: 'Error starting server: ', error];\x0a\x09 on: 'listening' do: [console log: 'Starting file server on port ', self port asString];\x0a\x09 listen: self port.",
source: "start\x0a\x09(http createServer: [:request :response |\x0a\x09 self handleRequest: request respondTo: response])\x0a\x09 on: 'error' do: [:error | console log: 'Error starting server: ', error];\x0a\x09 on: 'listening' do: [console log: 'Starting file server on port ', self port asString];\x0a\x09 listen: self port.",
messageSends: ["on:do:", "log:", ",", "asString", "port", "listen:", "createServer:", "handleRequest:respondTo:"],
referencedClasses: []
}),
@ -14228,19 +14248,22 @@ selector: "main",
category: 'initialization',
fn: function (){
var self=this;
var fileServer=nil;
var arguments=nil;
var portOption=nil;
var port=nil;
(fileServer=smalltalk.send(self, "_new", []));
smalltalk.send(fileServer, "_checkDirectoryLayout", []);
(arguments=smalltalk.send((typeof process == 'undefined' ? nil : process), "_argv", []));
(portOption=smalltalk.send(arguments, "_at_ifAbsent_", [(3), (function(){return nil;})]));
(self['@port']=smalltalk.send(arguments, "_at_ifAbsent_", [(4), (function(){return nil;})]));
((($receiver = smalltalk.send(smalltalk.send("-p", "__eq", [portOption]), "_and_", [(function(){return smalltalk.send(self['@port'], "_notNil", []);})])).klass === smalltalk.Boolean) ? ($receiver ? (function(){return smalltalk.send((smalltalk.FileServer || FileServer), "_port_", [self['@port']]);})() : nil) : smalltalk.send($receiver, "_ifTrue_", [(function(){return smalltalk.send((smalltalk.FileServer || FileServer), "_port_", [self['@port']]);})]));
return smalltalk.send(smalltalk.send(self, "_new", []), "_startOn_", [smalltalk.send(self, "_port", [])]);
((($receiver = smalltalk.send(smalltalk.send("-p", "__eq", [portOption]), "_and_", [(function(){return smalltalk.send(self['@port'], "_notNil", []);})])).klass === smalltalk.Boolean) ? ($receiver ? (function(){return smalltalk.send(fileServer, "_port_", [self['@port']]);})() : nil) : smalltalk.send($receiver, "_ifTrue_", [(function(){return smalltalk.send(fileServer, "_port_", [self['@port']]);})]));
return smalltalk.send(fileServer, "_start", []);
return self;},
args: [],
source: "main\x0a\x09| arguments portOption port|\x0a\x09arguments := process argv.\x0a\x09portOption := arguments at: 3 ifAbsent: [nil].\x0a\x09port := arguments at: 4 ifAbsent: [nil].\x0a\x0a\x09('-p' = portOption and: [port notNil]) ifTrue: [\x0a\x09\x09FileServer port: port.\x0a\x09].\x0a\x09^self new startOn: self port",
messageSends: ["argv", "at:ifAbsent:", "ifTrue:", "and:", "=", "notNil", "port:", "startOn:", "new", "port"],
referencedClasses: ["FileServer"]
source: "main\x0a\x09| fileServer arguments portOption port|\x0a\x09fileServer := self new.\x0a\x09fileServer checkDirectoryLayout.\x0a\x0a\x09arguments := process argv.\x0a\x09portOption := arguments at: 3 ifAbsent: [nil].\x0a\x09port := arguments at: 4 ifAbsent: [nil].\x0a\x09('-p' = portOption and: [port notNil]) ifTrue: [\x0a\x09\x09fileServer port: port.\x0a\x09].\x0a\x09^fileServer start",
messageSends: ["new", "checkDirectoryLayout", "argv", "at:ifAbsent:", "ifTrue:", "and:", "=", "notNil", "port:", "start"],
referencedClasses: []
}),
smalltalk.FileServer.klass);