From 8de6600c53cd65ea2a9238738469dc8a533b49c8 Mon Sep 17 00:00:00 2001 From: dsc Date: Fri, 13 Apr 2012 07:12:43 -0700 Subject: [PATCH] Search for datasource by id. --- lib/server/controllers/datasource.co | 22 ++++++++++++++-------- 1 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/server/controllers/datasource.co b/lib/server/controllers/datasource.co index 1f89863..fb07e6a 100644 --- a/lib/server/controllers/datasource.co +++ b/lib/server/controllers/datasource.co @@ -33,24 +33,30 @@ class DataSourceController extends Controller * Auto-load :id for related requests. */ autoload: (id, cb) -> - file = @toFile id - parser = JSON.parse + files = findit.sync @dataDir + pattern = new RegExp "#id\.(json|ya?ml)$", "i" + file = _.find files, -> pattern.test it - yamlFile = file.replace /\.json$/i, '.yaml' - if exists yamlFile - file = yamlFile + unless file + console.error "Unable to find DataSource for '#id'!" + return cb new Error "Unable to find DataSource for '#id'!" + + if _.endsWith file, "#id.json" + parser = JSON.parse + if _.endsWith file, "#id.yaml" parser = yaml.load err, data <- fs.readFile file, 'utf8' if 'ENOENT' is err?.code - return cb null, {} + console.error "Unable to find DataSource for '#id'!" + return cb new Error "Unable to find DataSource for '#id'!" if err - console.error "DataSourceController.autoload(#id, #{typeof cb}) -->\nerr" + console.error "DataSourceController.autoload(#id, #{typeof cb}) -->\n", err return cb err try cb null, parser data catch err - console.error "DataSourceController.autoload(#id, #{typeof cb}) -->\nerr" + console.error "DataSourceController.autoload(#id, #{typeof cb}) -->\n", err cb err /** -- 1.7.0.4