Skip to content

Commit

Permalink
Allow loading external files (urls)
Browse files Browse the repository at this point in the history
Now you use an url in the config:
files = [
  JASMINE,
  JASMINE_ADAPTER,
  'src/*.js',
  'http://code.jquery.com/jquery-1.7.2.min.js'
];

[changelog]
  • Loading branch information
vojtajina committed May 18, 2012
1 parent 0d52522 commit c55dbc8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
16 changes: 14 additions & 2 deletions lib/config.js
Expand Up @@ -71,7 +71,7 @@ var resolveSinglePattern = function(pattern, done) {
return null;
} else {
// it's a file - if end of pattern, accept the path
if (pointer === parts.length) results.push({path: path, mtime: stat.mtime});
if (pointer === parts.length) results.push({path: path, mtime: stat.mtime, isUrl: false});
return finish();
}
});
Expand All @@ -85,6 +85,8 @@ var resolveSinglePattern = function(pattern, done) {
* - unique files
* - absolute paths
*
* Keep absolute url patterns without checking them.
*
* @param {Array.<string>} patterns Array of patterns
* @param {Array.<string>} exclude Array of patterns to exlclude
* @param {function(err, files)} done Callback
Expand All @@ -99,6 +101,13 @@ var resolve = function(patterns, exclude, done) {
});

patterns.forEach(function(pattern, index) {

// By-pass absolute urls
if (util.isUrlAbsolute(pattern)) {
resultSets[index] = [{path: pattern, isUrl: true, mtime: null}];
return;
}

waiting++;

resolveSinglePattern(pattern, function(err, files) {
Expand Down Expand Up @@ -126,7 +135,7 @@ var resolve = function(patterns, exclude, done) {
log.debug('Excluded file ' + file.path);
} else {
uniqueResults.push(file);
log.debug('Resolved file ' + file.path);
log.debug('Resolved %s %s', file.isUrl ? 'url' : 'file', file.path);
}
}
});
Expand Down Expand Up @@ -184,6 +193,7 @@ var parseConfig = function(configFilePath, cliOptions) {
config.basePath = path.resolve(path.dirname(configFilePath), config.basePath);

var basePathResolve = function(relativePath) {
if (util.isUrlAbsolute(relativePath)) return relativePath;
return path.resolve(config.basePath, relativePath);
};

Expand Down Expand Up @@ -215,6 +225,8 @@ var FileGuardian = function(filePatterns, excludePatterns, emitter, autoWatch, a

log.debug('Checking files for modifications...');
files_.forEach(function(file) {
if (file.isUrl) return;

waiting++;
fs.stat(file.path, function(err, stat) {
if (file.mtime < stat.mtime) {
Expand Down
5 changes: 5 additions & 0 deletions lib/util.js
Expand Up @@ -47,6 +47,11 @@ exports.isString = function(value) {
};


exports.isUrlAbsolute = function(url) {
return /^https?:\/\//.test(url);
};


exports.camelToSnake = function(camelCase) {
return camelCase.replace(/[A-Z]/g, function(match, pos) {
return (pos > 0 ? '_' : '') + match.toLowerCase();
Expand Down
4 changes: 2 additions & 2 deletions lib/web-server.js
Expand Up @@ -65,8 +65,8 @@ var createHandler = function(fileGuardian, STATIC_FOLDER) {
setNoCacheHeaders(response);

var scriptTags = fileGuardian.getFiles().map(function(file) {
// add timestamps only for /context.html, no timestamps for /debug.html
return util.format(SCRIPT_TAG, request.url === '/context.html' ?
// Add timestamps only for /context.html, no timestamps for /debug.html or urls
return util.format(SCRIPT_TAG, request.url === '/context.html' && !file.isUrl ?
file.path + '?' + file.mtime.getTime() : file.path);
});

Expand Down
17 changes: 17 additions & 0 deletions test/unit/config.spec.coffee
Expand Up @@ -57,6 +57,7 @@ describe 'config', ->
conf:
'invalid.js': fsMock.file 0, '={function'
'exclude.js': fsMock.file 0, 'exclude = ["one.js", "sub/two.js"];'
'absolute.js': fsMock.file 0, 'files = ["http://some.com", "https://more.org/file.js"];'

# load file under test
m = loadFile __dirname + '/../../lib/config.js', mocks, {process: mocks.process}
Expand Down Expand Up @@ -191,6 +192,17 @@ describe 'config', ->
waitForFinished()


it 'should keep absolute url patterns', ->
m.resolve ['http://one.com', 'https://more.org/some.js', '/home/*.js'], [], (err, files) ->
expect(files[0].path).toBe 'http://one.com'
expect(files[0].isUrl).toBe true
expect(files[1].path).toBe 'https://more.org/some.js'
expect(files[1].isUrl).toBe true
finished++

waitForFinished()


#============================================================================
# config.parseConfig()
# Should parse configuration file and do some basic processing as well
Expand Down Expand Up @@ -218,6 +230,11 @@ describe 'config', ->
expect(config.files).toEqual ['/home/one.js', '/home/sub/two.js']


it 'should keep absolute url file patterns', ->
config = e.parseConfig '/conf/absolute.js'
expect(config.files).toEqual ['http://some.com', 'https://more.org/file.js']


it 'should resolve all exclude patterns', ->
config = e.parseConfig '/conf/exclude.js'
expect(config.exclude).toEqual ['/conf/one.js', '/conf/sub/two.js']
Expand Down
12 changes: 12 additions & 0 deletions test/unit/util.spec.coffee
Expand Up @@ -143,3 +143,15 @@ describe 'util', ->

expect(destination.a).toBe 1
expect(result).toEqual {a: 4, b: 2, c: 5}


#==============================================================================
# util.isUrlAbsolute()
#==============================================================================
describe 'isUrlAbsolute', ->

it 'should check http/https protocol', ->
expect(util.isUrlAbsolute 'some/path/http.html').toBe false
expect(util.isUrlAbsolute '/some/more.py').toBe false
expect(util.isUrlAbsolute 'http://some.com/path').toBe true
expect(util.isUrlAbsolute 'https://more.org/some.js').toBe true

0 comments on commit c55dbc8

Please sign in to comment.