Ejemplo n.º 1
0
exports.start = function(server, port, host, worker_count, options) {

  function isPort (x) { return parseInt(x) >= 0; }

  var unix_socket_path = null;
  if (!isPort(port)) {
    // shift arguments
    in_options = worker_count;
    worker_count = host;
    unix_socket_path = port;
  }

  // merge options
  var default_options = {
    working_path : process.cwd(),
    tmp_path : '/tmp',
    uid : process.getuid(),
    gid : process.getgid(),
    daemonize: false,
    verbose: false
  }
  if (options) {
    (function(obj1, obj2) {
      for (attrname in obj2) { obj1[attrname] = obj2[attrname]; }
    })(default_options, options);
  }
  options = default_options;

  var path = require('path');
  // check if paths exist
  if (!path.existsSync(options.tmp_path)) {
    throw "Temp path " + options.tmp_path + " does not exist. Please create it";
  }
  if (!path.existsSync(options.working_path)) {
    throw "Working path " + options.working_path + " does not exist. Please create it";
  }


  // setup log function
  var log = options.verbose ?
    function(what) { console.log(what); } :
    function() {};

  // Master or worker?
  var worker_id = process.env._FUGUE_WORKER;
  var is_master = !worker_id;

  // daemonize?
  if (is_master && options.daemonize) {
    log('daemonizing ' + process.pid);
    daemon = require(__dirname + '/../build/default/daemon');
    daemon.setSid();
    log('daemonized');
  }

  // require needed modules
  var net   = require('net'),
      http  = require('http'),
      spawn = require('child_process').spawn,
      sys   = require('sys'),
      fs    = require('fs'),
      path  = require('path'),
      netBinding = process.binding('net');

  // Redirect stdout and stderr
  if (options.log_file) {
    process.stdout.fd = fs.openSync(options.log_file, 'a');
  }

  // calculate master_socket_path.
  // It can come from env if we have been spawned or we have to create a new one
  var master_socket_path = null;
  if (process.env._FUGUE_MASTER_SOCKET_PATH) {
    if (path.existsSync(process.env._FUGUE_MASTER_SOCKET_PATH)) {
      master_socket_path = process.env._FUGUE_MASTER_SOCKET_PATH;
    }
  }
  if (!master_socket_path) {
    // make new
    master_socket_path = process.env._FUGUE_MASTER_SOCKET_PATH = path.join(options.tmp_path, 'fugue_' + process.pid + '_master.sock');
  }

  log('Using master socket path: ' + master_socket_path);

  if (is_master) { // Master

    // If we are respawned from another master we have to setsid anyway
    if (process.env._FUGUE_ORIG_MASTER_PID) {
      daemon = require(__dirname + '/../build/default/daemon');
      daemon.setSid();
    }
    log('Starting new master...');

    if (options.master_log_file) {
      process.stdout.fd = fs.openSync(options.master_log_file, 'w');
    }

    if (process.env._FUGUE_PORT && process.env._FUGUE_ORIG_MASTER_PID) { // we were respawned by another master
      try {
        log('Trying to get socket back from original server...');
        // request socket to original server
        // this startup part has to work synchronously, so we dig deep into the bindings...
        var client_socket = netBinding.socket('unix');
        log('connecting to ' + master_socket_path);
        netBinding.connect(client_socket, master_socket_path);
        var request_buffer = new Buffer('GIMME_SOCKET', 'ascii');
        netBinding.write(client_socket, request_buffer, 0, request_buffer.length);
        var responseBuffer = new Buffer(256);
        var length = null;
        // Node returns length and sets (or not) fd by design as explained in node_net.cc / RecvMsg
        do {
          length = netBinding.recvMsg(client_socket, responseBuffer, 0, responseBuffer.length)
        } while(!netBinding.recvMsg.fd);

        log('Got response from server:' + responseBuffer.toString('ascii', 0, length));
        server_socket = netBinding.recvMsg.fd;
      } catch(error) {
        log('Error trying to get server file descriptor: ' + error.message);
      }

      if (!server_socket) {
        log('Failed to get socket from original server.');
        log('Now I\'m going to try to create one...')
      }
    }

    if (!server_socket) {
      // Now we have to create a socket

      if (unix_socket_path) {
        // UNIX socket

        // remove socket file if it exists
        try {
          fs.unlinkSync(unix_socket_path);
        } catch(err) {
          // do nothing, file does not exist
        }

        server_socket = netBinding.socket('unix');
        netBinding.bind(server_socket, unix_socket_path);
        process.env._FUGUE_SOCKET = unix_socket_path;
      } else {
        // TCP socket
        server_socket = netBinding.socket('tcp' + (netBinding.isIP(host) == 6 ? 6 : 4));
        netBinding.bind(server_socket, port, host);
        process.env._FUGUE_PORT = port;
        process.env._FUGUE_HOST = host;
      }
      netBinding.listen(server_socket, 128);
    }

    log('Have server socket: ' + server_socket);

    var fd_server = net.createServer(function(conn) {
      conn.on('data', function(data) {
        log('got data for fd_server');
        if (data.toString() == 'GIMME_SOCKET') {
          log('serving server socket file descriptor ' + server_socket);
          conn.write('HERE_YOU_GO', 'ascii', server_socket);
        }
      });
    }).listen(master_socket_path);

    var workers = [];

    var spawn_worker = function(worker_idx) {
      // prepare environment for worker
      var env = {};
      for (var i in process.env) {
        env[i] = process.env[i];
      }
      env._FUGUE_WORKER = "" + (worker_idx + 1);
      var args = process.argv;

      // spawn worker process
      var new_worker = workers[worker_idx] = spawn(args[0], args.slice(1), env);
      if (!options.log_file) {
        // if worker does not log into file, pipe the worker output here
        var worker_log = function(what) {
          log("WORKER " + worker_idx + ": " + what.toString());
        }
        new_worker.stdout.on('data', worker_log);
        new_worker.stderr.on('data', worker_log);
      }

      // listen for when the worker dies and bring him back to life
      new_worker.on('exit', function() {
        log('Child ' + worker_idx + ' died. Respawning it.');
        spawn_worker(worker_idx);
      });

    }

    // fork workers
    log('Spawning workers...');
    process.env._FUGUE_WORKER_COUNT = worker_count;
    for (var worker_idx = 0; worker_idx < worker_count; worker_idx ++) {
      spawn_worker(worker_idx);
    }
    log('spawned.')

    var killer = function() {
      log('exiting.');
      for (var i in workers) {
        var worker =  workers[i];
        worker.removeAllListeners(); // Prevent from master respawn
        try {
          worker.kill();
        } catch(excep) {
          // do nothing, as the error is probably that the process does no longer exist
        }
      }
      workers = [];
      netBinding.close(server_socket);
      process.removeAllListeners();
      process.nextTick(function() {
        process.exit();
      });
    }

    // Listen for process exits
    // process.on('exit', world_killer);
    process.on('SIGINT', killer);
    process.on('SIGHUP', killer);
    process.on('SIGTERM', killer);

    // Listen to SIGUSR2 for master restarts
    process.on('SIGUSR2', function() {
      log('Got SIGUSR2, respawning self');
      // respawn self
      var env = {};
      for(var i in process.env){
        env[i] = process.env[i];
      }
      env._FUGUE_ORIG_MASTER_PID = process.pid.toString();
      var args = process.argv;

      // spawn worker process
      spawned = spawn(args[0], args.slice(1), env);
      spawned.stdout.on('data', function(data) {
        log("New master goes: " + data);
      });

    });

    // Save master PID
    if (options.master_pid_path) {
      fs.writeFile(options.master_pid_path, process.pid.toString(), function(error) {
        if (error) throw "Error saving master PID file in " + options.master_pid_path + ': ' + error;
      });
    }

  } else { // Worker

    log('Worker here');

    // track connections here
    var connection_count = 0;

    var die_soon = false; // tells if worker should die after serving exisiting connections
    // Setup killer
    var worker_killer = function() {
      if(connection_count == 0) {
        process.nextTick(function() {
          process.exit();
        });
      } else {
        // Stop listening for new connections - remove watcher from the event loop
        server.watcher.stop();
        // Set process to die after serving existing connections
        die_soon = true;
      }
    }
    process.on('SIGINT', worker_killer);
    process.on('SIGHUP', worker_killer);
    process.on('SIGTERM', worker_killer);

    process.on('exit', function() {
      log("Worker " + worker_id + " exiting.");
    });

    var connection = net.createConnection(master_socket_path).on('connect', function() {
      connection.write('GIMME_SOCKET');
      connection.on('fd', function(fd) {

        var kill_master_pid = parseInt(process.env._FUGUE_ORIG_MASTER_PID);
        // only the last worker tries to kill original master
        if (!!kill_master_pid && workerId() == process.env._FUGUE_WORKER_COUNT) {
          log('killing original master');
          process.kill(kill_master_pid);
        }

        // change working dir
        if (process.cwd() != options.working_path) {
          process.chdir(options.working_path);
        }
        // set gid
        if(process.getgid() != options.gid) {
          process.setgid(options.gid);
        }
        // set uid
        if(process.getuid() != options.uid) {
          process.setuid(options.uid);
        }

        server.listenFD(fd);

      });
    });

    // Track connections so we don't die in the middle of serving one
    server.on('connection', function(connection) {
      connection_count ++;
      connection.on('end', function() {
        connection_count --;
        if (die_soon && connection_count == 0) {
          process.exit();
        }
      });
    });

  }
}
Ejemplo n.º 2
0
 ['index.html', 'images/a.png', 'images/b.png'].forEach(function(file) {
   PATH.existsSync(PATH.resolve(buildir, file)).should.equal(true, file);
 });
Ejemplo n.º 3
0
var redis = require("redis");

var cf = require("cloudfoundry");

var env = {
    DOTCLOUD_SERVICE_NAME: 'Mac Book Pro',
    DOTCLOUD_DATA_REDIS_HOST: 'localhost',
    DOTCLOUD_DATA_REDIS_LOGIN: undefined,
    DOTCLOUD_DATA_REDIS_PASSWORD: "",
    DOTCLOUD_DATA_REDIS_PORT: '6379',
    DOTCLOUD_DATA_REDIS_URL: undefined
};

// app.listen((cloudfoundry.port || 3000), (cloudfoundry.host || ‘localhost’));

if (path.existsSync('/home/dotcloud/environment.json')) {
    env = JSON.parse(fs.readFileSync('/home/dotcloud/environment.json', 'utf-8'));
}

console.log('Application Name: ' + env['DOTCLOUD_SERVICE_NAME']);
console.log('Env: ' + JSON.stringify(env));

app.configure(function () {
    app.use(express.static(__dirname + '/public'));
    app.use(express.logger());
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({secret:"devoxx-2012" + env['DOTCLOUD_DATA_REDIS_PASSWORD']}));
    app.use(express.logger());
    app.use(express.methodOverride());
    app.use(app.router);
Ejemplo n.º 4
0
		testFiles.forEach(function(testFile) {
			fs.unlinkSync(testFile);
			assert.ok(!path.existsSync(testFile), "Could not delete test file: " + testFile);
		});
Ejemplo n.º 5
0
 inst.writeFile('built_assets/index.html', buildir, function(err) {
   if (err) return done(err);
   PATH.existsSync(path).should.equal(true, path);
   done();
 });
Ejemplo n.º 6
0
function buildSourceTask(completeHandler)
{	
	if(!PATH.existsSync(OUTPUT_DIR_NAME))
	{
		FILE.mkdirSync(OUTPUT_DIR_NAME);
	}
	
	js_file_name = js_file_name.split("%VERSION%").join(version);

	var file_args = [];
	var len = SOURCE_FILES.length;
    var merged_source_data = "";

	for(var i = 0; i < len; i++)
	{
		file_args.push("--js");
		file_args.push(SOURCE_FILES[i]);

        merged_source_data += FILE.readFileSync(SOURCE_FILES[i], "UTF-8");
	}
	
	if(extraSourceFiles)
	{
		len = extraSourceFiles.length;
		for(var i = 0; i < len; i++)
		{
			file_args.push("--js");
			file_args.push(extraSourceFiles[i]);

            merged_source_data += FILE.readFileSync(extraSourceFiles[i], "UTF-8");
		}
	}

    var license_data = FILE.readFileSync("license.txt", "UTF-8");
    JS_SRC_FILE_NAME = JS_SRC_FILE_NAME.split("%VERSION%").join(version);
    FILE.writeFileSync( PATH.join(OUTPUT_DIR_NAME, JS_SRC_FILE_NAME ), license_data+merged_source_data, "UTF-8");
    FILE.writeFileSync( PATH.join(OUTPUT_DIR_NAME, JS_SRC_FILE_DEFAULT_NAME), license_data+merged_source_data, "UTF-8");

	var tmp_file = PATH.join(OUTPUT_DIR_NAME,"tmp.js");
	var final_file = PATH.join(OUTPUT_DIR_NAME, js_file_name);

	var cmd = [
		"java", "-jar", GOOGLE_CLOSURE_PATH
	].concat(
			file_args
		).concat(
			["--js_output_file", tmp_file]
		);
		
    print("closure log : \n "+cmd);

	CHILD_PROCESS.exec(
		cmd.join(" "),
		function(error, stdout, stderr)
		{
			if(verbose)
			{
				if(stdout)
				{
					print(stdout);
				}
			
				if(stderr)
				{
					print(stderr);
				}
			}

		    if (error !== null)
			{
				print("Error Running Google Closure : " + error);
				exitWithFailure();
		    }
		

			var final_data = FILE.readFileSync(tmp_file, "UTF-8");

			FILE.writeFileSync(final_file, license_data + final_data, "UTF-8");

			FILE.unlinkSync(tmp_file);
			
			completeHandler(true);
		}
	);
}
Ejemplo n.º 7
0
 it("cleanSource() should delete source folder", function () {
     expect(path.existsSync(session.sourceDir)).toBeTruthy();
     expect(fs.statSync(session.sourceDir).isDirectory()).toBeTruthy();
     fileMgr.cleanSource(session);
     expect(path.existsSync(session.sourceDir)).toBeFalsy();
 });
Ejemplo n.º 8
0
function Bobsled(opts)
{
  require('http').Server.call(this);

  if (typeof(opts) == "undefined") opts = {};

  this.port = ("port" in opts) ? opts.port : 8085; // reads like BOBS on an old calculator

  if ("env" in opts) this.env = opts.env;

  this.template_compiler = ("template_compiler" in opts) ? opts.template_compiler : function () { throw new Error("no template compiler configured"); };

  // TODO: support other template engines besides doT?
  //
  this.template_config = {
    evaluate:    /<%([\s\S]+?)%>/g, // some erb like delimiters
    interpolate: /<%=([\s\S]+?)%>/g,
    varname: ["Page", "Server"],
    strip: false,
    append: true
  };

  this.templates = {};

  // compile all templates, useful for checking correctness on startup
  this.compileTemplates = function() {
    for (var template_name in this.templates) this.templates[template_name].compile();
  };

  this.controllers = {
    template: function(pathinfo, request, response) {
      var template_name = pathinfo.basename || "index"
      if (template_name in this.templates) {
        this.templateResponder(template_name, pathinfo, response);
      } else {
        this.emit("notFound", pathinfo, request, response);
      }
    }.bind(this),
    "static": function(pathinfo, request, response) {
      var filename = "." + pathinfo.dirname + "/" + pathinfo.basename + pathinfo.extname;
      if (!fs.statSync(filename).isFile()) {
        this.emit("notFound", pathinfo, request, response);
      } else {
        response.writeHead(200, {"content-type": "text/css"}); // HACK TODO mimetype
        fs.readFile(filename, function (err, data) { response.end(data); });
      }
    }.bind(this),
    favicon: function(pathinfo, request, response) {
      response.writeHead(200, {"content-type": "image/x-icon"}); // TODO: set expires
      fs.readFile("favicon.ico", function (err, data) { response.end(data); });
    },
    notFound: function (pathinfo, request, response) {
      if ("404" in this.templates) {
        this.templateResponder("404", { statusCode: 404, pathinfo: pathinfo, request: request }, response);
      } else {
        response.writeHead(404, {"content-type": "text/plain"});
        response.end(request.url + " not found");
      }
    }.bind(this)
  };

  // TODO: config/recipe for default routes
  this.routes = {
    GET: {
      "/": {
        "favicon.ico": this.controllers.favicon,
        "*.html": this.controllers.template
      },
    },
    POST: { }
  };

  // setup static routes
  if (true) { // TODO: opts.static? (could pass in path?)
    var static_folders = {"css": "/css", "img": "/img", "js": "/js"}; // TODO: pass in opts?
    for (folder in static_folders) {
      if (path.existsSync(folder) && fs.statSync(folder).isDirectory()) {
        this.routes.GET[static_folders[folder]] = { "*": this.controllers.static };
      }
    }
  }

  if (opts.env) {
    // TODO: make path part of config?
    this.routes.GET["/"]["env"] = function(pathinfo, request, response) {
      response.writeHead(200, {"content-type": "application/json"}); // TODO switch to HTML on extname
      var data = { env: process.env, uid: process.getuid(), os: {} };
      var os = require("os");
      ["hostname", "type", "platform", "arch", "release", "networkInterfaces", "cpus"].forEach(function (a) {
        data.os[a] = os[a]();
      });
      if (process.env.HOME) data.home = fs.readdirSync(process.env.HOME);
      // data.passwd = require("getpw").getpwuid(data.uid); (TODO: pass to constructor to eliminate hard dep)
      response.end(JSON.stringify(data, null, "  "));
    }
  }

  this.redirect = function(new_url, response) {
      response.writeHead(302, { 'Location': new_url });
      response.end();
  };

  this.on("notFound", this.controllers.notFound);

  this.route = function(request, response, body) {
    var parsed_url = url.parse(request.url, true);
    var method = request.method,
        dirname = path.dirname(parsed_url.pathname).replace(/\/*$/, ''), // strip any trailing slash
        extname = path.extname(parsed_url.pathname), // TODO: should have a default for extname?
        basename = path.basename(parsed_url.pathname, extname) || "index"; // TODO: configurable default basename?

    console.log("%s %s/%s%s", method, dirname, basename, extname); // TODO: add an on/off switch for this logging

    // TODO: add a switch for case (in)sensitivity
    // dirname = dirname.toLowerCase();
    // basename = basename.toLowerCase();

    var controller = this.controllers.notFound;
    if (this.routes[method] && this.routes[method][dirname]) {
       // find the most specific match possible
       var candidate, candidates = [basename + extname, basename, "*" + extname, "*"];
       while (candidate = candidates.shift()) {
         if (candidate in this.routes[method][dirname]) {
             controller = this.routes[method][dirname][candidate];
             break; // while
         }
       }
    }
    return controller({ dirname: dirname, basename: basename, extname: extname, query: parsed_url.query }, request, response, body);
  };
}
Ejemplo n.º 9
0
    geddy.server.addListener('request', function (req, resp) {
      var reqObj
        , params
        , urlParams
        , method
        , ctor
        , appCtor
        , baseController
        , controller
        , controllerName
        , actionName
        , staticPath
        , staticResp
        , err
        , errResp
        , body = ''
        , bodyParams
        , steps = {
            parseBody: false
          , sessions: false
          }
        , finish
        , accessTime = (new Date()).getTime()
        , inFlighId;

      // Buffered request-obj -- buffer the request data,
      // and pass this proxy object to the controller
      reqObj = new Request(req);

      finish = function (step) {
        steps[step] = true;
        for (var p in steps) {
          if (!steps[p]) {
            return false;
          }
        }
        controller._handleAction.call(controller, params.action);
        reqObj.sync(); // Flush buffered events and begin emitting
      };

      if (router) {
        urlParams = url.parse(req.url, true).query;

        if (req.method.toUpperCase() == 'POST') {
          // POSTs may be overridden by the _method param
          if (urlParams._method) {
              method = urlParams._method;
          }
          // Or x-http-method-override header
          else if (req.headers['x-http-method-override']) {
            method = req.headers['x-http-method-override'];
          }
          else {
            method = req.method;
          }
        }
        else {
          method = req.method;
        }
        // Okay, let's be anal and force all the HTTP verbs to uppercase
        method = method.toUpperCase();

        inFlightId = geddy.inFlight.addEntry({
          request: req
        , method: method
        , response: resp
        , accessTime: accessTime
        });
        req._geddyId = inFlightId;
        resp._geddyId = inFlightId;

        // FIXME: Holy shit, all inline here
        resp.addListener('finish', function () {
          var id = resp._geddyId
            , entry = geddy.inFlight.getEntry(id)
            , req = entry.request
            , stat = resp.statusCode
            , level = parseInt(stat, 10) > 499 ? 'error' : 'access'
            , endTime = new Date();
          // Apache extended log-format
          geddy.log[level](req.connection.remoteAddress + ' ' +
              '- ' +
              '- ' +
              '[' + new Date(entry.accessTime) + '] ' +
              '"' + entry.method + ' ' + req.url + ' ' +
                  req.httpVersion + '" ' +
              stat + ' ' +
              (resp._length || '-') + ' ' +
              '"' + (req.headers['referer'] || '-') + '" ' +
              '"' + (req.headers['user-agent'] || '-') + '" ');
          geddy.inFlight.removeEntry(id);
          if (geddy.config.metrics) {
            allRequestTimer.update(endTime - accessTime);
            controllerActionTimers[controllerName] = controllerActionTimers[controller] || {}
            controllerActionTimers[controllerName][actionName] =
                controllerActionTimers[controllerName][actionName] ||
                geddy.metrics.Timer("Geddy."+controllerName + '.' + actionName)
            controllerActionTimers[controllerName][actionName].update(endTime - accessTime);
          }
        });

        params = router.first(req, method);
      }

      if (params) {
        if (params instanceof Error) {
          err = new errors.MethodNotAllowedError(method + ' method is not allowed.');
          errResp = new response.Response(resp);
          errResp.send(err.message, err.statusCode, {
            'Content-Type': 'text/html'
          , 'Allow': params.allowed.join(', ')
          });
        }
        else {
          controllerName = params.controller;
          actionName = params.action;
          ctor = ctors[params.controller];
          if (ctor) {
            // Merge params from the URL and the query-string to produce a
            // Grand Unified Params object
            geddy.mixin(params, urlParams);

            // If it's a plain form-post, save the request-body, and parse it into
            // params as well
            if ((req.method == 'POST' || req.method == 'PUT') &&
                (req.headers['content-type'] &&
                  (req.headers['content-type'].indexOf('form-urlencoded') > -1 ||
                   req.headers['content-type'].indexOf('application/json') > -1))) {
              req.addListener('data', function (data) {
                body += data.toString();
              });
              // Handle the request once it's finished
              req.addListener('end', function () {
                bodyParams = querystring.parse(body);
                geddy.mixin(params, bodyParams);
                req.body = body;
                finish('parseBody');
              });
            }
            else {
              finish('parseBody');
            }

            // If there's an Application controller, use an instance of it
            // as the prototype of the controller for this request
            if (ctors.Application) {
              // The constructor for the Application controller
              appCtor = ctors.Application;
              // Give the constructor a new prototype to inherit from: an instance
              // of the BaseController, with the methods/properties from its original
              // prototype copied over
              appCtor.prototype = utils.enhance(new BaseController(),
                  appCtor.origPrototype);
              // Make an instance -- this instance will be the proto for the
              // controller for this request
              baseController = new appCtor();
            }
            // If there's no Application controller, use an instance of
            // BaseController as the proto
            else {
              baseController = new BaseController();
            }

            // Give the constructor for this request's controller a new
            // prototype: the Application/BaseController (or just BaseController)
            // instance, with the methods/properties from the original prototype
            // copied over
            ctor.prototype = utils.enhance(baseController,
                ctor.origPrototype);
            // Instantiate the controller
            controller = new ctor();

            // Now that there's a controller, record the access-time
            controller.accessTime = accessTime;

            if (typeof controller[params.action] == 'function') {
              controller.app = self;
              controller.request = reqObj;
              controller.response = resp;
              controller.method = method;
              controller.params = params;
              controller.name = params.controller;
              controller.i18n = new i18n.I18n(controller);

              // We have a controller instance; register it with the
              // in-flight data
              geddy.inFlight.updateEntry(req._geddyId, {
                controller: controller
              });

              controller.cookies = new CookieCollection(req);

              if (geddy.config.sessions) {
                controller.session =
                    new sessions.Session(controller, function () {
                  finish('sessions');
                });
              }
              else {
                finish('sessions');
              }
            }
            // No action, 500 error
            else {
              err = new errors.InternalServerError('No ' + params.action +
                  ' action on ' + params.controller + ' controller.');
              errResp = new response.Response(resp);
              errResp.send(err.message, err.statusCode,
                  {'Content-Type': 'text/html'});
            }
          }
          // no controller, 500 error
          else {
            err = new errors.internalservererror('controller ' +
                params.controller + ' not found.');
            errresp = new response.response(resp);
            errresp.send(err.message, err.statuscode,
                {'content-type': 'text/html'});
          }
        }
      }
      // Either static or 404
      else {
        // Get the path to the file
        staticPath = geddy.config.staticFilePath + '/' + req.url;
        // Ignore querystring
        staticPath = staticPath.split('?')[0];
        // Decode path (e.g. %20)
        staticPath = decodeURIComponent(staticPath);

        if (path.existsSync(staticPath)) {
          staticResp = new response.Response(resp);
          staticResp.sendFile(staticPath);
        }
        else {
          err = new errors.NotFoundError(req.url + ' not found.');
          errResp = new response.Response(resp);
          errResp.send(err.message, err.statusCode,
              {'Content-Type': 'text/html'});
        }
      }
    });
Ejemplo n.º 10
0
exports.exists = function (path, fn) {
    //for node v8 it'll give deprecation warning
    return isValidCallback(fn) ? pathModule.exists(path, fn) : pathModule.existsSync(path);
};
Ejemplo n.º 11
0
var version = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf-8'))["version"];
program.version(version)
  .option('-p, --port <port>', 'server port [8080]', Number, 8080);
program.parse(process.argv);

// Boot
var app = http.createServer(handler),
    io = socketIo.listen(app);
app.listen(program.port);
console.log("Listening for connections on :" + program.port);
var file = new(nodeStatic.Server)(__dirname + '/../public');

// Load Pivotal Tracker if credentials file exists at .pivotal_credentials.json
var pivotalCredentialsPath = __dirname + '/../.pivotal_credentials.json',
  pivotalCredentials;
if (path.existsSync(pivotalCredentialsPath)) {
  pivotalCredentials = JSON.parse(fs.readFileSync(".pivotal_credentials.json", 'utf8'));
}

function handler (req, rsp) {
  req.addListener('end', function () {
    file.serve(req, rsp);
  });
}

var iteration = new models.Iteration();

if (pivotalCredentials) {
  pivotal.getToken(pivotalCredentials.username, pivotalCredentials.password, function (token) {
    pivotal.getCurrentIteration(pivotalCredentials.projectId, token, function (results) {
      var unpointedStories = results.iterations.iteration.map(function (iteration) {
Ejemplo n.º 12
0
    shell.cmd('coffee start', 'Start CoffeeScript', function(req, res, next) {
        var args = [];
        var pipeStdout = settings.stdout && shell.isShell && !settings.detach;
        var pipeStderr = settings.stderr && shell.isShell && !settings.detach;
        // Before compiling, concatenate all scripts together in the
        // order they were passed, and write them into the specified
        // file. Useful for building large projects.
        if(settings.join){
            args.push('-j');
            args.push(enrichFiles(settings.join));
        }
        // Watch the modification times of the coffee-scripts,
        // recompiling as soon as a change occurs.
        args.push('-w');
        // If the jsl (JavaScript Lint) command is installed, use it
        // to check the compilation of a CoffeeScript file. (Handy
        // in conjunction with --watch)
        if(settings.lint){
            args.push('-l');
        }
        // Load a library before compiling or executing your script.
        // Can be used to hook in to the compiler (to add Growl
        // notifications, for example).
        if(settings.require){
            args.push('-r');
            args.push(settings.require);
        }
        // Compile the JavaScript without the top-level function
        // safety wrapper. (Used for CoffeeScript as a Node.js module.)
        args.push('-b');
        /* Not sure if this apply to wath mode
        // The node executable has some useful options you can set,
        // such as --debug and --max-stack-size. Use this flag to
        // forward options directly to Node.js. 
        if(settings.nodejs){
            args.push('--nodejs');
            args.push(settings.nodejs);
        }
        */
        // Write out all compiled JavaScript files into the specified
        // directory. Use in conjunction with --compile or --watch.
        if(!settings.output && path.existsSync(settings.workspace+'/lib')){
            settings.output = settings.workspace+'/lib/';
        }
        if(settings.output){
            args.push('-o');
            args.push(enrichFiles(settings.output));
        }
        // Compile a .coffee script into a .js JavaScript file
        // of the same name.
        if(!settings.compile && path.existsSync(settings.workspace+'/src')){
            settings.compile = settings.workspace+'/src/';
        }
        if(settings.compile){
            args.push('-c');
            args.push(enrichFiles(settings.compile));
        }
        if(!pipeStdout){
        	args.push('>');
        	args.push(settings.stdout);
        }
        if(!pipeStderr){
        	args.push('2>');
        	args.push(settings.stderr);
        }
        args.unshift('coffee');
        console.log(args.join(' '));
        coffee = exec(args.join(' '));
        if(pipeStdout){
            coffee.stdout.pipe(
                typeof settings.stdout === 'string'
                ? fs.createWriteStream(settings.stdout)
                : settings.stdout
            );
        }
        if(pipeStderr){
            coffee.stderr.pipe(
                typeof settings.stderr === 'string'
                ? fs.createWriteStream(settings.stderr)
                : settings.stderr
            );
        }
        if(!shell.isShell || settings.detach){
            var pidfile = settings.pidfile || '/tmp/coffee.pid';
            fs.writeFileSync(pidfile,''+coffee.pid);
        }
		// Give a chance to coffee to startup
        setTimeout(function(){
			if(coffee){
				res.cyan('CoffeeScript started').ln();
			}
        	res.prompt();
        },500);
    });
Ejemplo n.º 13
0
var checkForToDir = function(toDir) {
	if (path.existsSync(toDir)) {
		process.stdout.write('Cannot write to ' + toDir + ': already exists\n');
		process.exit(-1);
	}
};
Ejemplo n.º 14
0
	// Set up our filter
	filter = ['.svn', '.hg', '.hgcheck', '.hgignore', '.hgtags', '.project', 'projects', '.settings'];

	if (type === 'core') {
		checkForToDir(toDir);
		// Copy only the core Hemi library
		filter.push('lib', 'app.js', 'build.js', 'PublishReadMe',
			'PublishTemplate.html', 'editor', 'editor.min.js', 'editor.src.js', 'hemi', 'parse.js');
		copyFiles('.', toDir, false);
		copyFiles('./public/js', toDir, true);

		if (docs) {
			var docDir = './public/doc';

			if (path.existsSync(docDir)) {
				copyFiles(docDir, toDir + '/doc', true);
			}
		}
	} else if (type === 'uglify') {
		uglifyFile(toDir, '');
	} else if (type == 'uglifyHemi') {
		uglifyHemi('./public/js', toDir);
	} else if (type == 'uglifyEditor') {
		uglifyEditor('./public/js', toDir);
	} else if (type == 'fixCTR') {
		fixTexRef(toDir);
	} else {
		checkForToDir(toDir);

		// Unless building a full package, do not copy samples
	removeOutfile: function() {
		!program.keep && this.started && path.existsSync( this.outPath ) && fs.unlinkSync( this.outPath );
		return this;
	}, 
Ejemplo n.º 16
0
exports.loadGlobalStyles = function(appPath, opts) {
	// reset the global style array
	exports.globalStyle = [];

	// validate/set arguments
	opts = opts || {};
	var ret = false;
	var theme = opts.theme;
	var apptss = CONST.GLOBAL_STYLE;
	var stylesDir = path.join(appPath,CONST.DIR.STYLE);
	var themesDir;
	if (theme) {
		themesDir = path.join(appPath,'themes',theme,CONST.DIR.STYLE);
	}
	var buildlog = BuildLog();
	var cacheFile = path.join(appPath, '..', CONST.DIR.BUILD, GLOBAL_STYLE_CACHE);

	// create array of global styles to load based on arguments
	var loadArray = [];
	loadArray.push({
		path: path.join(stylesDir,apptss),
		msg: apptss
	});
	if (theme) {
		loadArray.push({
			path: path.join(themesDir,apptss),
			msg: apptss + '(theme:' + theme + ')',
			obj: { theme: true }
		});
	}
	loadArray.push({
		path: path.join(stylesDir,platform,apptss),
		msg: apptss + '(platform:' + platform + ')',
		obj: { platform: true }
	});
	if (theme) {
		loadArray.push({
			path: path.join(themesDir,platform,apptss),
			msg: apptss + '(theme:' + theme + ' platform:' + platform + ')',
			obj: { platform: true, theme: true }
		});
	}

	// get rid of entries that don't exist
	var len = loadArray.length;
	for (var i = len - 1; i >= 0; i--) {
		if (!path.existsSync(loadArray[i].path)) {
			loadArray.splice(i, 1);
		}
	}

	// create hash of existing global styles
	var hash = U.createHash(_.pluck(loadArray, 'path'));

	// see if we can use the cached global style
	if (buildlog.data.globalStyleCacheHash === hash && fs.existsSync(cacheFile)) {

		// load global style object from cache
		logger.info('[global style] loading from cache...');
		exports.globalStyle = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
		ret = true;

	} else {

		// add new hash to the buildlog
		buildlog.data.globalStyleCacheHash = hash;

		// create the new global style object
		_.each(loadArray, function(g) {
			if (path.existsSync(g.path)) {
				logger.info('[' + g.msg + '] global style processing...');
				exports.globalStyle = exports.loadAndSortStyle(g.path, _.extend(
					{ existingStyle: exports.globalStyle },
					g.obj || {}
				));
			}
		});

		// write global style object to cache
		logger.info('[global style] writing to cache...');
		fs.writeFileSync(cacheFile, JSON.stringify(exports.globalStyle));

	}

	styleOrderCounter++;

	return ret;
};
	removeInfile: function() {
		program.delete && path.existsSync( this.inPath ) && fs.unlinkSync( this.inPath );
		return this;
	}
Ejemplo n.º 18
0
require('should')
var path = require('path')

var fn = __dirname + "/log/wf00_all.log";
var ret = path.existsSync(fn);
ret.should.equal(true);
Ejemplo n.º 19
0
    it("prepareOutputFiles() should copy files and unzip archive", function () {
        fileMgr.prepareOutputFiles(session);

        expect(path.existsSync(session.sourcePaths.CHROME)).toBeTruthy();
        expect(path.existsSync(session.sourcePaths.LIB)).toBeTruthy();
    });
Ejemplo n.º 20
0
Archivo: v.js Proyecto: randylien/volo
 exists: function (filePath) {
     return path.existsSync(filePath);
 },
Ejemplo n.º 21
0
var fs = require('fs'),
    path = require('path'),
    srcFile = process.argv[2];

    console.log('changing '+ srcFile);
if(!srcFile){
  console.log('fail');
  process.exit(1);
}

if(path.existsSync(path.dirname(srcFile)+'/../.git')){
  console.log('do NOT run this in development');
  process.exit(1);
}

fs.readFile(srcFile, 'utf-8', function(err, data) {
  if(err) throw err;
  console.log('replacing');
  console.log(/<!--START-SCRIPT-TAGS(.|\s|\n)*END-SCRIPT-TAGS-->/g.exec(data));

  data = data.replace(
    /<!--START-SCRIPT-TAGS(.|\s|\n)*END-SCRIPT-TAGS-->/g, 
    '<script src="assets/js/lxjs.min.js"></script>'
  );

  console.log('writing');
  fs.writeFile(srcFile, data, 'utf-8', function(err) {
    if(err) throw err;
    console.log('file '+srcFile+' is ready');
  });
});
Ejemplo n.º 22
0
	grunt.registerTask( 'custom_init', 'ensure the output directory is present', function() {
		if( !path.existsSync(config.dirs.output) ) {
			fs.mkdirSync( config.dirs.output );
		}
	});
Ejemplo n.º 23
0
	afterEach(function() {
		if (path.existsSync(testFile)) fs.unlinkSync(testFile);
		assert.ok(!path.existsSync(testFile), "Could not delete test file: " + testFile);
	});
Ejemplo n.º 24
0
module.exports = function(grunt) {
  var path = require('path');
  var settingsFile = "settings.json"
  var file = path.existsSync(settingsFile) ? settingsFile : "settings.json.default";
  var settings = grunt.file.readJSON(file);

  grunt.initConfig({

    // The clean task ensures all files are removed from the dist/ directory so
    // that no files linger from previous builds.
    clean: ["dist/"],

    // The lint task will run the build configuration and the application
    // JavaScript through JSHint and report any errors.  You can change the
    // options for this task, by reading this:
    // https://github.com/cowboy/grunt/blob/master/docs/task_lint.md
    lint: {
      files: [
        "build/config.js", "app/**/*.js"
      ]
    },

    // The jshint option for scripturl is set to lax, because the anchor
    // override inside main.js needs to test for them so as to not accidentally
    // route.
    jshint: {
      options: {
        scripturl: true
      }
    },

    // The jst task compiles all application templates into JavaScript
    // functions with the underscore.js template function from 1.2.4.  You can
    // change the namespace and the template options, by reading this:
    // https://github.com/gruntjs/grunt-contrib/blob/master/docs/jst.md
    //
    // The concat task depends on this file to exist, so if you decide to
    // remove this, ensure concat is updated accordingly.
    jst: {
      "dist/debug/templates.js": [
        "app/templates/**/*.html"
      ]
    },

    // The handlebars task compiles all application templates into JavaScript
    // functions using Handlebars templating engine.
    //
    // Since this task defaults to writing to the same file as the jst task,
    // edit the debug task replacing jst with handlebars.
    //
    // The concat task depends on this file to exist, so if you decide to
    // remove this, ensure concat is updated accordingly.
    handlebars: {
      "dist/debug/templates.js": ["app/templates/**/*.html"]
    },

    // The concatenate task is used here to merge the almond require/define
    // shim and the templates into the application code.  It's named
    // dist/debug/require.js, because we want to only load one script file in
    // index.html.
    concat: {
      dist: {
        src: [
          "assets/js/libs/almond.js",
          "dist/debug/templates.js",
          "dist/debug/require.js"
        ],

        dest: "dist/debug/assets/js/require.js",

        separator: ";"
      },
      debug: {
        src: ['assets/css/bootstrap.css'],
        dest: 'dist/debug/assets/css/index.css'
      }

    },

    // This task uses the MinCSS Node.js project to take all your CSS files in
    // order and concatenate them into a single CSS file named index.css.  It
    // also minifies all the CSS as well.  This is named index.css, because we
    // only want to load one stylesheet in index.html.
    mincss: {
      "dist/release/index.css": [
        "assets/css/h5bp.css"
      ]
    },

    // Takes the built require.js file and minifies it for filesize benefits.
    min: {
      "dist/release/require.js": [
        "dist/debug/require.js"
      ]
    },

    // Running the server without specifying an action will run the defaults,
    // port: 8000 and host: 127.0.0.1.  If you would like to change these
    // defaults, simply add in the properties `port` and `host` respectively.
    // Alternatively you can omit the port and host properties and the server
    // task will instead default to process.env.PORT or process.env.HOST.
    //
    // Changing the defaults might look something like this:
    //
    // server: {
    //   host: "127.0.0.1", port: 9001
    //   debug: { ... can set host and port here too ...
    //  }
    //
    //  To learn more about using the server task, please refer to the code
    //  until documentation has been written.
    server: {
      // Ensure the favicon is mapped correctly.
      files: { "favicon.ico": "favicon.ico" },

      debug: {
        // Ensure the favicon is mapped correctly.
        files: { "favicon.ico": "favicon.ico" },

        // Map `server:debug` to `debug` folders.
        folders: {
          "app": "dist/debug",
          "assets/js/libs": "dist/debug"
        }
      },

      release: {
        // This makes it easier for deploying, by defaulting to any IP.
        host: "0.0.0.0",

        // Ensure the favicon is mapped correctly.
        files: { "favicon.ico": "favicon.ico" },

        // Map `server:release` to `release` folders.
        folders: {
          "app": "dist/release",
          "assets/js/libs": "dist/release",
          "assets/css": "dist/release"
        }
      }
    },

    // This task uses James Burke's excellent r.js AMD build tool.  In the
    // future other builders may be contributed as drop-in alternatives.
    requirejs: {
      // Include the main configuration file.
      mainConfigFile: "app/config.js",

      // Output file.
      out: "dist/debug/require.js",

      // Root application module.
      name: "config",

      // Do not wrap everything in an IIFE.
      wrap: false
    },

    // The headless QUnit testing environment is provided for "free" by Grunt.
    // Simply point the configuration to your test directory.
    qunit: {
      all: ["test/qunit/*.html"]
    },

    // The headless Jasmine testing is provided by grunt-jasmine-task. Simply
    // point the configuration to your test directory.
    jasmine: {
      all: ["test/jasmine/*.html"]
    },
    // Copy build artifacts and library code into the distribution

    mkcouchdb: settings.couchdb,
    rmcouchdb: settings.couchdb,
    couchapp: settings.couchdb,
    // Create static html files from templates, for managing change of script
    // or css name.
    template: {
      release: {
        src: 'assets/index.underscore',
        dest: 'dist/release/index.html',
        variables: {
          assets_root: '/',
          requirejs: 'require.min.js',
          base: '/'
        }
      },
      debug: {
        src: 'assets/index.underscore',
        dest: 'dist/debug/index.html',
        variables: {
          assets_root: '/',
          requirejs: 'require.js',
          base: '/'
        }
      },
      default: {
        src: 'assets/index.underscore',
        dest: 'dist/debug/index.html',
        variables: {
          //assets_root: '/blouch/_design/blouch/',
          assets_root: '/assets/',
          requirejs: 'require.js',
          //base: '/blouch/_design/blouch/index.html'
          base: '/blouch/_design/blouch/index.html'
        }
      }
    }
  });

  grunt.registerMultiTask('template', 'generates an html file from a specified template', function(){
    var data = this.data;
    var blouchSettings = settings.blouch[this.target] || settings.blouch.default;
    console.log("DATA", this, data);
    console.log("CONFIG", grunt.config.get('loadSettings'));

    var _ = grunt.utils._;
    var vars = _.extend(data.variables, {
      blouch: blouchSettings
    });
    console.log("VARS", vars);
    var tmpl = _.template(grunt.file.read(data.src), null, vars);
    grunt.file.write(data.dest, tmpl(vars));
  });

  // Load the couchapp task
  grunt.loadNpmTasks('grunt-couchapp');
  //grunt.loadNpmTasks('./helper');
  // Load the copy task

  // The debug task will remove all contents inside the dist/ folder, lint
  // all your code, precompile all the underscore templates into
  // dist/debug/templates.js, compile all the application code into
  // dist/debug/require.js, and then concatenate the require/define shim
  // almond.js and dist/debug/templates.js into the require.js file.
  grunt.registerTask("debug", "clean lint jst requirejs concat");

  // The release task will run the debug tasks and then minify the
  // dist/debug/require.js file and CSS files.
  grunt.registerTask("release", "debug min mincss");
  grunt.registerTask("couchdb", "test build minify template:couchdb copy:couchdb");
  grunt.registerTask("couchapp_setup", "debug template:default");
  grunt.registerTask("couchapp_dev", "couchapp_setup couchapp:default");
};
Ejemplo n.º 25
0
 'built_assets/images/b.png'].forEach(function(file) {
   PATH.existsSync(PATH.resolve(buildir, file)).should.equal(true, file);
 });
Ejemplo n.º 26
0
	req.on('end', function () {
		var deps = [],
			depsArray = [],
			allowBuild = true;

		// anything in the ?build= arg? needs to be comma separated.
		if (query.build) {
			// make a file that requires all
			deps = query.build.split(',');

			console.log('Created a custom include with ' + deps.join(', '));

			fs.writeFile(file, deps.join(','), function(error) {});
			appBuild.include = deps;
		}
		else if (u.pathname != '/' && u.pathname.length == 10 && u.pathname.match(/\/([A-Z0-9]+)/)) {
			id = u.pathname.replace('/', '');
			// see if the old build exists
			var orig = './hash/epitome-' + id + '.js';

			if (!path.existsSync(orig)) {
				respond(res, 404, 'Failed to find existing build for ' + id);
				allowBuild = false;
			}
			else {
				console.log('Found existing hash id, rebuilding...');
			}
		}
		else {
			console.log('No custom includes found, returning main instead');
			id = 'base.js';
			deps = appBuild.include;
		}

		// set output folder in out for quick reference
		appBuild.out = '../out/epitome-'+ id +'-min.js';

		var handleBuilding = function() {
			console.log('[LOG] running: r.js -o ./hash/' + id + '.json');

			try {
                // read original
                fs.readFile('./hash/' + id + '.json', function(error, build) {
                    var ab = JSON.parse(build);


                    ps.exec('r.js -o ./hash/' + id + '.json', function(error, output) {
                        console.log(output);

                        // if error, output 500 internal code with dump
                        if (error) {
                            respond(res, 500, error.toString());
                            return;
                        }



                        // read the generated file and pipe through to stdout (er, browser).
                        fs.readFile('./out/epitome-' + id + '-min.js', function(error, contents) {
                            // add a hash so same build config can be reused.
                            contents = '/*Epitome hash: ' + id + '\n  Download: http://' + req.headers.host + '/' + id +'\n  Selected: ' +  ab.include.join(', ') + ' */\n' + contents;
                            respond(res, 200, contents);

                            // clean up the out file also, we can rebuild
                            /*fs.unlink('./out/epitome-' + id + '-min.js', function(error) {
                                if (error)
                                    throw error;
                                console.log('deleted built js file');
                            });*/
                        });
                    });
                }); // json read
			} catch (e) {
				// something went wrong.
				// respond(res, 500, e.toString());
			}
		};

		fs.writeFile('./hash/' + id + '.json', JSON.stringify(appBuild), function() {
			// what we will actually run now
			if (allowBuild)
				handleBuilding();
		});
	});
Ejemplo n.º 27
0
Archivo: ini.js Proyecto: jeisc/npm
  , ini = require("./ini-parser")
  , sys = require("sys")
  , hasSSL = false
  , crypto
  , base64 = require("./base64")

try {
  crypto = process.binding("crypto") && require("crypto")
  hasSSL = true
} catch (ex) {
  crypto = require("crypto")
}

var sys = require("sys")
  , defaultConfig = require("./default-config")
  , configfile = path.existsSync(defaultConfig.configFile)
               ? defaultConfig.configFile
               : path.join(process.env.HOME, '.npmrc')
  , config = getConfig() || {}
  , privateKey

exports.config = config
exports.save = save
exports.del = del
exports.get = get
exports.set = set

function getConfig () {
  // TODO: --config <path> on the cli to set this.
  var config
  log(configfile, "configfile")
Ejemplo n.º 28
0
var assertPath = function(test, path, shouldExist, message) {
    test.ok(Path.existsSync(path) == shouldExist, message || "");
};
Ejemplo n.º 29
0
Wrap.prototype.require = function (mfile, opts) {                           // 
    var self = this;                                                        // 
    if (!opts) opts = {};                                                   // 
    if (!opts.basedir) opts.basedir = process.cwd();                        // 
    // 
    if (typeof mfile === 'object') {                                        // 
        return self.requireMultiple(mfile, opts);                           // 
    }                                                                       // 
    // 
    if (self.ignoring[mfile]) return self;                                  // 
    if (self.aliases[mfile]) return self;                                   // 
    // 
    var pkg = {};                                                           // 
    if (resolve.isCore(mfile)) {                                            // 
        var file = path.resolve(__dirname, '../builtins/' + mfile + '.js'); // 
        opts.target = opts.target || mfile;                                 // 
        // 
        if (!path.existsSync(file)) {                                       // 
            try {                                                           // 
                require.resolve(mfile + '-browserify');                     // 
                opts.body = 'module.exports = require('                     // 
                + JSON.stringify(mfile + '-browserify')                     // 
                + ')';                                                      // 
            }                                                               // 
            catch (err) {                                                   // 
                throw new Error('No wrapper for core module ' + mfile);     // 
            }                                                               // 
        }                                                                   // 
    }                                                                       // 
    else if (self.has(mfile, '/node_modules/' + mfile + '/index.js')        // 
        || self.has(mfile, '/node_modules/' + mfile + '/package.json')) {   // 
        // package has already been included in some fashion, no need to res// 
        return self;                                                        // 
    }                                                                       // 
    else if (opts.body) {                                                   // 
        var file = mfile;                                                   // 
    }                                                                       // 
    else {                                                                  // 
        try {                                                               // 
            var file = self.resolver(mfile, opts.basedir);                  // 
        }                                                                   // 
        catch (err) {                                                       // 
            throw new Error('Cannot find module ' + JSON.stringify(mfile)   // 
                + ' from directory ' + JSON.stringify(opts.basedir)         // 
            );                                                              // 
        }                                                                   // 
    }                                                                       // 
    // 
    opts.basedir = path.dirname(file);                                      // 
    // 
    if (!opts.root && mfile.match(/^\//)) {                                 // 
        opts.root = opts.basedir;                                           // 
    }                                                                       // 
    // 
    if (!opts.target) {                                                     // 
        opts.target = makeTarget(file, opts.root);                          // 
    }                                                                       // 
    // 
    if (self.ignoring[opts.target]) return self;                            // 
    if (self.has(file, opts.target)) return self;                           // 
    // 
    var pkgfile = opts.basedir + '/package.json';                           // 
    if (!mfile.match(/^(\.\.?)?\//)) {                                      // 
        try {                                                               // 
            pkgfile = resolve.sync(mfile + '/package.json', {               // 
                basedir : opts.basedir                                      // 
            });                                                             // 
        }                                                                   // 
        catch (err) { }                                                     // 
    }                                                                       // 
    // 
    if (pkgfile && !self._checkedPackages[pkgfile]) {                       // 
        self._checkedPackages[pkgfile] = true;                              // 
        if (path.existsSync(pkgfile)) {                                     // 
            var pkgBody = fs.readFileSync(pkgfile, 'utf8');                 // 
            try {                                                           // 
                var npmpkg = JSON.parse(pkgBody);                           // 
                if (npmpkg.main !== undefined) {                            // 
                    pkg.main = npmpkg.main;                                 // 
                }                                                           // 
                if (npmpkg.browserify !== undefined) {                      // 
                    pkg.browserify = npmpkg.browserify;                     // 
                }                                                           // 
            }                                                               // 
            catch (err) {                                                   // 
                // ignore broken package.jsons just like node               // 
            }                                                               // 
            // 
            self.include(pkgfile, makeTarget(pkgfile, opts.root),           // 
                'module.exports = ' + JSON.stringify(pkg), opts.root        // 
            );                                                              // 
        }                                                                   // 
    }                                                                       // 
    // 
    var body = opts.body || self.readFile(file);                            // 
    self.include(file, opts.target, body, opts.root);                       // 
    // 
    try {                                                                   // 
        var required = self.detective.find(body);                           // 
    }                                                                       // 
    catch (err) {                                                           // 
        process.nextTick(function () {                                      // 
            self.emit('syntaxError', err);                                  // 
        });                                                                 // 
        return self;                                                        // 
    }                                                                       // 
    // 
    if (pkg.browserify && pkg.browserify.require) {                         // 
        required.strings = required.strings.concat(                         // 
            pkg.browserify.require                                          // 
        );                                                                  // 
    }                                                                       // 
    // 
    if (required.expressions.length) {                                      // 
        console.error('Expressions in require() statements:');              // 
        required.expressions.forEach(function (ex) {                        // 
            console.error('    require(' + ex + ')');                       // 
        });                                                                 // 
    }                                                                       // 
    // 
    nub(required.strings).forEach(function (req) {                          // 
        self.require(req, { basedir : opts.basedir, root : opts.root })     // 
    });                                                                     // 

    return self;                                                            // 
};                                                                          // 
Ejemplo n.º 30
0
var RestUtils = require('./RestUtils'),
	path = require('path'),
	outerror = require('./Outerror'),
	msg =  require('./msg/msg'),
	fs = require('fs'),
	baseDir = _restConfig.baseDir,
	respage = function(res, filepath, fn){
			fs.readFile(baseDir+filepath, 'utf-8', function (err, data) {
				 if (err) return fn(err);
				 res.statusCode = 404;
				 res.send(data);
				 fn(null, data);
			});		
	};
	if(!path.existsSync(baseDir+'/404.html')){//如果路径不存在,则创建它
		module.exports = function(res, filepath, fn){
		var fn = fn || function(){};		
			if(!filepath) {
				RestUtils.errorRes404(res);
				fn();
			}
			else respage(res, filepath, fn);
		};
	}
	else{
		module.exports = function(res, filepath, fn){
			var fn = fn || function(){};	
			if(!filepath) respage(res, '/404.html', fn);	
			else respage(res, filepath, fn);
		};
	}