all files / src/commands/ chunk.js

0% Statements 0/58
0% Branches 0/10
0% Functions 0/14
0% Lines 0/50
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                                                                                                                                                                                                                                 
import chalk from 'chalk';
import yargs from 'yargs';
import common from './common-yargs';
import { chunkFile } from '../index';
import path from 'path';
import fs from 'fs';
import fsp from 'fs-promise';
import { ensureDir, expand } from '../fs-utils';
 
async function executeChunk(input, outputDir, chunkOptions) {
  let basename = path.basename(input, '.css');
 
  const {data, maps, totalSelectorCount} = await chunkFile(input, chunkOptions);
 
  let chunkData = data.map((ast, index) => {
    let outputFilename = path.join(outputDir, `${basename}.${index}.css`);
 
    return fsp.writeFile(outputFilename, ast);
  });
 
  let sourcemaps = maps.map((sourcemap, index) => {
    let outputFilename = path.join(outputDir, `${basename}.${index}.css.map`);
 
    return fsp.writeFile(outputFilename, JSON.stringify(sourcemap));
  });
 
  const chunks = await Promise.all(chunkData);
  await Promise.all(sourcemaps);
 
  return chunks;
}
 
function execute(options) {
  return expand(options.input)
    .filter(x => /\.css$/.test(x))
    .map(filepath => {
      let outputDir = options.outDir;
 
      return ensureDir(outputDir)
        .then(() => executeChunk(filepath, outputDir, { sourcemaps: options.sourcemaps }));
    })
    .flatMap(x => x)
    .reduce((acc, x) => acc.concat([x]), [])
    .toPromise(Promise)
    .then(() => {
      console.log(chalk.green('Complete'));
      return 0;
    });
}
 
function yargsSetup() {
  yargs.command('chunk', 'breaks up css file to multiple files if it exceeds IE selector limits');
}
 
function examples() {
  yargs.example('$0 chunk <input file|directory>   (chunked files will reside next to input css files with the format *.##.css)');
  yargs.example('$0 chunk <input file|directory> --out-dir <output directory>');
  yargs.example('$0 chunk <input file|directory> --sourcemaps');
}
 
function parseArgs(argv){
  yargs.reset();
 
  common();
  examples();
 
  let options = yargs
    .help('h')
    .alias('h', 'help')
    .options({
      'out-dir': {
        description: 'output directory',
        type: 'string'
      },
      'sourcemaps': {
        description: 'ouput sourcemaps',
        type: 'boolean'
      }
    })
    .parse(argv);
 
  if (options.help) {
    return options;
  }
 
  options.input = options._.length > 0 ? options._[0] : null;
 
  if (!options.input) {
    throw 'No input provided';
  }
 
  options.input = path.resolve(options.input);
 
  if (!options.outDir) {
    if (fs.statSync(options.input).isDirectory()) {
      options.outDir = options.input;
    } else {
      options.outDir = path.dirname(options.input);
    }
  } else {
    options.outDir = path.resolve(options.outDir);
  }
 
  return options;
}
 
export default {
  execute,
  examples,
  yargsSetup,
  parseArgs
};