From 46d2757172625ce6daa8103ea2dad5339a2f1194 Mon Sep 17 00:00:00 2001 From: Chris Barth Date: Mon, 4 Sep 2023 14:32:22 -0500 Subject: [PATCH] rebase --- lib/src/Gren.js | 22 +++++++++++++++++----- lib/src/_utils.js | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/lib/src/Gren.js b/lib/src/Gren.js index e29c819c..c8d7f71b 100644 --- a/lib/src/Gren.js +++ b/lib/src/Gren.js @@ -59,6 +59,7 @@ class Gren { this.options.ignoreIssuesWith = utils.convertStringToArray(ignoreIssuesWith); this.options.ignoreCommitsWith = utils.convertStringToArray(ignoreCommitsWith); this.options.ignoreTagsWith = utils.convertStringToArray(ignoreTagsWith); + this.options.version = this.options.version || utils.findRelevantVersion(); if (limit && limit > 0 && limit <= MAX_TAGS_LIMIT) { this.options.limit = limit; @@ -1104,7 +1105,7 @@ class Gren { if (this.options.head != null) { const latest = [{ ...sortedReleaseDates[0], - name: this.options.head, + name: this.options.version, date: (new Date()).toISOString() }, { @@ -1154,12 +1155,23 @@ class Gren { for (const releaseRange of releaseRanges) { let commits; if (releaseRange[1].name != null) { + let response; + try { + response = await this.repo.compareBranches( + releaseRange[1].name, + releaseRange[0].name + ); + } catch { + // If the above failed, it is because we are using a calculated version as the first `name` + // Use the head of the branch instead + response = await this.repo.compareBranches( + releaseRange[1].name, + this.options.head + ); + } ({ data: { commits } - } = await this.repo.compareBranches( - releaseRange[1].name, - releaseRange[0].name - )); + } = response); } else { ({ data: commits } = await this.repo.listCommits({ since: releaseRange[1].date, diff --git a/lib/src/_utils.js b/lib/src/_utils.js index f3061024..4cb7daf6 100644 --- a/lib/src/_utils.js +++ b/lib/src/_utils.js @@ -1,5 +1,6 @@ const chalk = require('chalk'); const fs = require('fs'); +const path = require('path'); const ora = require('ora'); const YAML = require('json2yaml'); const Path = require('path'); @@ -360,6 +361,44 @@ function getGrenConfig(path, ...args) { return config; } +/** + * Look at the `package.json` and try to get the version of the current program + * If that fails, return the version of this package + * + * @returns {string} The version of the current package + */ +function findRelevantVersion() { + const startPath = __dirname; + let currentPath = startPath; + let lastVersion = null; + let passedNodeModules = false; + + while (true) { + const dirName = path.basename(currentPath); + + if (dirName === 'node_modules') { + passedNodeModules = true; + } else { + const packageJsonPath = path.join(currentPath, 'package.json'); + if (fs.existsSync(packageJsonPath)) { + lastVersion = require(packageJsonPath).version; + if (passedNodeModules) { + break; + } + } + } + + const newPath = path.resolve(currentPath, '..'); + if (newPath === currentPath) { + break; // We're at the root and can't go any higher + } + + currentPath = newPath; + } + + return lastVersion; +} + /** * Just a noop function */ @@ -385,5 +424,6 @@ module.exports = { requireConfig, sortObject, task, - writeConfigToFile + writeConfigToFile, + findRelevantVersion };