All files / packages/design-system/scripts/helpers publish.js

0% Statements 0/61
0% Branches 0/26
0% Functions 0/20
0% Lines 0/52

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 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                                                                                                                                                                                                                                                                                                                                                                         
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
const _ = require('lodash');
const async = require('async');
const cssstats = require('cssstats');
const { exec } = require('child_process');
const fs = require('fs');
const gulp = require('gulp');
const gulpzip = require('gulp-zip');
const path = require('path');
const buildServerApi = require('@salesforce-ux/build-server-api');
 
const packageJSON = require('../../package.json');
const paths = require('./paths');
 
const { publish } = buildServerApi(process.env.BUILD_SERVER_HOST_NEW);
 
const CSS_PATH = 'assets/styles/salesforce-lightning-design-system.css';
 
const buildPaths = {
  build: path.resolve.bind(path, paths.build),
  buildDist: path.resolve.bind(path, paths.build, 'dist')
};
 
const execute = (cmd, done) =>
  exec(cmd, { cwd: paths.root, stdio: 'inherit' }, (err, out, stderr) => {
    if (err) return done(err);
    done(null, out.trim());
  });
 
const formatStats = stats => ({
  kbSize: stats.size,
  gzipSize: stats.gzipSize,
  ruleCount: stats.rules.total,
  selectorCount: stats.selectors.total,
  declarationCount: stats.declarations.total
});
 
const hasError = x =>
  Object.keys(x).some(k => Object.keys(x[k]).includes('error'));
 
const formatVnuCount = report => ({
  htmlErrors: Object.keys(report).reduce(
    (acc, k) => acc + report[k].filter(hasError).length,
    0
  )
});
 
const formatA11yCount = issues => ({
  allyErrors: _.flatMap(issues, i =>
    _.flatMap(i.violations, v => v.nodes.length)
  ).reduce((acc, x) => acc + x, 0)
});
 
const zip = (src, done) =>
  gulp
    .src(buildPaths.build(`${src}/**/*`))
    .pipe(gulpzip(`${src}.zip`))
    .on('error', done)
    .pipe(gulp.dest(buildPaths.build()))
    .on('error', done)
    .on('finish', done);
 
const prepare = done => {
  console.log('[BUILD:PREPARE]');
  return async.series(
    [
      // clean
      done =>
        async.series(
          [
            async.apply(execute, `rm -rf ${paths.build}`),
            async.apply(execute, `mkdir ${paths.build}`)
          ],
          done
        ),
      // dist
      done =>
        async.series(
          [
            async.apply(execute, `cp -a ${paths.dist}/. ${paths.build}/dist`),
            async.apply(execute, `rm -rf ${paths.build}/dist/*.zip`)
          ],
          done
        ),
      // examples
      async.apply(
        execute,
        `cp -a ${paths.generated}/examples/. ${paths.build}/examples`
      ),
      // snap which is created during the test/lint phase
      async.apply(
        execute,
        `mv ${paths.generated}/snapshot.json ${paths.build}`
      ),
      // tokens
      async.apply(
        execute,
        `cp -a ${paths.designTokens}/. ${paths.build}/design-tokens`
      ),
      // git info
      async.apply(execute, 'git show --format="%an|%ae|%ad|%s" | head -n 1'),
      // stats
      done =>
        async.series(
          [
            done => {
              let css = fs.readFileSync(buildPaths.buildDist(CSS_PATH), 'utf8');
              let stats = cssstats(css);
              done(null, formatStats(stats));
            },
            done => {
              let report =
                fs.readFileSync(`${paths.reports}/vnu_report.json`, 'utf-8') ||
                '';
              done(null, formatVnuCount(JSON.parse(report)));
            },
            done => {
              let report =
                fs.readFileSync(`${paths.reports}/a11y.json`, 'utf-8') || '';
              done(null, formatA11yCount(JSON.parse(report)));
            },
            done => {
              let report =
                fs.readFileSync(`${paths.reports}/validations.json`, 'utf-8') ||
                '';
              done(null, { validationFailures: JSON.parse(report).total });
            }
          ],
          (err, [counts, html, a11y, validations]) => {
            if (err) return done(err);
            done(null, _.assign({}, counts, html, a11y, validations));
          }
        ),
      // zip
      async.apply(zip, 'dist'),
      async.apply(zip, 'examples'),
      async.apply(zip, 'design-tokens')
    ],
    (err, [_prepare, _dist, _examples, _snaps, _tokens, info, stats, _zip]) => {
      if (err) return done(err);
      let result = _.assign(
        {},
        { info, stats },
        {
          sha: process.env.TRAVIS_COMMIT,
          tag: process.env.TRAVIS_TAG || '',
          pullRequest: process.env.TRAVIS_PULL_REQUEST || '',
          branch: process.env.TRAVIS_BRANCH || '',
          commitRange: process.env.TRAVIS_COMMIT_RANGE || '',
          headCommit: process.env.TRAVIS_PULL_REQUEST_SHA,
          commit: process.env.TRAVIS_COMMIT || '',
          eventType: process.env.TRAVIS_EVENT_TYPE || '',
          version: packageJSON.version,
          isNew: true
        }
      );
      done(null, result);
    }
  );
};
 
module.exports = done =>
  prepare((err, result) => {
    if (err) return done(err);
    publish(
      {
        result,
        zips: [
          'dist.zip',
          'examples.zip',
          'snapshot.json',
          'design-tokens.zip'
        ].map(p => buildPaths.build(p)),
        project: 'design-system'
      },
      done
    );
  });