Package.describe({
  summary: 'Zone.Js integration for meteor'
});

Package.on_use(function (api) {
  addPackageFiles(api);
});

Package.on_test(function (api) {
  addPackageFiles(api);

  api.use([
    'tinytest',
    'test-helpers',
  ], 'client');

  api.add_files([
    'tests/_both.js'
  ], ['client', 'server']);

  api.add_files([
    'tests/_server.js'
  ], 'server');

  api.add_files([
    'tests/loader.js',
    'tests/reporters.js',
    'tests/hijacks/methods.js',
    'tests/hijacks/subscriptions.js',
    'tests/hijacks/collections.js',
  ], 'client');
});

function addPackageFiles(api) {
  api.add_files([
    'assets/utils.js',
    'assets/before.js',
    'assets/zone.js',
    'assets/after.js',
    'assets/reporters.js',
    'assets/tracer.js',
  ], 'client', {isAsset: true});

  api.add_files(['server/inject.js'], 'server');

  api.add_files([
    'client/hijack.js'
  ], 'client');

  api.use('ui', 'client');
  api.use('templating', 'client');
  api.use('deps', 'client');
  api.use('session', 'client');
  api.use('livedata', 'client');
  api.use('minimongo', 'client');
  api.use('inject-initial');
  api.use('iron-router', 'client', {weak: true});
}

@@ -1,16 +1,77 @@
+var fs = Npm.require('fs');%0Avar path = Npm.require('path');%0A%0A
 Package.describe
@@ -177,16 +177,354 @@
 es(api);
+%0A%0A  // Add iron router only if it exists%0A  if(ironRouterExists()) %7B%0A    api.use(%5B'iron-router'%5D, %5B'client', 'server'%5D);%0A  %7D%0A%0A  // A hack to detect if IR has been added or removed from the app%0A  // if IR was not there on the app and added later.%0A  if(isAppDir('./')) %7B%0A    api.add_files('../../.meteor/packages', %5B'client', 'server'%5D);%0A  %7D
 %0A%7D);%0A%0APa
@@ -1522,56 +1522,883 @@
 ');%0A
-  api.use('iron-router', 'client', %7Bweak: true%7D);
+%7D%0A%0A//--------------------------------------------------------------------------%5C%5C%0A%0Afunction ironRouterExists() %7B%0A  try %7B%0A    var meteorPackages = fs.readFileSync(path.join(meteorRoot(), '.meteor', 'packages'), 'utf8');%0A    return !!meteorPackages.match(/iron-router/);%0A  %7D catch(ex) %7B%0A    // seems like FastRender running outside a Meteor app (ie: with tinytest)%0A    // So there is no iron-router%0A    return false;%0A  %7D%0A%7D%0A%0Afunction meteorRoot() %7B%0A  var currentDir = process.cwd();%0A  while (currentDir) %7B%0A    var newDir = path.dirname(currentDir);%0A    if (isAppDir(currentDir)) %7B%0A      break;%0A    %7D else if (newDir === currentDir) %7B%0A      return null;%0A    %7D else %7B%0A      currentDir = newDir;%0A    %7D%0A  %7D%0A  return currentDir;%0A%7D%0A%0Afunction isAppDir(filepath) %7B%0A  try %7B%0A    return fs.statSync(path.join(filepath, '.meteor', 'packages')).isFile();%0A  %7D catch (e) %7B%0A    return false;%0A  %7D
 %0A%7D%0A

Add iron router only if it exists