Skip to content

Instantly share code, notes, and snippets.

@AndreasMadsen
Created May 16, 2012 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasMadsen/2708337 to your computer and use it in GitHub Desktop.
Save AndreasMadsen/2708337 to your computer and use it in GitHub Desktop.
File converter module (piccolo project)

#File processor

Submodule to piccolo

The propuse is to create an abstraction for reading and processing static files. The source code of the static files should be in read and then be processed and copied to write so they don't have to be processed, even after an application crash. In order to know if the files has been changed since the even after an application crash crash, an map object between fs.Stat.modefied and the jsonFile.modefied will be stored.

  var module = require("module");
  
  var convert = module({
    read: "~/Site/modules/",
    write: "~/Site/temp/modules/",
    stat: "~/Site/temp/modules.stat.json"
  });
  
  // Ignore funny files
  convert.ignore(".DS_Store");
  
  // Will be executed on All JavaScript files
  convert.handle("js", function (content, next) {
    // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
    // because the buffer-to-string conversion in `fs.read()`
    // translates it to FEFF, the UTF-16 BOM.
    if (content.charCodeAt(0) === 0xFEFF) {
      content = content.slice(1);
    }
    next(content);
  });
  
  if (process.env["node_service"] !== "development") {
  
  var jsp = require("uglify-js").parser;
  var pro = require("uglify-js").uglify;
 
  // Will compress JavaScript files
  // You Can do convert.handle("css", cssCompressor) to compress CSS files
  convert.handle("js", function (content, next) {
    var ast = jsp.parse(content);
        ast = pro.ast_mangle(ast);
        ast = pro.ast_squeeze(ast);
    
     next(pro.gen_code(ast));
  });
  
  // no filter, all files will be gziped
  // should propperly be convert.handle(["js", "css", ...], convert.gzip);
  convert.handle(convert.gzip);
  
  // will watch the read dir for changes
  // and reconvert files when requested
  convert.watch();
  
  // Will store often used files in memory
  convert.memory("10 MB");
  
  // will convert all files in the read dir
  convert.compile(function (error) {
    if (error) throw error;

    // now ready
    // read File relative to read dir
    convert.read("/events.js", function (error, content) {
      if (error) throw error;
      
      // send content or whatever
      request.end(content);
    });
  });
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment