All files files.js

90.47% Statements 38/42
74.5% Branches 38/51
100% Functions 4/4
90.47% Lines 38/42

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1272x 2x 2x 2x 2x                                                                 2x                     9x     9x 9x 9x   9x         9x 9x 9x   9x       9x 9x     9x 36x 27x   9x 8x   8x 5x 5x 1x         9x 9x   9x     9x     9x                       2x 3x 3x   3x 3x           3x     2x        
const { dirname } = require('path');
const { existsSync, readFileSync, writeFileSync } = require('fs');
const { OPTIONS } = require('./global');
const { getLinkUrls, runCmd } = require('./cmds');
const { getComparisonCommitHashes } = require('./parse');
 
/**
 * Update `changelog` and `package.json`
 *
 * @module Files
 */
 
/**
 * ToDo: syntax for the comparison can include the use of caret
 * Review using caret vs a release commit for determining range
 */
/**
 * Update CHANGELOG.md with commit output.
 *
 * @param {object} params
 * @param {{ feat: { commits: Array }, refactor: { commits: Array }, fix: { commits: Array } }} params.commits
 * @param {boolean} params.isBreakingChanges Apply a 'major' weight if true
 * @param {*|string} params.packageVersion
 * @param {object} options
 * @param {string} options.changelogPath
 * @param {string} options.date
 * @param {boolean} options.isBasic
 * @param {boolean} options.isDryRun
 * @param {string} options.releaseDescription
 * @param {object} settings
 * @param {string} settings.breakingChangeReleaseDesc
 * @param {string} settings.fallbackPackageVersion
 * @param {Function} settings.getComparisonCommitHashes
 * @param {Function} settings.getLinkUrls
 * @param {string} settings.headerMd
 * @returns {string}
 */
const updateChangelog = (
  { commits: parsedCommits = {}, isBreakingChanges = false, packageVersion } = {},
  { date, changelogPath, isBasic = false, isDryRun = false, releaseDescription } = OPTIONS,
  {
    breakingChangeReleaseDesc = `\u26A0 BREAKING CHANGES`,
    fallbackPackageVersion = '¯\\_(ツ)_/¯',
    getComparisonCommitHashes: getAliasComparisonCommitHashes = getComparisonCommitHashes,
    getLinkUrls: getAliasLinkUrls = getLinkUrls,
    headerMd = `# Changelog\nAll notable changes to this project will be documented in this file.`
  } = {}
) => {
  const systemTimestamp = ((date && new Date(date)) || new Date()).toLocaleDateString('fr-CA', {
    timeZone: 'UTC'
  });
  const { compareUrl } = getAliasLinkUrls();
  const updatedReleaseDescription = releaseDescription || '';
  const updatedBreakingChanges = (isBreakingChanges && breakingChangeReleaseDesc) || '';
  const fullReleaseDescription =
    (updatedBreakingChanges &&
      updatedReleaseDescription &&
      `${updatedBreakingChanges}\n\n${updatedReleaseDescription}`) ||
    `${updatedBreakingChanges}${updatedReleaseDescription}`;
 
  let header = headerMd;
  let version = fallbackPackageVersion;
  let body = '';
 
  Iif (existsSync(changelogPath)) {
    const [tempHeader, ...tempBody] = readFileSync(changelogPath, 'utf-8').split('##');
    header = tempHeader.trim();
    body = (tempBody.length && `## ${tempBody.join('##').trim()}`) || body;
  } else Eif (!isDryRun) {
    writeFileSync(changelogPath, '');
  }
 
  const displayCommits = Object.values(parsedCommits)
    .sort(({ title: titleA }, { title: titleB }) => titleB.localeCompare(titleA))
    .reduce((str, { title, commits = [] }) => `${str}\n### ${title}\n${commits.join('\n')}\n`, '');
 
  if (packageVersion) {
    version = packageVersion;
 
    if (!isBasic && compareUrl) {
      const { first, last } = getAliasComparisonCommitHashes();
      if (first && last) {
        version = `[${version}](${compareUrl}${first}...${last})`;
      }
    }
  }
 
  const updatedBody = `## ${version} (${systemTimestamp})\n${fullReleaseDescription}\n${displayCommits}`;
  const output = `${header}\n\n${updatedBody}\n${body}${(body && '\n') || ''}`;
 
  Iif (isDryRun) {
    console.info(`\n${updatedBody}`);
  } else {
    writeFileSync(changelogPath, output);
  }
 
  return output;
};
 
/**
 * Apply bump and update package.json
 *
 * @param {'major'|'minor'|'patch'|*} versionBump
 * @param {object} options
 * @param {boolean} options.isDryRun
 * @param {string} options.packagePath
 * @returns {string}
 */
const updatePackage = (versionBump, { isDryRun = false, packagePath } = OPTIONS) => {
  const output = `Version bump: ${versionBump}`;
  const directory = dirname(packagePath);
 
  Eif (!isDryRun) {
    runCmd(
      `(cd ${directory} && npm version ${versionBump} --git-tag-version=false)`,
      'Skipping package.json version... {0}'
    );
  }
 
  return output;
};
 
module.exports = {
  updateChangelog,
  updatePackage
};