Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use application version number for head of changelog #7

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/src/Gren.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
},
{
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 41 additions & 1 deletion lib/src/_utils.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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
*/
Expand All @@ -385,5 +424,6 @@ module.exports = {
requireConfig,
sortObject,
task,
writeConfigToFile
writeConfigToFile,
findRelevantVersion
};