Skip to content

Commit

Permalink
Add a file type, File
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethkufluk committed Jun 13, 2012
1 parent 893749b commit 5804d73
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
10 changes: 4 additions & 6 deletions lib/loadbuilder/analyzer.js
@@ -1,4 +1,5 @@
var parser = require('esprima');
var parser = require('esprima'),
File = require('./file');

function match(fragment, tree) {
// console.log(fragment, tree);
Expand Down Expand Up @@ -53,12 +54,9 @@ function walk(matcher, tree, parent, index) {

function analyze(matcher, source) {
// treeish can be source string, file object (from asset.js) or ast
if (source.mtime && !source.ast) {
source.ast = parser.parse(source.source);
}
var tree;
if (source.ast) {
tree = source.ast;
if (source instanceof File) {
tree = source.ast();
} else if (typeof source == 'object') {
tree = source;
} else {
Expand Down
18 changes: 6 additions & 12 deletions lib/loadbuilder/asset.js
Expand Up @@ -3,7 +3,7 @@ var util = require('./util'),
fs = require('fs'),
path = require('path'),
jshint = require('jshint').JSHINT,
escodegen = require('escodegen');
File = require('./file');

var USING = {
type: "CallExpression",
Expand Down Expand Up @@ -90,17 +90,11 @@ util.extend(Script.prototype, {
toSource: function() {
return this.deferWrapper(this.fromFile().source);
},
preProcess: function(data) {
return this.builder.preProcessor ? this.builder.preProcessor(data) : data;
},
fromFile: function() {
var fileInfo = fs.statSync(this.fullPath());
if (!this.file || this.file.mtime != fileInfo.mtime.getTime()) {
this.file = {
mtime: fileInfo.mtime.getTime(),
source: this.preProcess(fs.readFileSync(this.fullPath(), 'utf8'))
};
if (!this.file) {
this.file = new File(this.fullPath(), this.builder.preProcessor);
}
this.file.load();
return this.file;
},
fullPath: function() {
Expand Down Expand Up @@ -171,7 +165,7 @@ util.extend(Module.prototype, {
},
addId: function() {
var provides = analyzer.analyze(PROVIDE, this.fromFile()),
tree = this.fromFile().ast,
tree = this.fromFile().ast(),
provide;

if (provides.length > 0) {
Expand All @@ -185,7 +179,7 @@ util.extend(Module.prototype, {
});
}
}
return escodegen.generate(tree, { beautify: true });
return this.fromFile().generate({ beautify: true });
}
});

Expand Down
40 changes: 40 additions & 0 deletions lib/loadbuilder/file.js
@@ -0,0 +1,40 @@
var util = require('./util'),
fs = require('fs'),
esprima = require('esprima'),
escodegen= require('escodegen');

function File(fullPath, preProcessor) {
this.fullPath = fullPath;
this.preProcessor = preProcessor;
this.load();
}

util.extend(File.prototype, {
load: function() {
var fileInfo = fs.statSync(this.fullPath),
mtime = fileInfo.mtime.getTime();
if (!this.source || this.mtime != mtime) {
this.mtime = mtime;
this.source = this.preProcess(fs.readFileSync(this.fullPath, 'utf8'));
this.esprimaAst = null;
}
},
preProcess: function(data) {
return this.preProcessor ? this.preProcessor(data) : data;
},
ast: function() {
return this.esprimaAst = this.esprimaAst || esprima.parse(this.source);
},
generate: function(options) {
return escodegen.generate(this.ast(), options);
},
minified: function() {

},
});

function file(fullPath, preProcessor) {
return new File(fullPath, preProcessor);
}

module.exports = File;

0 comments on commit 5804d73

Please sign in to comment.