Skip to content

[Website] Changelog component docs script format enhancements #2953

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@

### Patch Changes

<!-- START components/card -->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- START components/card -->
<!-- START components/card -->

[Note] There is a linting error that gets thrown for this format and is only fixed by adding a new line after this comment? Any way to disable this, or another workaround?

`Card` - Test change multi-line
- Change one
- Change two
<!-- END components/card -->

<!-- START components/tag -->
`Tag` - Test change
<!-- END components/tag -->

<!-- START components/card -->
`Card` - Test change three
<!-- END components/card -->

<!-- START components/link/standalone -->
`Link::Standalone` - Test change
<!-- END components/link/standalone -->

`AdvancedTable` - Fixed a bug that prevented the `model` from updating when the argument changes

<small class="doc-whats-new-changelog-metadata">[#2919](https://github.com/hashicorp/design-system/pull/2919)</small>
Expand Down
18 changes: 18 additions & 0 deletions website/docs/whats-new/release-notes/partials/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@

**Patch changes**

<!-- START components/card -->
`Card` - Test change multi-line
- Change one
- Change two
<!-- END components/card -->

<!-- START components/tag -->
`Tag` - Test change
<!-- END components/tag -->

<!-- START components/card -->
`Card` - Test change three
<!-- END components/card -->

<!-- START components/link/standalone -->
`Link::Standalone` - Test change
<!-- END components/link/standalone -->

`AdvancedTable` - Fixed a bug that prevented the `model` from updating when the argument changes

<small class="doc-whats-new-changelog-metadata">[#2919](https://github.com/hashicorp/design-system/pull/2919)</small>
Expand Down
138 changes: 49 additions & 89 deletions website/lib/changelog/generate-component-changelog-entries.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,18 @@ const readVersionFromPackageJson = (filePath) => {
};

const getComponentPaths = (baseDir) => {
const components = {};
const components = [];
try {
const folders = fs.readdirSync(baseDir, { withFileTypes: true });
folders.forEach((folder) => {
if (folder.isDirectory()) {
const componentPath = `${baseDir}/${folder.name}`;
const partialsPath = `${componentPath}/partials`;
if (fs.existsSync(partialsPath)) {
// we have some special cases where intermediate namespacing is used to group components:
if (baseDir.endsWith('/copy')) {
components[`copy-${folder.name}`] = componentPath;
} else if (baseDir.endsWith('/form')) {
if (folder.name === 'primitives') {
const primitiveNames = [
'character-count',
'error',
'field',
'fieldset',
'helper-text',
'indicator',
'label',
'legend',
];
primitiveNames.forEach((componentName) => {
components[`form-${componentName}`] = componentPath;
});
} else {
components[`form-${folder.name}`] = componentPath;
}
} else if (baseDir.endsWith('/layouts')) {
if (folder.name === 'app-frame') {
components[`${folder.name}`] = componentPath;
} else {
components[`layout-${folder.name}`] = componentPath;
}
} else if (baseDir.endsWith('/link')) {
components[`link-${folder.name}`] = componentPath;
} else if (baseDir.endsWith('/stepper')) {
// The components/stepper/indicator page contains both the Stepper::Step::Indicator and Stepper::Task::Indicator
if (folder.name === 'indicator') {
components[`stepper-step-${folder.name}`] = componentPath;
components[`stepper-task-${folder.name}`] = componentPath;
} else {
components[`stepper-${folder.name}`] = componentPath;
}
} else {
components[folder.name] = componentPath;
}
components.push(componentPath.replace('./docs/', ''));
} else {
// For component docs that are nested, we need to recursively get their paths
components.push(...getComponentPaths(componentPath));
}
}
});
Expand All @@ -91,38 +55,26 @@ const extractVersion = (changelogContent, version) => {
return match ? match[0] : null;
};

const convertComponentNameFormat = (componentName) => {
const twoLevelComponentNames = ['copy', 'form', 'layout', 'link', 'stepper'];
const threeLevelComponentNames = [
'stepper-step-indicator',
'stepper-task-indicator',
];
if (twoLevelComponentNames.includes(componentName.split('-')[0])) {
let words = componentName
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1));
if (threeLevelComponentNames.includes(componentName)) {
return words.join('::');
} else {
return words[0] + '::' + words.slice(1).join('');
}
} else {
return componentName
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
}
};

const extractComponentChangelogEntries = (components, lastVersionContent) => {
const componentChangelogEntries = {};

Object.keys(components).forEach((componentName) => {
const formattedComponentName = convertComponentNameFormat(componentName);
const regex = new RegExp(`^\`${formattedComponentName}\` - .*$`, 'gm');
components.forEach((componentName) => {
const regex = new RegExp(
`^(<!-- START ${componentName})((.|\n)*?)(<!-- END ${componentName} -->)$`,
'gm',
);
const matches = lastVersionContent.match(regex);
if (matches) {
componentChangelogEntries[componentName] = matches;
const cleanedMatches = [];
matches.forEach((match) => {
// Remove the start and end comments to get the changelog entry
const cleanMatch = match
.replace(`<!-- START ${componentName} -->`, '')
.replace(`<!-- END ${componentName} -->`, '')
.trim();
cleanedMatches.push(cleanMatch);
});
componentChangelogEntries[componentName] = cleanedMatches;
}
});

Expand All @@ -131,14 +83,14 @@ const extractComponentChangelogEntries = (components, lastVersionContent) => {

const updateComponentVersionHistory = (componentChangelogEntries, version) => {
Object.keys(componentChangelogEntries).forEach((componentName) => {
const versionHistoryPath = `${allComponentsPath[componentName]}/partials/version-history/version-history.md`;
const versionHistoryPath = `./docs/${componentName}/partials/version-history/version-history.md`;
let versionHistoryContent = '';

if (fs.existsSync(versionHistoryPath)) {
versionHistoryContent = fs.readFileSync(versionHistoryPath, 'utf8');
} else {
fs.mkdirSync(
`${allComponentsPath[componentName]}/partials/version-history`,
`./docs/${componentName}/partials/version-history/version-history`,
{ recursive: true },
);
}
Expand All @@ -149,10 +101,7 @@ const updateComponentVersionHistory = (componentChangelogEntries, version) => {
const newEntries = componentChangelogEntries[componentName]
.map((entry) => {
// If the component is a form primitive, we want to keep the component name in the description
if (
allComponentsPath[componentName] ===
'./docs/components/form/primitives'
) {
if (componentName === 'components/form/primitives') {
return entry;
} else {
return entry.split(' - ')[1];
Expand All @@ -167,7 +116,7 @@ const updateComponentVersionHistory = (componentChangelogEntries, version) => {

const updateComponentFrontMatter = (componentChangelogEntries, version) => {
Object.keys(componentChangelogEntries).forEach((componentName) => {
const indexPath = `${allComponentsPath[componentName]}/index.md`;
const indexPath = `${allComponentPaths[componentName]}/index.md`;

if (fs.existsSync(indexPath)) {
// Fetch the index markdown file
Expand Down Expand Up @@ -236,6 +185,23 @@ const cleanComponentFrontMatter = (components, version) => {
});
};

const cleanChangelogContent = (filePath) => {
try {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
const newLines = [];
lines.forEach((line) => {
if (!(line.startsWith('<!-- START') || line.startsWith('<!-- END'))) {
newLines.push(line);
}
});
fs.writeFileSync(filePath, newLines.join('\n'), 'utf8');
} catch (err) {
console.error(`Error cleaning changelog content from ${filePath}:`, err);
return null;
}
};

const isNotPatchVersion = (version) => {
// eslint-disable-next-line no-unused-vars
const [major, minor, patch] = version.split('.').map(Number);
Expand All @@ -249,25 +215,15 @@ const version = readVersionFromPackageJson(

// Determine component paths
const componentPaths = getComponentPaths('./docs/components');
const copyComponentPaths = getComponentPaths('./docs/components/copy');
const formComponentPaths = getComponentPaths('./docs/components/form');
const linkComponentPaths = getComponentPaths('./docs/components/link');
const stepperComponentPaths = getComponentPaths('./docs/components/stepper');
const tableComponentPaths = getComponentPaths('./docs/components/table');
const layoutComponentPaths = getComponentPaths('./docs/layouts');
const overrideComponentPaths = getComponentPaths('./docs/overrides');
const utilityComponentPaths = getComponentPaths('./docs/utilities');
const allComponentsPath = {
const allComponentPaths = [
...componentPaths,
...copyComponentPaths,
...formComponentPaths,
...linkComponentPaths,
...stepperComponentPaths,
...tableComponentPaths,
...layoutComponentPaths,
...overrideComponentPaths,
...utilityComponentPaths,
};
];

// Read the main changelog entry for components
const changelogContent = readChangelogContent(
Expand All @@ -278,7 +234,7 @@ const currentVersionContent = extractVersion(changelogContent, version);

// Extracts changelog entries for each components
const componentChangelogEntries = extractComponentChangelogEntries(
allComponentsPath,
allComponentPaths,
currentVersionContent,
);

Expand All @@ -288,8 +244,12 @@ updateComponentVersionHistory(componentChangelogEntries, version);
// Check if the current version is a new minor version
if (isNotPatchVersion(version)) {
// Clean previous front matter status for all components
cleanComponentFrontMatter(allComponentsPath, version);
cleanComponentFrontMatter(allComponentPaths, version);
}

// Update front matter for each updated component
updateComponentFrontMatter(componentChangelogEntries, version);

// // Clean the changelog entries for components
cleanChangelogContent('./docs/whats-new/release-notes/partials/components.md');
cleanChangelogContent('../packages/components/CHANGELOG.md');
Loading