All files ts.js

86.2% Statements 25/29
70.58% Branches 24/34
100% Functions 1/1
86.2% Lines 25/29

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 1093x 3x 3x 3x                                               3x                   6x 6x   6x 1x     6x 1x 1x   1x           5x 1x   1x               5x 3x   3x               5x                       5x         5x 4x               5x     3x      
const fs = require('fs');
const path = require('path');
const { createFile, OPTIONS } = require('./global');
const { consoleMessage } = require('./logger');
 
/**
 * @module Typescript
 */
 
/**
 * Create, or merge, a tsconfig file.
 *
 * @param {object} dotenv
 * @param {string} dotenv._BUILD_DIST_DIR
 * @param {object} options
 * @param {string} options.baseTsConfig
 * @param {string} options.contextPath
 * @param {boolean} options.isCreateTsConfig
 * @param {boolean} options.isMergeTsConfig
 * @param {boolean} options.isRegenTsConfig
 * @param {object} settings
 * @param {string} settings.configFilename
 * @param {object} settings.consoleMessage
 * @param {Function} settings.createFile
 * @param {boolean} settings.isMessaging Helps with standalone configuration
 * @returns {undefined|{compilerOptions: {noEmit: boolean, allowJs: boolean, outDir: string}}}
 */
const createTsConfig = (
  { _BUILD_DIST_DIR: DIST_DIR } = OPTIONS.dotenv || {},
  { baseTsConfig, contextPath, isCreateTsConfig, isMergeTsConfig, isRegenTsConfig } = OPTIONS,
  {
    configFilename = 'tsconfig.json',
    consoleMessage: aliasConsoleMessage = consoleMessage,
    createFile: aliasCreateFile = createFile,
    isMessaging = true
  } = {}
) => {
  const currentConfigPath = path.join(contextPath, configFilename);
  const isCurrentConfig = fs.existsSync(currentConfigPath);
 
  if (isCurrentConfig && isMessaging) {
    aliasConsoleMessage.info(`Current ${configFilename} found: ${currentConfigPath}`);
  }
 
  if (!isCreateTsConfig) {
    Eif (isMessaging) {
      aliasConsoleMessage.info(`Ignoring ${configFilename}`);
    }
    return undefined;
  }
 
  let currentConfig;
  let presetConfig;
 
  if (isMergeTsConfig) {
    try {
      // eslint-disable-next-line global-require
      currentConfig = require(currentConfigPath);
    } catch (e) {
      if (isMessaging) {
        aliasConsoleMessage.warn(`No ${configFilename} found.`);
      }
    }
  }
 
  if (baseTsConfig) {
    try {
      // eslint-disable-next-line global-require
      presetConfig = require(`@tsconfig/${baseTsConfig}/${configFilename}`);
    } catch (e) {
      if (isMessaging) {
        aliasConsoleMessage.warn(`No preset ${configFilename} specified, using basic properties only.`);
      }
    }
  }
 
  const customTsConfig = {
    ...currentConfig,
    compilerOptions: {
      allowJs: true,
      noEmit: false,
      // strict: false,
      ...currentConfig?.compilerOptions,
      ...presetConfig?.compilerOptions,
      outDir: path.basename(DIST_DIR)
    }
  };
 
  aliasCreateFile(`${JSON.stringify(customTsConfig, null, 2)}\n`, {
    dir: contextPath,
    filename: configFilename
  });
 
  if (isCreateTsConfig && isMessaging) {
    aliasConsoleMessage.success(
      `${(isMergeTsConfig && 'Merged') || (isRegenTsConfig && 'Regenerated') || 'Created'} config: ${path.join(
        contextPath,
        configFilename
      )}`
    );
  }
 
  return customTsConfig;
};
 
module.exports = {
  createTsConfig
};