All files / src/types/kpj kpj-file-writer.ts

91.81% Statements 101/110
66.66% Branches 22/33
100% Functions 6/6
91.81% Lines 101/110

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 103 104 105 106 107 108 109 110 1111x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 1x 1x 3x 3x 3x 3x 1x 1x 1x     1x 1x 1x 1x 1x 10x 32x 11x 32x   21x 8x 8x 32x 10x 1x 1x 21x 21x 21x       21x 21x 13x 13x 13x 8x 8x 8x 8x 8x 21x 1x 1x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x       1x  
/*
 * Keyman is copyright (C) SIL Global. MIT License.
 *
 * Created by mcdurdin on 2024-10-11.
 *
 * Write a Keyman Developer Project file to a string in .kpj XML format
 */
import { KPJFile, KPJFileFile } from './kpj-file.js';
import { KeymanXMLWriter } from '../../index.js';
import { KeymanDeveloperProject, KeymanDeveloperProjectOptions, KeymanDeveloperProjectType, KeymanDeveloperProjectVersion } from './keyman-developer-project.js';
 
export class KPJFileWriter {
  public write(project: KeymanDeveloperProject): string {
    const defaultOptions = new KeymanDeveloperProjectOptions(project.options.version);
 
    const kpj: KPJFile = {
      KeymanDeveloperProject: {
        Options: {
          BuildPath: project.options.buildPath === defaultOptions.buildPath ? null : this.normalizePath(project.options.buildPath, project.options.version),
          CheckFilenameConventions: project.options.checkFilenameConventions === defaultOptions.checkFilenameConventions ? null : this.boolToString(project.options.checkFilenameConventions),
          CompilerWarningsAsErrors: project.options.compilerWarningsAsErrors === defaultOptions.compilerWarningsAsErrors ? null : this.boolToString(project.options.compilerWarningsAsErrors),
          ProjectType: project.options.projectType == KeymanDeveloperProjectType.LexicalModel ? 'lexicalmodel' : 'keyboard',
          SkipMetadataFiles: project.options.skipMetadataFiles === defaultOptions.skipMetadataFiles ? null : this.boolToString(project.options.skipMetadataFiles),
          SourcePath: project.options.sourcePath === defaultOptions.sourcePath ? null : this.normalizePath(project.options.sourcePath, project.options.version),
          Version: project.options.version == '1.0' ? null : project.options.version,
          WarnDeprecatedCode: project.options.warnDeprecatedCode === defaultOptions.warnDeprecatedCode ? null : this.boolToString(project.options.warnDeprecatedCode),
        },
      }
    };
 
    if(project.options.version == '1.0') {
      kpj.KeymanDeveloperProject.Files = { File: [] };
      for(const file of project.files) {
        // we skip any parented files, and any file details
        const File: KPJFileFile = {
          ID: file.filename,
          Filename: file.filename,
          Filepath: this.normalizePath(file.filePath, project.options.version),
          FileType: file.fileType,
        };
        kpj.KeymanDeveloperProject.Files.File.push(File);
      }
    }
 
    this.stripEmptyMembers(kpj);
 
    const result = new KeymanXMLWriter('kpj').write(kpj);
    return result;
  }
 
  private normalizePath(path: string, version: KeymanDeveloperProjectVersion) {
    return version == '1.0'
      ? path?.replaceAll(/\//g, '\\')
      : path?.replaceAll(/\\/g, '/');
  }
 
  private boolToString(b?: boolean) {
    if(b === null || b === undefined) {
      return null;
    }
    // .kpj expects title case True/False
    return b ? 'True' : 'False';
  }
 
  private stripEmptyMembers(o: any) {
    for(const p of Object.keys(o)) {
      if(typeof o[p] === 'undefined' || o[p] === null) {
        delete o[p];
      } else if(this.isEmptyObject(o[p])) {
        delete o[p];
      } else if(typeof o[p] == 'object') {
        this.stripEmptyMembers(o[p]);
      }
    }
  }
 
  private isEmptyObject(value: any) {
    // https://stackoverflow.com/a/32108184/1836776
 
    if (value == null) {
      // null or undefined
      return false;
    }
 
    if (typeof value !== 'object') {
      // boolean, number, string, function, etc.
      return false;
    }
 
    const proto = Object.getPrototypeOf(value);
 
    // consider `Object.create(null)`, commonly used as a safe map
    // before `Map` support, an empty object as well as `{}`
    if (proto !== null && proto !== Object.prototype) {
      return false;
    }
 
    return this.isEmpty(value);
  }
 
  private isEmpty(obj: any) {
    for (const prop in obj) {
      if (Object.hasOwn(obj, prop)) {
        return false;
      }
    }

    return true;
  }
}