diff --git a/__fixtures__/docsPathsAllVersionsMock.json b/__fixtures__/docsPathsAllVersionsMock.json index 04b8779464..6f510a4266 100644 --- a/__fixtures__/docsPathsAllVersionsMock.json +++ b/__fixtures__/docsPathsAllVersionsMock.json @@ -1,4 +1,13 @@ { + "terraform-docs-common": { + "v0.0.x": [ + { + "path": "terraform/plugin/debugging", + "itemPath": "content/terraform-docs-common/docs/plugin/debugging.mdx", + "created_at": "2025-06-03T18:02:21+00:00" + } + ] + }, "terraform-plugin-framework": { "v1.14.x": [ { diff --git a/scripts/prebuild/prebuild-arm-linux-binary.gz b/scripts/prebuild/prebuild-arm-linux-binary.gz index 88fe72cc83..9b086a1862 100755 Binary files a/scripts/prebuild/prebuild-arm-linux-binary.gz and b/scripts/prebuild/prebuild-arm-linux-binary.gz differ diff --git a/scripts/prebuild/prebuild-arm-mac-binary.gz b/scripts/prebuild/prebuild-arm-mac-binary.gz index ece3efd0e2..108c0a49d0 100755 Binary files a/scripts/prebuild/prebuild-arm-mac-binary.gz and b/scripts/prebuild/prebuild-arm-mac-binary.gz differ diff --git a/scripts/prebuild/prebuild-x64-linux-binary.gz b/scripts/prebuild/prebuild-x64-linux-binary.gz index bfb0df5b5e..a00bffc3eb 100755 Binary files a/scripts/prebuild/prebuild-x64-linux-binary.gz and b/scripts/prebuild/prebuild-x64-linux-binary.gz differ diff --git a/scripts/utils/file-path/url/index.mjs b/scripts/utils/file-path/url/index.mjs index 86f610174d..e0fdc4be2c 100644 --- a/scripts/utils/file-path/url/index.mjs +++ b/scripts/utils/file-path/url/index.mjs @@ -22,14 +22,38 @@ export function getUrlFromFilePath( productConfig = PRODUCT_CONFIG, ) { const repoDir = getProductDirectoryFromFilePath(filePath) - const version = getVersionFromFilePath(filePath) const isValidProduct = productConfig[repoDir] if (!isValidProduct) { throw new Error(`Product not found for ${repoDir}`) - } else { - return allDocsPaths[repoDir][version].find((path) => { - return filePath.endsWith(path.itemPath) - }).path } + + // Check for versionless products and use v0.0.x as the version key + const version = productConfig[repoDir].versionedDocs + ? getVersionFromFilePath(filePath) + : 'v0.0.x' + + // Check if the version exists in allDocsPaths + if (!allDocsPaths[repoDir]) { + throw new Error(`No docs paths found for product: ${repoDir}`) + } + + if (!allDocsPaths[repoDir][version]) { + throw new Error( + `Version ${version} not found for product ${repoDir}. File path: ${filePath}`, + ) + } + + // Find the matching path + const matchedPath = allDocsPaths[repoDir][version].find((path) => { + return filePath.endsWith(path.itemPath) + }) + + if (!matchedPath) { + throw new Error( + `No matching path found for file: ${filePath} in version ${version} of ${repoDir}`, + ) + } + + return matchedPath.path } diff --git a/scripts/utils/file-path/url/index.test.mjs b/scripts/utils/file-path/url/index.test.mjs index dfe1054f5a..b3151eec74 100644 --- a/scripts/utils/file-path/url/index.test.mjs +++ b/scripts/utils/file-path/url/index.test.mjs @@ -52,4 +52,105 @@ describe('getUrlFromFilePath', () => { return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG) }).toThrow(`Product not found for ${repoDir}`) }) + + it('should use v0.0.x for versionless products (terraform-docs-common)', () => { + const filePath = 'content/terraform-docs-common/docs/plugin/debugging.mdx' + const repoDir = 'terraform-docs-common' + const expectedUrl = 'terraform/plugin/debugging' + + getProductDirectoryFromFilePath.mockReturnValue(repoDir) + // versionedDocs: false, so should use v0.0.x + + const result = getUrlFromFilePath( + filePath, + allDocsPathsJsonMock, + PRODUCT_CONFIG, + ) + expect(result).toBe(expectedUrl) + }) + + it('should use v0.0.x for versionless products (hcp-docs)', () => { + const filePath = + 'content/hcp-docs/content/docs/vagrant/reclaim-vagrant-cloud.mdx' + const repoDir = 'hcp-docs' + const expectedUrl = 'hcp/docs/vagrant/reclaim-vagrant-cloud' + + // Add hcp-docs to product config for this test only + const mockProductConfig = { + ...PRODUCT_CONFIG, + 'hcp-docs': { + versionedDocs: false, + productSlug: 'hcp', + }, + } + + // Add hcp-docs to allDocsPaths for this test only + const mockAllDocsPaths = { + ...allDocsPathsJsonMock, + 'hcp-docs': { + 'v0.0.x': [ + { + path: expectedUrl, + itemPath: filePath, + created_at: '2025-06-03T18:02:21+00:00', + }, + ], + }, + } + + getProductDirectoryFromFilePath.mockReturnValue(repoDir) + // versionedDocs: false, so should use v0.0.x + + const result = getUrlFromFilePath( + filePath, + mockAllDocsPaths, + mockProductConfig, + ) + expect(result).toBe(expectedUrl) + }) + + it('should throw an error if no docs paths found for product', () => { + const filePath = 'content/terraform-plugin-framework/v1.14.x/docs/test.mdx' + const repoDir = 'terraform-plugin-framework' + const versionValue = 'v1.14.x' + const allDocsPathsEmpty = {} + + getProductDirectoryFromFilePath.mockReturnValue(repoDir) + getVersionFromFilePath.mockReturnValue(versionValue) + + expect(() => { + return getUrlFromFilePath(filePath, allDocsPathsEmpty, PRODUCT_CONFIG) + }).toThrow(`No docs paths found for product: ${repoDir}`) + }) + + it('should throw an error if version not found for product', () => { + const filePath = 'content/terraform-plugin-framework/v99.99.x/docs/test.mdx' + const repoDir = 'terraform-plugin-framework' + const versionValue = 'v99.99.x' + + getProductDirectoryFromFilePath.mockReturnValue(repoDir) + getVersionFromFilePath.mockReturnValue(versionValue) + + expect(() => { + return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG) + }).toThrow( + `Version ${versionValue} not found for product ${repoDir}. File path: ${filePath}`, + ) + }) + + it('should throw an error if no matching path found for file', () => { + const filePath = + 'content/terraform-plugin-framework/v1.14.x/docs/nonexistent-file.mdx' + const repoDir = 'terraform-plugin-framework' + const versionValue = 'v1.14.x' + + getProductDirectoryFromFilePath.mockReturnValue(repoDir) + getVersionFromFilePath.mockReturnValue(versionValue) + + expect(() => { + return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG) + }).toThrow( + `No matching path found for file: ${filePath} in version ${versionValue} of ${repoDir}`, + ) + }) })