Skip to content

Commit 25cb69c

Browse files
authored
Convert precompile.js to TypeScript (#56556)
1 parent 2443c65 commit 25cb69c

File tree

8 files changed

+49
-28
lines changed

8 files changed

+49
-28
lines changed

src/ghes-releases/scripts/deprecate/archive-version.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import loadRedirects from '@/redirects/lib/precompile'
1919
import { loadPageMap, loadPages } from '@/frame/lib/page-data'
2020
import { languageKeys } from '@/languages/lib/languages'
2121
import { RewriteAssetPathsPlugin } from '@/ghes-releases/scripts/deprecate/rewrite-asset-paths'
22+
import type { Page } from '@/types'
2223

2324
const port = '4001'
2425
const host = `http://localhost:${port}`
@@ -27,7 +28,7 @@ const GH_PAGES_URL = `https://github.github.com/docs-ghes-${version}`
2728

2829
// Once page-data.js is converted to TS,
2930
// we can import the more comprehesive type
30-
type PageList = Array<Object>
31+
type PageList = Page[]
3132
type MapObj = { [key: string]: string }
3233

3334
program
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import fs from 'fs'
22

3+
type Redirects = Record<string, string>
4+
35
// This function expects a .txt file in a specific format.
4-
export default function getExceptionRedirects(exceptionsTxtFile) {
5-
const exceptions = {}
6+
export default function getExceptionRedirects(exceptionsTxtFile: string): Redirects {
7+
const exceptions: Redirects = {}
68
const exceptionRedirectsLines = fs
79
.readFileSync(exceptionsTxtFile, 'utf-8')
810
.split('\n')
911
.filter(Boolean)
1012
.map((line) => line.trim())
1113
.filter((line) => !line.startsWith('#'))
1214

13-
let parent = null
15+
let parent: string | null = null
1416
for (const line of exceptionRedirectsLines) {
1517
if (line.startsWith('-')) {
1618
if (!parent) {
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-version'
22
import { getPathWithoutVersion } from '@/frame/lib/path-utils'
33

4-
export default function permalinkRedirects(permalinks, redirectFrom) {
5-
const redirects = {}
4+
import type { Permalink } from '@/types'
5+
6+
type Redirects = Record<string, string>
7+
8+
export default function permalinkRedirects(
9+
permalinks: Permalink[],
10+
redirectFrom: string[],
11+
): Redirects {
12+
const redirects: Redirects = {}
613
if (!permalinks.length) return redirects
714

815
// The following is handling for versionless redirect fallbacks!
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import { readCompressedJsonFileFallback } from '@/frame/lib/read-json-file'
22
import getExceptionRedirects from './exception-redirects'
33
import { latest } from '@/versions/lib/enterprise-server-releases'
44

5+
import type { Page } from '@/types'
6+
57
const EXCEPTIONS_FILE = './src/redirects/lib/static/redirect-exceptions.txt'
68

9+
type Redirects = Record<string, string>
10+
711
// This function runs at server warmup and precompiles possible redirect routes.
812
// It outputs them in key-value pairs within a neat JavaScript object: { oldPath: newPath }
9-
export async function precompileRedirects(pageList) {
10-
const allRedirects = readCompressedJsonFileFallback('./src/redirects/lib/static/developer.json')
13+
export async function precompileRedirects(pageList: Page[]): Promise<Redirects> {
14+
const allRedirects: Redirects = readCompressedJsonFileFallback(
15+
'./src/redirects/lib/static/developer.json',
16+
)
1117

12-
const externalRedirects = readCompressedJsonFileFallback(
18+
const externalRedirects: Redirects = readCompressedJsonFileFallback(
1319
'./src/redirects/lib/external-sites.json',
1420
)
1521
Object.assign(allRedirects, externalRedirects)
@@ -37,7 +43,7 @@ export async function precompileRedirects(pageList) {
3743
// The advantage of the exception redirects file is that it's encoded in plain
3844
// text so it's possible to write comments and it's also possible to write 1
3945
// destination URL once for each N redirect origins.
40-
const exceptions = getExceptionRedirects(EXCEPTIONS_FILE)
46+
const exceptions = getExceptionRedirects(EXCEPTIONS_FILE) as Redirects
4147
Object.assign(allRedirects, exceptions)
4248

4349
Object.entries(allRedirects).forEach(([fromURI, toURI]) => {

src/redirects/scripts/get-new-dotcom-path.js renamed to src/redirects/scripts/get-new-dotcom-path.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const markdownRegex = new RegExp(`${markdownExtension}$`, 'm')
1515

1616
const newDotcomDir = 'content/github'
1717

18-
const oldPath = process.argv.slice(2)[0]
18+
const oldPath: string = process.argv.slice(2)[0]
1919
assert(oldPath, 'must provide old dotcom path like "foo" or "articles/foo"')
2020

21-
let filename = oldPath
21+
let filename: string = oldPath
2222

2323
// get last part of path
24-
if (filename.includes('/')) filename = last(filename.split('/'))
24+
if (filename.includes('/')) filename = last(filename.split('/')) as string
2525

2626
// first check whether name is a category
2727
const categoryDir = `${newDotcomDir}/${filename.replace(markdownRegex, '')}`
@@ -35,7 +35,7 @@ if (fs.existsSync(categoryDir)) {
3535
if (!filename.endsWith(markdownExtension)) filename = filename + markdownExtension
3636

3737
// run find command
38-
const newPath = execSync(`find ${newDotcomDir} -name ${filename}`).toString()
38+
const newPath: string = execSync(`find ${newDotcomDir} -name ${filename}`).toString()
3939

4040
if (!newPath) {
4141
console.log(`Cannot find new path for "${oldPath}". Check the name and try again.\n`)

src/redirects/scripts/helpers/add-redirect-to-frontmatter.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// add a new redirect string to redirect_from frontmatter
2+
3+
export default function addRedirectToFrontmatter(
4+
redirectFromData: string | string[] | undefined,
5+
newRedirectString: string,
6+
): string[] {
7+
if (Array.isArray(redirectFromData) && !redirectFromData.includes(newRedirectString)) {
8+
redirectFromData.push(newRedirectString)
9+
return redirectFromData
10+
} else if (typeof redirectFromData === 'string') {
11+
const redirectArray = [redirectFromData]
12+
redirectArray.push(newRedirectString)
13+
return redirectArray
14+
} else {
15+
return [newRedirectString]
16+
}
17+
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export type Permalink = {
323323
pageVersion: string
324324
title: string
325325
href: string
326+
hrefWithoutLanguage: string
326327
}
327328

328329
export type FrontmatterVersions = {
@@ -361,6 +362,7 @@ export type Page = {
361362
effectiveDate?: string
362363
fullTitle?: string
363364
render: (context: Context) => Promise<string>
365+
buildRedirects: () => Record<string, string>
364366
octicon?: string
365367
category?: string[]
366368
complexity?: string[]

0 commit comments

Comments
 (0)