Merge pull request #239 from creative-commoners/pulls/master/changelog-sort-order

FIX Ensure changelogs are sorted correctly.
This commit is contained in:
Steve Boyd 2022-05-20 12:26:34 +12:00 committed by GitHub
commit 305d41e073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 1 deletions

View File

@ -1,10 +1,40 @@
import { SilverstripeDocument } from "../types";
import path from 'path';
/**
* Validates whether a given string looks like a semantic versioning number
*/
function isVersionNumber(value: string): boolean {
return /^\d+\.\d+\.\d+(-(alpha|beta|rc)\d*)?$/.test(value);
}
const sortFiles = (a: SilverstripeDocument, b: SilverstripeDocument): number => {
if (a.isIndex !== b.isIndex) {
//return a.isIndex ? -1 : 1;
}
return a.fileAbsolutePath > b.fileAbsolutePath ? 1 : -1;
let compA = a.fileAbsolutePath;
let compB = b.fileAbsolutePath;
const compareOptions = {
numeric: false,
sensitivity: 'case',
};
// Only apply special comparison logic for files in the same directory
if (path.dirname(a.fileAbsolutePath) === path.dirname(b.fileAbsolutePath)) {
// Compare numbers and version numbers numerically
if (
(!isNaN(a.fileTitle) && !isNaN(b.fileTitle))
|| (isVersionNumber(a.fileTitle) && isVersionNumber(a.fileTitle))
) {
// compare numerically
compA = a.fileTitle;
compB = b.fileTitle;
compareOptions.numeric = true;
}
}
return compA.localeCompare(compB, 'en', compareOptions);
};
export default sortFiles;