All files file.ts

85.71% Statements 18/21
73.33% Branches 11/15
100% Functions 2/2
85.71% Lines 18/21

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       2x 2x   2x 2x 1x 1x 1x 1x 1x           1x                 2x 2x   2x           2x    
import { Construct, ISynthesisSession, Tokenization, DefaultTokenResolver, StringConcat } from 'constructs';
import * as fs from 'fs';
import * as path from 'path';
import { Project } from './project';
 
export interface FileBaseOptions {
  /**
   * Indicates whether this file should be committed to git or ignored. By
   * default, all generated files are committed and anti-tamper is used to
   * protect against manual modifications.
   *
   * @default true
   */
  readonly committed?: boolean;
 
  /**
   * Update the project's .gitignore file
   * @default true
   */
  readonly editGitignore?: boolean;
}
 
export abstract class FileBase extends Construct {
  public readonly path: string;
 
  constructor(project: Project, filePath: string, options: FileBaseOptions = { }) {
    super(project, filePath);
    this.path = filePath;
 
    const gitignore = options.editGitignore ?? true;
    if (gitignore) {
      const committed = options.committed ?? true;
      const pattern = `/${this.path}`;
      Eif (committed) {
        project.gitignore.comment('synthesized by projen, (do not modify by hand)');
        project.gitignore.include(pattern);
      } else {
        project.gitignore.comment('synthesized by projen');
        project.gitignore.exclude(pattern);
      }
    } else {
      Iif (options.committed != null) {
        throw new Error('"gitignore" is disabled, so it does not make sense to specify "committed"');
      }
    }
  }
 
  protected abstract get data(): string;
 
  public onSynthesize(session: ISynthesisSession): void {
    const filePath = path.join(session.outdir, this.path);
    fs.mkdirSync(path.dirname(filePath), { recursive: true });
 
    const post = Tokenization.resolve(this.data, {
      resolver: new DefaultTokenResolver(new StringConcat()),
      scope: this,
      preparing: false,
    });
 
    fs.writeFileSync(filePath, post);
  }
}