All files cerba_package.ts

94.34% Statements 100/106
80% Branches 36/45
100% Functions 21/21
98.55% Lines 68/69

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 1031x 1x   1x 1x   1x   8x   8x 8x   1x 34x 34x 25x     1x 11x 11x   1x 23x 23x   1x 11x 11x   1x 27x   12x 12x 12x 12x       4x 4x 4x 4x 4x 4x         1x 4x 4x 4x 4x 4x 8x       1x 4x 4x         4x 4x 4x 4x 4x     4x 4x 4x 4x 8x 4x 4x       8x 8x 8x 4x 4x 4x     1x 4x 4x   1x 8x 8x   1x  
import * as path from 'path';
import {BabelFile} from './babel_file';
import {CerbaConfig, CerbaConfigPkg} from './cerba_config';
import {PackageJSON, PackageJSONType} from './package_json';
import {Path} from './path';
 
export class CerbaPackage extends BabelFile {
  constructor(
    public props: ConstructorParameters<typeof BabelFile>[0] & Partial<CerbaConfigPkg> & {config: CerbaConfig}
  ) {
    super({basename: props.main, ...props});
    this.props = {basename: props.main, ...props};
  }
  getName(): {name?: string; scope?: string} {
    const split = this.props?.name?.split('/');
    if (!split) return {};
    Eif (split && split.length === 1) return {name: split[0]};
    return {name: split[1], scope: split[2]};
  }
  get name() {
    Iif (this.scope) return `${this.scope}/${this.id}`;
    return `${this.id}`;
  }
  get id() {
    const {name} = this.getName();
    return name || this.coreBasename;
  }
  get scope() {
    const {scope} = this.getName();
    return scope || this.config.scope;
  }
  get config() {
    return this.props.config;
  }
  get destDir() {
    return (async () => {
      const packagesDir = await this.config.packagesDir;
      return path.join(packagesDir, this.id);
    })();
  }
 
  get version() {
    return (async () => {
      const p = await this.package;
      const version = await p.version;
      Iif (version) return version;
      return await this.config.initialVersion;
    })();
  }
 
  /** moves files to the packagesDir */
  async moveFiles() {
    const files = await this.recursiveSiblings;
    const packagesDir = await this.destDir;
    return Promise.all(
      files.map(async file => {
        const dest = path.join(packagesDir, file.relativePath);
        await file.copy({path: dest});
      })
    );
  }
  async mapJavascriptFiles(basename: string) {
    return new Path({
      cwd: await this.destDir,
      basename,
    }).replaceExt('.js').relativePath;
  }
  /** returns the package json for this file */
  get packageContent() {
    return (async () => {
      const dependencies = await this.dependencies;
      const version = await this.version;
      const results: PackageJSONType = {
        name: this.name,
      };
      const {description, main, bin} = this.props;
      Eif (version) results.version = version;
      if (description) results.description = description;
      if (Imain) results.main = await this.mapJavascriptFiles(main);
      if (Ebin) results.bin = await this.mapJavascriptFiles(bin);
      Eif (dependencies) results.dependencies = dependencies;
      return results;
    })();
  }
  cachePackage?: PackageJSON;
  get package() {
    return (async () => {
      if (this.cachePackage) return this.cachePackage;
      const cwd = await this.destDir;
      this.cachePackage = new PackageJSON({cwd});
      return this.cachePackage;
    })();
  }
  async buildPackage() {
    const pack = await this.package;
    return pack.write(await this.packageContent);
  }
  async build() {
    await this.buildPackage();
    await this.moveFiles();
  }
}