
var PATH = require("path"),
    FS = require("fs"),
    CONNECT = require("connect"),
    BUNDLER = require("sourcemint-platform-nodejs/lib/bundler");


exports.main = function(options) {

    // Rebuild bundles to get a clean start.

    console.log("Building fresh bundles from source ...");
    
    if (!PATH.existsSync(__dirname + "/dist")) {
        FS.mkdir(__dirname + "/dist", 0755);
    }

    BUNDLER.bundle(PATH.dirname(__dirname) + "/demo/kitchen-sink", __dirname + "/dist", {
        packageIdHashSeed: "__ACE__",
        forceCompleteBuild: true,
        writeManifest: true
    }).then(function() {

        console.log("... Done. Bundles will be updated as changes are detected in source files.");

        var server = CONNECT();

        server.use(CONNECT.router(function(app) {

            app.get(/^\/loader.js/, CONNECT.static(PATH.dirname(require.resolve("sourcemint-loader-js/loader.js"))));

            app.get(/^(\/demo\/kitchen-sink)(\.js)?(\/(.*))?$/, function (req, res) {

                req.url = req.params[2] || "";

                BUNDLER.Middleware(PATH.dirname(__dirname) + "/demo/kitchen-sink", __dirname + "/dist", {
                    packageIdHashSeed: "__ACE__",
                    // TODO: https://github.com/sourcemint/bundler-js/issues/3
                    rebuildChanges: false
                }).handle(req, res);
            });

            app.get(/^\//, function(req, res)
            {
                CONNECT.static(__dirname)(req, res, function()
                {
                    res.writeHead(404);
                    res.end("Not found!");
                });                
            });
        }));

        server.listen(options.port, "127.0.0.1");

        console.log("ACE development server running at http://127.0.0.1:" + options.port + "/");

    }, function(err) {
        console.error(err.stack);
    });
}

if (require.main === module) {
    // TODO: Make configurable via command-line flag.
    exports.main({
        port: 8888
    });
}

@@ -187,539 +187,8 @@
  %7B%0A%0A
-    // Rebuild bundles to get a clean start.%0A%0A    console.log(%22Building fresh bundles from source ...%22);%0A    %0A    if (!PATH.existsSync(__dirname + %22/dist%22)) %7B%0A        FS.mkdir(__dirname + %22/dist%22, 0755);%0A    %7D%0A%0A    BUNDLER.bundle(PATH.dirname(__dirname) + %22/demo/kitchen-sink%22, __dirname + %22/dist%22, %7B%0A        packageIdHashSeed: %22__ACE__%22,%0A        forceCompleteBuild: true,%0A        writeManifest: true%0A    %7D).then(function() %7B%0A%0A        console.log(%22... Done. Bundles will be updated as changes are detected in source files.%22);%0A%0A    
     
@@ -208,28 +208,24 @@
 CONNECT();%0A%0A
-    
     server.u
@@ -259,36 +259,32 @@
 app) %7B%0A%0A        
-   
- 
 app.get(/%5E%5C/load
@@ -374,36 +374,32 @@
 %22))));%0A%0A        
-    
 app.get(/%5E(%5C/dem
@@ -393,16 +393,18 @@
 .get(/%5E(
+?:
 %5C/demo%5C/
@@ -417,16 +417,18 @@
 n-sink)(
+?:
 %5C.js)?(%5C
@@ -432,123 +432,29 @@
 ?(%5C/
-(
 .*)
-)
 ?$/, 
-function (req, res) %7B%0A%0A                req.url = req.params%5B2%5D %7C%7C %22%22;%0A%0A                BUNDLER.Middleware
+BUNDLER.hoist
 (PAT
@@ -497,16 +497,52 @@
 n-sink%22,
+ %7B%0A            distributionBasePath:
  __dirna
@@ -554,27 +554,17 @@
 %22/dist%22,
- %7B%0A        
+%0A
         
@@ -613,175 +613,187 @@
     
-        // TODO: https://github.com/sourcemint/bundler-js/issues/3%0A                    rebuildChanges: false%0A                %7D).handle(req, res);%0A            %7D);%0A%0A    
+bundleLoader: false,%0A            logger: %7B%0A                log: function() %7B%0A                    console.log.apply(null, arguments);%0A                %7D%0A            %7D%0A        %7D));%0A%0A
     
@@ -834,34 +834,26 @@
 es)%0A        
-    %7B%0A    
+%7B%0A
             
@@ -907,33 +907,26 @@
             
-    %7B%0A   
+%7B%0A
             
@@ -925,25 +925,24 @@
             
- 
 res.writeHea
@@ -949,20 +949,16 @@
 d(404);%0A
-    
         
@@ -1000,20 +1000,16 @@
         
-   
- 
 %7D);     
@@ -1032,20 +1032,16 @@
     
-    
 %7D);%0A    
     
@@ -1032,38 +1032,30 @@
     %7D);%0A    
-  
-  
 %7D));%0A%0A
-    
     server.l
@@ -1085,28 +1085,24 @@
 7.0.0.1%22);%0A%0A
-    
     console.
@@ -1185,74 +1185,8 @@
 /%22);
-%0A%0A    %7D, function(err) %7B%0A        console.error(err.stack);%0A    %7D);
 %0A%7D%0A%0A

switch to incremental bundle rebuild on source change middleware