-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
/* | ||
* This script builds FF.com's redirects | ||
*/ | ||
|
||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
import chalk from "chalk"; | ||
import fs from "fs-extra"; | ||
|
||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const { | ||
params: { currentVersion, ltsVersion }, | ||
} = await fs.readJSON(path.resolve(dirname, "data", "versions.json")); | ||
|
||
try { | ||
const content = `/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
// Map of incoming URL paths to redirect URLs | ||
const routes = new Map([ | ||
["/docs/apis", "/docs/api/${currentVersion}"], | ||
["/docs/api/current", "/docs/api/${currentVersion}"], | ||
["/docs/api/lts", "/docs/api/${ltsVersion}"], | ||
]); | ||
/** | ||
* Handles incoming HTTP requests and redirects them to the appropriate URL based on the current and LTS versions. | ||
* It reads the versions from /docs/data/versions.json and matches the incoming URL to a set of predefined routes. | ||
* If a matching route is found, it constructs and returns the redirect URL. Otherwise, it returns a 404 response. | ||
*/ | ||
module.exports = async (context, { headers }) => { | ||
const { pathname, search } = new URL(headers["x-ms-original-url"]); | ||
const route = [...routes].find(([path, _]) => pathname.startsWith(path)); | ||
context.res = { | ||
status: route ? 302 : 404, | ||
headers: { location: route ? \`\${pathname.replace(...route)}\${search}\` : "/404" }, | ||
}; | ||
}; | ||
`; | ||
const versionPath = path.resolve(dirname, "api", "fallback", "index.js"); | ||
await fs.writeFile(versionPath, content); | ||
} catch (error) { | ||
console.error(chalk.red("Redirects could not be generated due to one or more errors:")); | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
|
||
console.log(chalk.green("Redirects generated!")); | ||
process.exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters