var exec = require('child_process').exec,
    lumbar = require('lumbar');

module.exports = {
  generateToken: function(command, callback) {
    if (!command) {
      return callback(undefined, '');
    }

    exec(command, {cwd: lumbar.fileUtil.lookupPath()}, function(err, stdout, stderr) {
      callback(err, (stdout || '').replace(/\n/g, ''));
    });
  },

  fileName: function(context, next, complete) {
    // Prepend the cache buster value
    next(function(err, ret) {
      if (err) {
        return complete(err);
      }

      module.exports.generateToken(
        context.config.attributes['long-expires'],
        function(err, token) {
          if (ret && token) {
            // Move the expires token after the platform path if that is the start of the path
            if (ret.path.indexOf(context.platformPath) === 0) {
              ret.path = ret.path.substring(context.platformPath.length);
              token = context.platformPath + token;
            }
            ret.path = token + '/' + ret.path;
          }
          complete(err, ret);
        });
    });
  }
};

var exec = require('child_process').exec,
    lumbar = require('lumbar');

module.exports = {
  generateToken: function(command, callback) {
    if (!command) {
      return callback(undefined, '');
    }

    exec(command, {cwd: lumbar.fileUtil.lookupPath()}, function(err, stdout, stderr) {
      callback(err, (stdout || '').replace(/\n/g, ''));
    });
  },

  fileName: function(context, next, complete) {
    // Prepend the cache buster value
    next(function(err, ret) {
      if (err) {
        return complete(err);
      }

      function generatePath(err, token) {
        if (ret && token) {
          // Move the expires token after the platform path if that is the start of the path
          if (ret.path.indexOf(context.platformPath) === 0) {
            ret.path = ret.path.substring(context.platformPath.length);
            token = context.platformPath + token;
          }
          ret.path = token + '/' + ret.path;
        }
        complete(err, ret);
      }

      if (context.configCache.longExpires) {
        generatePath(undefined, context.configCache.longExpires);
      } else {
        module.exports.generateToken(
          context.config.attributes['long-expires'],
          generatePath);
      }
    });
  }
};

Store expires token on the config cache
Store expires token on the config cache

This will help prevent errors where the token changes and not all resources are updated to reflect the change.