all files / lib/ index.js

99.29% Statements 139/140
92.86% Branches 65/70
100% Functions 42/42
98.92% Lines 92/93
2 statements, 4 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275                    13×           13×     13×         14× 1×     13×             1×                 13× 1×   13× 17× 1×   16× 16× 12×     12× 12× 2×   12×       4×   1×                 1× 1× 1× 1×   1×               12× 16×         1×               16×           1×               16× 1×               22× 2×   20× 1×                   11× 14× 4× 6× 6× 4×   6× 6×       1×               11× 14×   1×               11× 14× 1× 3×     14×   1×               11× 14×   1×     14×   1×               11× 14× 10×   4× 6× 6× 6×   6× 8× 8×   8× 8×         1×               11× 14× 11×   3×   1×                   1× 1× 1× 1× 1×         13× 1× 13× 13× 13× 13× 13×     13×                      
 
import _ from 'lodash';
import W from 'when';
import S from 'string';
import path from 'path';
import contentful from 'contentful';
import pluralize from 'pluralize';
import RootsUtil from 'roots-util';
import querystring from 'querystring';
 
const errors = {
  no_token: 'Missing required options for roots-contentful. Please ensure `access_token` and `space_id` are present.',
  no_type_id: 'One or more of your content types is missing an `id` value',
  sys_conflict: 'One of your content types has `sys` as a field. This is reserved for storing Contentful system metadata, please rename this field to a different value.'
};
 
const hosts = {
  develop: 'preview.contentful.com',
  production: 'cdn.contentful.com'
};
 
/* TODO: change to `export default` when tests are rewritten in ES6/7 */
module.exports = function(opts) {
 
  if (!(opts.access_token && opts.space_id)) {
    throw new Error(errors.no_token);
  }
 
  let client = contentful.createClient({
    host:
      hosts[process.env.CONTENTFUL_ENV] ||
      (opts.preview ? hosts.develop : null) ||
      hosts.production,
    accessToken: opts.access_token,
    space: opts.space_id
  });
 
  /*
   * Configures content types set in app.coffee. Sets default values if
   * optional config options are missing.
   * @param {Array} types - content_types set in app.coffee extension config
   * @return {Promise} - returns an array of configured content types
   */
  function configure_content(types) {
    if (_.isPlainObject(types)) {
      types = reconfigure_alt_type_config(types);
    }
    return W.map(types, t => {
      if (!t.id) {
        return W.reject(errors.no_type_id);
      }
      t.filters = t.filters || {};
      if (!t.name || (t.template && !t.path)) {
        return W(
          client.contentType(t.id)
            .then(res => {
              t.name = t.name || pluralize(S(res.name).toLowerCase().underscore().s);
              if (t.template) {
                t.path = t.path || (e => `${t.name}/${S(e[res.displayField]).slugify().s}`);
              }
              return t
            })
        )
      }
      return W.resolve(t);
    });
  }
 
  /*
   * Reconfigures content types set in app.coffee using an object instead of
   * an array. The keys of the object set as the `name` option in the config
   * @param {Object} types - content_types set in app.coffee extension config
   * @return {Promise} - returns an array of content types
   */
  function reconfigure_alt_type_config(types) {
    return _.reduce(types, (res, type, k) => {
      type.name = k;
      res.push(type);
      return res;
    }, []);
  }
 
  /*
   * Fetches data from Contentful for content types, and formats the raw data
   * @param {Array} types - configured content_type objects
   * @return {Promise} - returns formatted locals object with all content
   */
  function get_all_content(types) {
    return W.map(types, t => {
      return fetch_content(t)
        .then(format_content)
        .then(c => t.content = c)
        .yield(t)
    });
  }
 
  /*
   * Fetch entries for a single content type object
   * @param {Object} type - content type object
   * @return {Promise} - returns response from Contentful API
   */
  function fetch_content(type) {
    return W(client.entries(
      _.merge(type.filters, {
        content_type: type.id,
        include: 10
      })
    ));
  }
 
  /*
   * Formats raw response from Contentful
   * @param {Object} content - entries API response for a content type
   * @return {Promise} - returns formatted content type entries object
   */
  function format_content(content) {
    return W.map(content, format_entry);
  }
 
  /*
   * Formats a single entry object from Contentful API response
   * @param {Object} e - single entry object from API response
   * @return {Promise} - returns formatted entry object
   */
  function format_entry(e) {
    if (_.has(e.fields, 'sys')) {
      return W.reject(errors.sys_conflict);
    }
    return _.assign(_.omit(e, 'fields'), e.fields);
  }
 
  /*
   * Sets `_url` and `_urls` properties on content with single entry views
   * `_url` takes the value `null` if the content type's custom path function
   * returns multiple paths
   * @param {Array} types - content type objects
   * return {Promise} - promise when urls are set
   */
  function set_urls(types) {
    return W.map(types, t => {
      if (t.template) {
        return W.map(t.content, entry => {
          let paths = t.path(entry);
          if (_.isString(paths)) {
            paths = [paths];
          }
          entry._urls = paths.map(p => `/${p}.html`);
          return entry._url = entry._urls.length === 1 ? entry._urls[0] : null;
        });
      }
    })
  }
 
  /*
   * Builds locals object from types objects with content
   * @param {Array} types - populated content type objects
   * @return {Promise} - promise for when complete
   */
  function set_locals(types) {
    return W.map(types, t => {
      return this.roots.config.locals.contentful[t.name] = t.content
    });
  }
 
  /*
   * Transforms every type with content with the user provided callback
   * @param {Array} types - Populated content type objects
   * @return {Promise} - promise for when compilation is finished
   */
  function transform_entries(types) {
    return W.map(types, t => {
      if (t.transform) {
        W.map(t.content, entry => {
          return W(entry, t.transform);
        });
      }
      return W.resolve(t);
    });
  }
 
  /*
   * Sort every type content with the user provided callback
   * @param {Array} types - Populated content type objects
   * @return {Promise} - promise for when compilation is finished
   */
  function sort_entries(types) {
    return W.map(types, t => {
      if (t.sort) {
        // in order to sort promises we have to resolve them first.
        W.all(t.content)
          .then(data => t.content = data.sort(t.sort));
      }
      return W.resolve(t);
    });
  }
 
  /*
   * Compiles single entry views for content types
   * @param {Array} types - Populated content type objects
   * @return {Promise} - promise for when compilation is finished
   */
  function compile_entries(types) {
    return W.map(types, t => {
      if (!t.template) {
        return W.resolve();
      }
      return W.map(t.content, entry => {
        let template = path.join(this.roots.root, t.template);
        let compiler = _.find(this.roots.config.compilers, c => {
          return _.contains(c.extensions, path.extname(template).substring(1));
        });
        return W.map(entry._urls, url => {
          this.roots.config.locals.entry = _.assign({}, entry, { _url: url });
          return compiler.renderFile(template, this.roots.config.locals)
            .then(res => {
              this.roots.config.locals.entry = null;
              return this.util.write(url, res.result);
            });
        });
      });
    });
  }
 
  /*
   * Writes all data for type with content as json
   * @param {Array} types - Populated content type objects
   * @return {Promise} - promise for when compilation is finished
   */
  function write_entries(types) {
    return W.map(types, t => {
      if (!t.write) {
        return W.resolve();
      }
      return this.util.write(t.write, JSON.stringify(t.content));
    });
  }
 
  /*
   * View helper for accessing the actual url from a Contentful asset
   * and appends any query string params
   * @param {Object} asset - Asset object returned from Contentful API
   * @param {Object} opts - Query string params to append to the URL
   * @return {String} - URL string for the asset
   */
  function asset_view_helper(asset = {}, params) {
    asset.fields = asset.fields || {};
    asset.fields.file = asset.fields.file || {};
    let url = asset.fields.file.url;
    Eif (params) {
      return `${url}?${querystring.stringify(params)}`;
    }
    return url;
  }
 
  return class RootsContentful {
    constructor(roots) {
      this.roots = roots;
      this.util = new RootsUtil(this.roots);
      this.roots.config.locals = this.roots.config.locals || {};
      this.roots.config.locals.contentful = this.roots.config.locals.contentful || {};
      this.roots.config.locals.asset = asset_view_helper;
    }
    setup() {
      return configure_content(opts.content_types).with(this)
        .then(get_all_content)
        .tap(set_urls)
        .then(transform_entries)
        .then(sort_entries)
        .tap(set_locals)
        .tap(compile_entries)
        .tap(write_entries);
    }
  }
}