# all files / src/models/ file-info.js

92.06% Statements 58/63
94.44% Branches 17/18
100% Functions 10/10
84.85% Lines 28/33
2 statements, 5 branches Ignored

    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
    631×
    1×
    1×
    1×

    5×
    5×

    2×
    2×
    1×

    1×
    1×

    1×
    1×

    2×

    2×
    2×
    2×

    2×
    2×

    2×

    2×
    2×
    2×

    1×
    1×

    1×
    1×

     import ejs from 'ejs';
    import fs from 'fs';
    import { outputFileSync } from 'fs-extra';
    import { fileExists } from '../util/fs';

    class FileInfo {
      constructor(args) {
        this.ui = args.ui;
        this.templateVariables = args.templateVariables; // locals passed to ejs template
        this.originalPath = args.originalPath;           // path to template
        this.mappedPath = args.mappedPath;               // destination path to be written to
      }

      writeFile() {
        this.ui.writeDebug(`Attempting to write file: ${this.mappedPath}`);
        if (fileExists(this.mappedPath)) {
          this.ui.writeError(
            `Not writing file.  File already exists at: ${this.mappedPath}`
          );
        } else {
          const fileContent = this.renderTemplate();
          this.ui.writeDebug(`fileContent: ${fileContent}`);

          outputFileSync(this.mappedPath, fileContent);
          this.ui.writeCreate(this.mappedPath);
        }
        return;
      }

      renderTemplate() {
        let rendered;
        this.ui.writeDebug(`rendering template: ${this.originalPath}`);
        const template = fs.readFileSync(this.originalPath, 'utf8');

        try {
          rendered = ejs.render(template, this.templateVariables);
        } catch (err) {
          this.ui.writeDebug('couldnt render');
          err.message += ' (Error in blueprint template: ' + this.originalPath + ')';
          this.ui.writeError(`error was: ${err.message}`);
          throw err;
        }
        return rendered;
      }

      isFile() {
        let fileCheck;
        try {
          fileCheck = fs.lstatSync(this.originalPath).isFile();
        } catch (e) {
          Eif (e.code === 'ENOENT') {
            return false;
          } else {
            throw e;
          }
        }
        this.ui.writeDebug(`checking file: ${this.originalPath} - ${fileCheck}`);
        return fileCheck;
      }
    }

    export default FileInfo;