All files / packages/design-system/scripts/ui index.js

88.05% Statements 59/67
100% Branches 8/8
82.35% Functions 28/34
87.93% Lines 51/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x   3x 3x   3x 3x   3x   16x   244x   3x 244x 244x     244x 3x   8x 3x   24x 3x   212x 3x         4x 4x           8x 24x 3x 212x           212x 212x       212x 212x 212x 3x 3x                 16x 16x 212x 212x 3x                 3x   3x 3x 3x                                          
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
const Task = require('data.task');
const gulp = require('gulp');
const gulpFile = require('gulp-file');
const path = require('path');
const paths = require('../helpers/paths');
const fs = require('fs');
const I = require('immutable-ext');
const Either = require('data.either');
const { Right, Left } = Either;
 
const createParser = require('@salesforce-ux/design-system-parser');
const { mapTree, toList } = require('../../shared/utils/tree');
 
const getComments = require('./comments');
const getShowcase = require('./showcase');
 
const isVariant = x => x.getIn(['annotations', 'variant']);
const isUtil = x => x.getIn(['annotations', 'utility']);
 
const variants = c => toList(c).filter(isVariant);
 
const toMap = f => (ac, c) => ac.set(c.get('id'), f(c.get('id')).get());
 
const exists = p => (fs.existsSync(p) ? Right(p) : Left(null));
 
const pathIfExists = filepath =>
  exists(path.resolve(paths.root, filepath))
    .map(() => filepath)
    .getOrElse(null);
 
const utilityExamplePath = utilId =>
  pathIfExists(`./ui/utilities/${utilId}/example.jsx`);
 
const componentExamplePath = (componentId, variantId) =>
  pathIfExists(`./ui/components/${componentId}/${variantId}/example.jsx`);
 
const docPath = (type, componentId) =>
  pathIfExists(`./ui/${type}/${componentId}/docs.mdx`);
 
const createUI = parser =>
  I.Map({
    components: parser.components().reduce(toMap(parser.component), I.Map()),
    utilities: parser.utilities().reduce(toMap(parser.utility), I.Map())
  });
 
const showcasePath = (componentId, item) =>
  isVariant(item) || isUtil(item)
    ? isUtil(item) // skips util's children since they aren't utils...
      ? utilityExamplePath(componentId)
      : componentExamplePath(componentId, item.get('id'))
    : null;
 
const addShowcaseAndDocPaths = (type, componentId, item) => {
  const docs = docPath(type, item.get('id'));
  const showcase = showcasePath(componentId, item);
  return item
    .set('docPath', docs)
    .set('showcasePath', showcase)
    .set('showcase', getShowcase(showcase));
};
 
const uiFromComments = () =>
  getComments()
    .map(createParser)
    .map(createUI);
 
const ui = () =>
  uiFromComments().map(u =>
    u.map((group, type) =>
      group.map((item, componentId) =>
        mapTree(item, itm => addShowcaseAndDocPaths(type, componentId, itm))
      )
    )
  );
 
/**
 * Get json from parsed comments
 * uiJson : Task -> string
 */
const uiJson = () =>
  ui()
    .map(x => JSON.stringify(x, null, 2))
    .chain(json => new Task((rej, res) => json))
    .fork(y => y);
 
/**
 * Basic UIJson Data - sans showcase and doc paths
 */
const uiJsonBasic = () =>
  uiFromComments()
    .map(x => JSON.stringify(x, null, 2))
    .chain(json => new Task((rej, res) => json))
    .fork(y => y);
 
const writeToDist = () =>
  ui()
    .map(x => JSON.stringify(x, null, 2))
    .chain(
      json =>
        new Task((reject, resolve) =>
          gulpFile('ui.json', json, { src: true })
            .pipe(gulp.dest(paths.dist))
            .on('finish', resolve)
            .on('error', reject)
        )
    );
 
module.exports = {
  ui,
  uiJson,
  uiJsonBasic,
  isVariant,
  variants,
  writeToDist,
  componentExamplePath,
  utilityExamplePath
};