Skip to content

Commit b8a8f3c

Browse files
committed
Merge glob support by bgmort, fixes dcodeIO#8
1 parent 28f7b8f commit b8a8f3c

File tree

8 files changed

+64
-24
lines changed

8 files changed

+64
-24
lines changed

Preprocessor.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,46 @@
7575

7676
/**
7777
* Definition expression
78-
* @type {RegExp}
78+
* @type {!RegExp}
7979
*/
8080
Preprocessor.EXPR = /([ ]*)\/\/[ ]+#(include_once|include|ifn?def|if|endif|else|elif|put|define)/g;
8181

8282
/**
8383
* #include "path/to/file". Requires node.js' "fs" module.
84-
* @type {RegExp}
84+
* @type {!RegExp}
8585
*/
8686
Preprocessor.INCLUDE = /(include_once|include)[ ]+"([^"\\]*(\\.[^"\\]*)*)"[ ]*\r?(?:\n|$)/g;
8787

8888
/**
8989
* #ifdef/#ifndef SOMEDEFINE, #if EXPRESSION
90-
* @type {RegExp}
90+
* @type {!RegExp}
9191
*/
9292
Preprocessor.IF = /(ifdef|ifndef|if)[ ]*([^\r\n]+)\r?\n/g;
9393

9494
/**
9595
* #endif/#else, #elif EXPRESSION
96-
* @type {RegExp}
96+
* @type {!RegExp}
9797
*/
9898
Preprocessor.ENDIF = /(endif|else|elif)([ ]+[^\r\n]+)?\r?(?:\n|$)/g;
9999

100100
/**
101101
* #put EXPRESSION
102-
* @type {RegExp}
102+
* @type {!RegExp}
103103
*/
104104
Preprocessor.PUT = /put[ ]+([^\n]+)[ ]*/g;
105105

106106
/**
107107
* #define EXPRESSION
108-
* @type {RegExp}
108+
* @type {!RegExp}
109109
*/
110110
Preprocessor.DEFINE = /define[ ]+([^\n]+)\r?(?:\n|$)/g;
111111

112+
/**
113+
* @type {!RegExp}
114+
* @inner
115+
*/
116+
var GLOB_EXP = /(?:^|[^\\])\*/;
117+
112118
/**
113119
* Strips slashes from an escaped string.
114120
* @param {string} str Escaped string
@@ -232,11 +238,29 @@
232238
throw(new Error("Failed to resolve include: "+this.baseDir+"/"+include));
233239
}
234240
try {
235-
var key = include;
236-
include = require("fs").readFileSync(this.baseDir+"/"+include)+"";
237-
this.includes[key] = include;
241+
var key = include,
242+
fs = require("fs");
243+
if (GLOB_EXP.test(include)) {
244+
var glob = require("glob");
245+
verbose(' glob incl: '+this.baseDir+"/"+include);
246+
var _this = this;
247+
glob(this.baseDir+"/"+include, {"sync": true}, function(err, files) {
248+
if (err) throw(err);
249+
include = '';
250+
for (var i=0; i<files.length; i++) {
251+
verbose(' incl: '+files[i]);
252+
var contents = fs.readFileSync(files[i])+"";
253+
_this.includes[key] = contents;
254+
include += contents;
255+
}
256+
});
257+
} else {
258+
verbose(' incl: '+include);
259+
include = fs.readFileSync(this.baseDir+"/"+include)+"";
260+
this.includes[key] = include;
261+
}
238262
} catch (e) {
239-
throw(new Error("File not found: "+include+" ("+e+")"));
263+
throw(new Error("Include failed: "+include+" ("+e+")"));
240264
}
241265
}
242266
this.source = this.source.substring(0, match.index)+Preprocessor.indent(include, indent)+this.source.substring(Preprocessor.INCLUDE.lastIndex);

Preprocessor.min.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)