Skip to content

Commit 4db12e6

Browse files
fix terraform docs common metadata (#438)
* Assign a placeholder version for unversioned docs * Move all TFC versions to 'v0.0.x' * Give version less docs a version * Use PRODUCT_CONFIG when we can * fix sitemaps for terraform-docs-common * Check PRODUCT_CONFIG in more places * fix file path * remove console.log * fix docs route tests * change back to null * fix more tests * fix gather-version tests * fix test * put aloglia check back * fix tests * remove extra console.log --------- Co-authored-by: Leah Bush <leah.bush@hashicorp.com>
1 parent 6c7f247 commit 4db12e6

File tree

18 files changed

+84
-20
lines changed

18 files changed

+84
-20
lines changed

__fixtures__/versionMetadata.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@
382382
"isLatest": false
383383
}
384384
],
385-
"terraform-docs-common": [],
385+
"terraform-docs-common": [
386+
{
387+
"version": "v0.0.x",
388+
"releaseStage": "stable",
389+
"isLatest": true
390+
}
391+
],
386392
"terraform-plugin-framework": [
387393
{
388394
"version": "v1.5.x",

app/api/content/[productSlug]/doc/[version]/[...docsPath]/route.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ vi.mock('@api/docsPathsAllVersions.json', () => {
5656
vi.mock('@utils/productConfig.mjs', () => {
5757
return {
5858
PRODUCT_CONFIG: {
59-
'terraform-enterprise': { contentDir: 'docs' },
60-
'terraform-plugin-framework': { contentDir: 'docs' },
59+
'terraform-enterprise': { contentDir: 'docs', versionedDocs: true },
60+
'terraform-plugin-framework': { contentDir: 'docs', versionedDocs: true },
6161
},
6262
}
6363
})

app/api/content/[productSlug]/doc/[version]/[...docsPath]/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ export async function GET(request: Request, { params }: { params: GetParams }) {
140140
result: {
141141
fullPath: parsedDocsPath,
142142
product: productSlug,
143-
version: parsedVersion,
143+
version: PRODUCT_CONFIG[productSlug].versionedDocs
144+
? parsedVersion
145+
: 'v0.0.x',
144146
metadata,
145147
subpath: 'docs', // TODO: I guess we could grab the first part of the rawDocsPath? Is there something I am missing here?
146148
markdownSource,

app/utils/allDocsPaths.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { Ok, Err, errorResultToString } from '@utils/result'
77
import docsPathsAllVersions from '@api/docsPathsAllVersions.json'
88
import { getProductVersion } from './contentVersions'
9+
import { PRODUCT_CONFIG } from './productConfig.mjs'
910

1011
export const getDocsPaths = async (
1112
productSlugs: string[],
@@ -21,6 +22,9 @@ export const getDocsPaths = async (
2122
}
2223

2324
if (docsPathsData[productSlug]) {
25+
if (!PRODUCT_CONFIG[productSlug].versionedDocs) {
26+
return docsPathsData[productSlug]['v0.0.x']
27+
}
2428
return docsPathsData[productSlug][latestProductVersion.value]
2529
}
2630
console.error(`Product, ${productSlug}, not found in docs paths`)

app/utils/contentVersions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import versionMetadata from '@api/versionMetadata.json'
77
import { Ok, Err } from '@utils/result'
88

9+
import { PRODUCT_CONFIG } from '@utils/productConfig.mjs'
10+
911
type ProductVersionMetadata = {
1012
version: string
1113
isLatest: boolean
@@ -34,7 +36,7 @@ export const getProductVersion = (
3436
},
3537
)
3638

37-
if (!foundVersion) {
39+
if (!PRODUCT_CONFIG[productSlug].versionedDocs) {
3840
parsedVersion = '' // Set to an empty string if no latest version is found, as in the case for versionless docs such as terraform-docs-common
3941
} else {
4042
parsedVersion = foundVersion.version

app/utils/utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ vi.mock('@api/docsPathsAllVersions.json', () => {
2929
}
3030
})
3131

32+
vi.mock('@utils/productConfig.mjs', () => {
33+
return {
34+
PRODUCT_CONFIG: {
35+
terraform: { contentDir: 'docs', versionedDocs: true },
36+
},
37+
}
38+
})
39+
3240
test('getProductVersion should return error for non-existent product', () => {
3341
const expected = {
3442
ok: false,

scripts/add-version-to-nav-data.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import fs from 'node:fs/promises'
77
import { PRODUCT_CONFIG } from '../app/utils/productConfig.mjs'
8-
import semver from 'semver'
98

109
/**
1110
* Adds version information to navigation data in a JSON file.
@@ -36,7 +35,7 @@ export async function addVersionToNavData(filePath, versionMetadata) {
3635
const [product, version] = relativePath.split('/')
3736

3837
// We are looking at a versionless doc
39-
if (!semver.valid(semver.coerce(version))) {
38+
if (PRODUCT_CONFIG[product].versionedDocs === false) {
4039
return
4140
}
4241

scripts/algolia/convert-mdx-to-json/index.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ export async function getLatestProductVersionDirectories(dir, versionMetadata) {
3131
directoryPath,
3232
versionMetadata,
3333
)
34+
3435
// getLatestVersion will return null for versionless products
3536
// if directory is for versionless docs, return null
36-
return latestProductVersion == null
37+
return latestProductVersion == 'v0.0.x'
3738
? directoryPath
3839
: path.join(directoryPath, latestProductVersion)
3940
}),

scripts/gather-all-versions-docs-paths.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function gatherAllVersionsDocsPaths(versionMetadata) {
3838
const contentPath = path.join(
3939
'./content',
4040
product,
41-
version?.version ?? '',
41+
PRODUCT_CONFIG[product].versionedDocs ? (version?.version ?? '') : '',
4242
PRODUCT_CONFIG[product].contentDir,
4343
)
4444

scripts/gather-version-metadata.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import path from 'path'
88
// Third-party
99
import semver from 'semver'
1010

11+
import { PRODUCT_CONFIG } from '../app/utils/productConfig.mjs'
12+
1113
/**
1214
* Given a content directory, and a JSON output file, build version metadata
1315
* based on the content directory structure, and return it.
@@ -31,6 +33,21 @@ export async function gatherVersionMetadata(contentDir) {
3133
for (const product of products) {
3234
// Initialize the product array
3335
versionMetadata[product] = []
36+
37+
/**
38+
* If the product is not versioned, we add a single entry for the
39+
* "v0.0.x" version, which is a placeholder for non-versioned products.
40+
* This is useful for products that do not have versioned documentation,
41+
* such as the HashiCorp Cloud Platform.
42+
*/
43+
if (PRODUCT_CONFIG[product].versionedDocs === false) {
44+
versionMetadata[product].push({
45+
version: 'v0.0.x',
46+
releaseStage: 'stable',
47+
isLatest: true,
48+
})
49+
}
50+
3451
/**
3552
* We expect the product directory to contain a directory for each version.
3653
* We expect that either:

scripts/gather-version-metadata.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import { beforeEach, expect, it, vi } from 'vitest'
77
import { vol } from 'memfs'
88
import { gatherVersionMetadata } from './gather-version-metadata.mjs'
99

10+
vi.mock('../app/utils/productConfig.mjs', () => {
11+
return {
12+
PRODUCT_CONFIG: {
13+
'terraform-enterprise': { contentDir: 'docs', versionedDocs: true },
14+
terraform: { contentDir: 'docs', versionedDocs: true },
15+
},
16+
}
17+
})
18+
1019
// tell vitest to use fs mock from __mocks__ folder
1120
// this can be done in a setup file if fs should always be mocked
1221
vi.mock('fs')

scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import remark from 'remark'
77
import remarkMdx from 'remark-mdx'
88
import flatMap from 'unist-util-flatmap'
9-
import semver from 'semver'
109

1110
import { PRODUCT_CONFIG } from '../../../app/utils/productConfig.mjs'
1211

@@ -28,8 +27,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
2827
*/
2928
const [product, version] = relativePath.split('/')
3029

31-
// We are looking at a versionless doc
32-
if (semver.valid(semver.coerce(version)) === null) {
30+
if (PRODUCT_CONFIG[product].versionedDocs === false) {
3331
return
3432
}
3533

scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,43 @@ vi.mock('../../../app/utils/productConfig.mjs', () => {
1313
PRODUCT_CONFIG: {
1414
terraform: {
1515
basePaths: ['cli', 'internals', 'intro', 'language'],
16+
versionedDocs: true,
1617
},
1718
'terraform-enterprise': {
1819
basePaths: ['enterprise'],
20+
versionedDocs: true,
1921
},
2022
'terraform-cdk': {
2123
basePaths: ['cdktf'],
24+
versionedDocs: true,
2225
},
2326
'terraform-docs-agents': {
2427
basePaths: ['cloud-docs/agents'],
28+
versionedDocs: true,
2529
},
2630
'terraform-plugin-framework': {
2731
basePaths: ['plugin/framework'],
32+
versionedDocs: true,
2833
},
2934
'terraform-plugin-log': {
3035
basePaths: ['plugin/log'],
36+
versionedDocs: true,
3137
},
3238
'terraform-plugin-mux': {
3339
basePaths: ['plugin/mux'],
40+
versionedDocs: true,
3441
},
3542
'terraform-plugin-sdk': {
3643
basePaths: ['plugin/sdkv2'],
44+
versionedDocs: true,
3745
},
3846
'terraform-plugin-testing': {
3947
basePaths: ['plugin/testing'],
48+
versionedDocs: true,
49+
},
50+
'terraform-docs-common': {
51+
basePaths: [],
52+
versionedDocs: false,
4053
},
4154
},
4255
}

scripts/mdx-transforms/build-mdx-transforms.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
} from './rewrite-internal-redirects/rewrite-internal-redirects.mjs'
2626
import { transformExcludeTerraformContent } from './exclude-terraform-content/index.mjs'
2727

28+
import { PRODUCT_CONFIG } from '../../app/utils/productConfig.mjs'
29+
2830
/**
2931
* Given a target directory,
3032
* Apply MDX transforms to all `.mdx` files found in the directory and its
@@ -61,7 +63,9 @@ export async function buildMdxTransforms(
6163
* if versionless, version becomes the content dir
6264
* which will cause an error when trying resolve partials
6365
*/
64-
const verifiedVersion = semver.valid(semver.coerce(version)) ? version : ''
66+
const verifiedVersion = PRODUCT_CONFIG[repoSlug].versionedDocs
67+
? version
68+
: ''
6569
const verifiedContentDir = semver.valid(semver.coerce(version))
6670
? contentDir
6771
: version

scripts/utils/file-path/latest-version/index.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import { getProductDirectoryFromFilePath } from '../product-directory/index.mjs'
77
import { getVersionFromFilePath } from '../version/index.mjs'
88

9+
import { PRODUCT_CONFIG } from '../../../../app/utils/productConfig.mjs'
10+
911
/**
1012
* Checks if the given file path corresponds to the latest version of the document.
1113
*
@@ -29,7 +31,7 @@ export function isLatestVersion(filePath, versionMetadata) {
2931
const productVersions = versionMetadata[productDir]
3032

3133
// if repo dir exists, and is an empty array (aka versionless docs)
32-
if (Array.isArray(productVersions) && productVersions.length === 0) {
34+
if (PRODUCT_CONFIG[productDir].versionedDocs === false) {
3335
return true
3436
}
3537
// check that the file path version exists in the version metadata and is the latest version

scripts/utils/file-path/latest-version/index.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ describe('getLatestVersion', () => {
118118
expect(latestVersion).toBe('v0.14.x')
119119
})
120120

121-
test('should return null if product does not have versions', () => {
121+
test('should return v0.0.x for versionless docs', () => {
122122
getProductDirectoryFromFilePath.mockReturnValue('terraform-docs-common')
123123
const filePath =
124124
'content/terraform-docs-common/docs/cloud-docs/cost-estimation/gcp.mdx'
125125
const latestVersion = getLatestVersion(filePath, versionMetadata)
126-
expect(latestVersion).toBeNull()
126+
expect(latestVersion).toBe('v0.0.x')
127127
})
128128
})

scripts/utils/file-path/version/index.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: BUSL-1.1
44
*/
55

6-
import semver from 'semver'
76
/**
87
* Extracts the version from a given file path.
98
*
@@ -40,6 +39,6 @@ export function getVersionFromFilePath(filePath) {
4039
* e.g. terraform-docs-common
4140
*/
4241

43-
return semver.valid(semver.coerce(version))
42+
return version === 'v0.0.x'
4443
}
4544
}

scripts/utils/file-path/version/index.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('getVersionFromFilePath', () => {
3434
expect(version).toBe('v202410-1')
3535
})
3636

37-
test('should return null if the file path is for versionless docs', () => {
37+
test('should return false if the file path is for versionless docs', () => {
3838
const filePath =
3939
'content/terraform-docs-common/docs/cloud-docs/cost-estimation/gcp.mdx'
4040
const version = getVersionFromFilePath(filePath)
41-
expect(version).toBeNull()
41+
expect(version).toBe(false)
4242
})
4343
})

0 commit comments

Comments
 (0)