All files cerba.ts

100% Statements 62/62
100% Branches 19/19
100% Functions 14/14
100% Lines 45/45

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 621x 1x 1x 1x   1x 12x 1x 37x 12x   1x 21x   1x 8x     9x 9x 9x 8x 8x 8x 8x 8x 8x 8x 5x 5x 5x     8x 8x     1x 6x 6x 6x 1x   1x 2x 1x   1x 1x   1x       1x 4x 4x 3x 3x   1x  
import * as path from 'path';
import {CerbaConfig} from './cerba_config';
import {CerbaPackage} from './cerba_package';
import {PackageJSON} from './package_json';
 
export class Cerba {
  constructor(public props: {cwd?: string; main?: string; name?: string; scope?: string} = {}) {}
  get cwd() {
    if (this.props.cwd) return path.resolve(this.props.cwd);
    return process.cwd();
  }
  get config() {
    return new CerbaConfig({cwd: this.cwd, scope: this.props.scope});
  }
  get packageJSON() {
    return new PackageJSON({cwd: this.cwd, readonly: true});
  }
  private cachePackages?: CerbaPackage[];
  get packages(): Promise<CerbaPackage[]> {
    return (async () => {
      if (this.cachePackages) return this.cachePackages;
      const op = await (async () => {
        const config = this.config;
        const main = this.props.main;
        const cwd = this.cwd;
        const pkg = this.packageJSON;
        const plugins = await this.config.babelPlugins;
        if (main) return [new CerbaPackage({config, plugins, cwd, pkg, main})];
        const packages = await this.config.packages;
        return packages.map(pack => {
          return new CerbaPackage({config, plugins, cwd, pkg, ...pack});
        });
      })();
      this.cachePackages = op;
      return op;
    })();
  }
  async findPackage(name: string) {
    const pkgs = await this.packages;
    const results = pkgs.find(pkg => pkg.name === name);
    if (results) return results;
    throw new Error(`Unable to find package "${name}" within cerba config.`);
  }
  async build() {
    if (this.props.name) {
      return this.buildPackage(this.props.name);
    }
    const packages = await this.packages;
    return Promise.all(
      packages.map(p => {
        return p.build();
      })
    );
  }
  async buildPackage(name?: string) {
    const n = this.props.name ?? name;
    if (!n) throw new Error('Unable to build package, no package name provided');
    const file = await this.findPackage(n);
    return file.build();
  }
}