Commits

kai zhu committed 8271a8c

refactor moduleLoad. move stringFormat to libGist.js.

Comments (0)

Files changed (4)

 #!/usr/bin/env node
+
 /*FILE_BEG {"actions": ["jslint", "eval", "watch"], "name": "cli.js"}*/
 /*jslint indent: 2, nomen: true, regexp: true, stupid: true*/
 (function () {
 
     moduleLoad: function (module) {
       //// custom module loader
-      var file, fnd, ii, rgx, code;
+      var content = EXPORTS.requireFs.readFileSync(module.filename, 'utf8');
       //// if module is a filename, create a new module with filename as module id
       if (typeof module === 'string') {
         module = EXPORTS.requirePath.resolve(EXPORTS.FS_CWD, module);
         module = EXPORTS.MODULES[module] || new EXPORTS.requireModule.Module(module);
       }
       EXPORTS.MODULES[module.id] = module;
-      rgx = (/^\/\*FILE_BEG (.*?)\*\/$([\S\s]*?)^\/\*FILE_END\*\/$/gm);
-      code = EXPORTS.requireFs.readFileSync(module.filename, 'utf8');
       //// bump up version
       EXPORTS.VERSION = new Date().toISOString().replace((/(....)-(..)-(..)T(..):(..):(..).*/), '$1.$2.$3-$4.$5.$6');
-      while (true) {
-        fnd = rgx.exec(code);
-        if (!fnd) { break; }
-        file = JSON.parse(fnd[1]);
+      return content.replace((/^\/\*FILE_BEG (.*?)\*\/$([\S\s]*?)^\/\*FILE_END\*\/$/gm), function (_, file, code, start) {
+        file = JSON.parse(file);
         //// filename
         file.name = file.name.replace((/\/FS_MODULE\b/), EXPORTS.FS_MODULE);
-        //// save pseudo-file's source code
-        EXPORTS.MODULE_FILE[file.name] = EXPORTS.stringFormat(
-          code.slice(0, rgx.lastIndex - fnd[0].length).replace((/[^\n]+/g), '') + fnd[2],
-          { EXPORTS: EXPORTS }
-        );
-        for (ii = 0; ii < file.actions.length; ii += 1) {
-          if (EXPORTS.MODULE_ACTION.hasOwnProperty(file.actions[ii])) {
-            EXPORTS.MODULE_ACTION[file.actions[ii]](module, file, EXPORTS.MODULE_FILE[file.name]);
-          }
-        }
-      }
+        //// preserve line number position
+        code = (content.slice(0, start).match('\n') || []).join('') + code;
+        //// save source code
+        EXPORTS.MODULE_FILE[file.name] = EXPORTS.stringFormat ? EXPORTS.stringFormat(code, { EXPORTS: EXPORTS }) : code;
+        file.actions.forEach(function (action) {
+          EXPORTS.MODULE_ACTION[action](module, file, EXPORTS.MODULE_FILE[file.name]);
+        });
+      });
     },
 
     moduleWatch: function (module) {
           typeof code === 'string' ? code.trim() : code
         );
       }
-    },
-
-    stringFormat: function (template, options) {
-      function getValue(keys) {
-        //// get value from options using keys
-        var fndKey, rgxKey = (/["']([^"'])+|(\w+)/g), val = options;
-        while (true) {
-          fndKey = rgxKey.exec(keys);
-          if (!(fndKey && val)) { break; }
-          val = val[fndKey[0] || fndKey[1]];
-        }
-        return val;
-      }
-      function format(code) {
-        //// format {{...}}
-        var args, result;
-        args = code.split('(');
-        result = getValue(args.pop());
-        try {
-          return result === undefined ? code : args.length ? getValue(args.pop())(result) : result;
-        } catch (err) {
-          return code;
-        }
-      }
-      return template.replace((/\{\{.+?\}\}/g), format);
     }
 
   };
 /*FILE_BEG {"actions": ["jslint", "coverSelf", "eval", "watch"], "name": "libGist.js"}*/
-/*jslint bitwise: true, indent: 2, nomen: true, stupid: true*/
+/*jslint bitwise: true, indent: 2, nomen: true, regexp: true, stupid: true*/
 (function () {
 
   'use strict';
     },
 
     hashSha256: function (str) {
-      var hash = EXPORTS.requireCrypto.createHash('sha256');
+      var hash = require('crypto').createHash('sha256');
       hash.update(typeof str === 'string' || Buffer.isBuffer(str) ? str : String(str));
       return hash.digest('hex');
     },
       reader.on('error', onEnd);
     },
 
+    stringFormat: function (template, options) {
+      function getValue(keys) {
+        //// get value from options using keys
+        var fndKey, rgxKey = (/["']([^"'])+|(\w+)/g), val = options;
+        while (true) {
+          fndKey = rgxKey.exec(keys);
+          if (!(fndKey && val)) { break; }
+          val = val[fndKey[0] || fndKey[1]];
+        }
+        return val;
+      }
+      return template.replace((/\{\{.+?\}\}/g), function (code) {
+        //// format {{...}}
+        var args, result;
+        args = code.split('(');
+        result = getValue(args.pop());
+        try {
+          return result === undefined ? code : args.length ? getValue(args.pop())(result) : result;
+        } catch (err) {
+          return code;
+        }
+      });
+    },
+
     uuid4: function () {
       //// return uuid4 string of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
       var uuid = '', ii;
     "istanbul": "*",
     "jslint": "*",
     "mime": "*",
-    "minify": "*",
     "nopt": "*",
     "tinyjs-external": "*",
-    "uglify-js": "*"
+    "uglify-js": "*",
+    "uglifycss": "*"
   },
   "description": "tinyjs is another nodejs micro-framework",
   "engines": {
 /*
 CHANGELOG
-migrate from connect to express
+refactor moduleLoad. move stringFormat to libGist.js.
 
 TODO
 auto-generate online documentation via yuidoc.
     "istanbul": "*",
     "jslint": "*",
     "mime": "*",
-    "minify": "*",
     "nopt": "*",
     "tinyjs-external": "*",
-    "uglify-js": "*"
+    "uglify-js": "*",
+    "uglifycss": "*"
   },
   "description": "tinyjs is another nodejs micro-framework",
   "engines": {
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
   },
-  "version": "2013.03.17-08.46.43"
+  "version": "2013.03.17-11.17.42"
 }