HTML5 Dev Gal

Messing with HTML5, CSS3 and JavaScript.

Dynamic Namespace Objects Based on Directory Structure and File Names

| Comments

In a previous post I showed how to create a dynamic namespace autoloader with node.js, express, and walk.

Please check out that post for information on how to set this up, what node modules to install and where to put this code.

In the http.createServer() function, add this code (or something like it):

(dynamicNamespaceObjectsFilenames.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port') + " in " + process.env.NODE_ENV + " mode.");

    var emitter,
        str,
        iter = [],
        startPath = "public/javascripts/poc",
        appName = "poc",
        filePath = "public/javascripts/poc/namespace.js";

    var log = fs.createWriteStream(filePath, {'flags': 'w'});
    var spacedStartPath = startPath.replace(/\//g, " ");
    str = "window." + appName + " = {} || " + appName + ";\r\n";
    emitter = walk.walk(startPath);

    function upperCaseMe (txt){
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    }

    emitter.on('file', function (path, stat, next) {
        if (stat.name[0] !== '.') {
            var filepath =  [path, '/', stat.name].join('');
            var noSlash =  filepath.replace(/\//g, " ");
            var stripStartPath =  noSlash.replace(spacedStartPath,"");
            var stripJs =  stripStartPath.replace ('.js',"");
            var uppercase = stripJs.replace(/\w\S*/g, upperCaseMe);
            if (uppercase.indexOf(" ") != -1) {
                uppercase = uppercase.replace(/ /g,".");
            };
            var pathArr = uppercase.split(".");
            var joined = [];
            var joinedStr = '';
            for (var i = 1; i < pathArr.length; i++) {
                joined.push(pathArr[i]);
                joinedStr += appName + '.' + joined.join(".") + " = {};\r\n";
            }

            iter.push(joinedStr);
        }

        next();
    });

    emitter.on('end', function () {
        var uniquePaths = []
        , uniqueEntries = []
        , end = "\r\n"
        , finalJoinedString = "";

        for (var i = 0; i < iter.length; i++) {
            if (uniquePaths.indexOf(iter[i]) == -1) {
                uniquePaths.push(iter[i]);
                str += iter[i];
            }
        }

        var strArray = str.split(end);
        for (var i = 0; i < strArray.length; i++) {
            if (uniqueEntries.indexOf(strArray[i]) == -1) {
                uniqueEntries.push(strArray[i]);
                finalJoinedString += strArray[i] + end;
            }
        }

        log.write(finalJoinedString);
    });

});

Please note this is kind of a mess of arrays so I’d be very grateful for any suggestions on how to make this cleaner.

Comments