From 306cadd1759e62ff49503519f4234b05ee03cf35 Mon Sep 17 00:00:00 2001 From: Martijn Janssen Date: Wed, 11 Mar 2020 22:13:30 +0100 Subject: [PATCH] chore(gatsby): migrate normalize-page-path to typescript (#22188) --- .../gatsby/src/utils/normalize-page-path.js | 14 --------- .../gatsby/src/utils/normalize-page-path.ts | 29 +++++++++++++++++++ .../gatsby/src/utils/websocket-manager.js | 17 ++--------- 3 files changed, 31 insertions(+), 29 deletions(-) delete mode 100644 packages/gatsby/src/utils/normalize-page-path.js create mode 100644 packages/gatsby/src/utils/normalize-page-path.ts diff --git a/packages/gatsby/src/utils/normalize-page-path.js b/packages/gatsby/src/utils/normalize-page-path.js deleted file mode 100644 index b393690c738a6..0000000000000 --- a/packages/gatsby/src/utils/normalize-page-path.js +++ /dev/null @@ -1,14 +0,0 @@ -// This is a duplicate of the runtime util -// file cache-dir/normalize-page-path.js -module.exports = path => { - if (path === undefined) { - return path - } - if (path === `/`) { - return `/` - } - if (path.charAt(path.length - 1) === `/`) { - return path.slice(0, -1) - } - return path -} diff --git a/packages/gatsby/src/utils/normalize-page-path.ts b/packages/gatsby/src/utils/normalize-page-path.ts new file mode 100644 index 0000000000000..06247a4f37a42 --- /dev/null +++ b/packages/gatsby/src/utils/normalize-page-path.ts @@ -0,0 +1,29 @@ +// This is a duplicate of the runtime util +// file cache-dir/normalize-page-path.js +// While still a duplicate, this one is +// rewritten in typescript +export function normalizePagePath(path: string): string { + if (path === undefined) { + return path + } + if (path === `/`) { + return `/` + } + if (path.charAt(path.length - 1) === `/`) { + return path.slice(0, -1) + } + return path +} + +export function denormalizePagePath(path: string): string { + if (path === undefined) { + return path + } + if (path === `/`) { + return `/` + } + if (path.charAt(path.length - 1) !== `/`) { + return path + `/` + } + return path +} diff --git a/packages/gatsby/src/utils/websocket-manager.js b/packages/gatsby/src/utils/websocket-manager.js index e64c6cf441297..552e57423df1b 100644 --- a/packages/gatsby/src/utils/websocket-manager.js +++ b/packages/gatsby/src/utils/websocket-manager.js @@ -4,7 +4,7 @@ const path = require(`path`) const { store } = require(`../redux`) const fs = require(`fs`) const pageDataUtil = require(`../utils/page-data`) -const normalizePagePath = require(`../utils/normalize-page-path`) +import { normalizePagePath, denormalizePagePath } from "./normalize-page-path" const telemetry = require(`gatsby-telemetry`) const url = require(`url`) const { createHash } = require(`crypto`) @@ -16,19 +16,6 @@ type QueryResult = { type QueryResultsMap = Map -const denormalize = path => { - if (path === undefined) { - return path - } - if (path === `/`) { - return `/` - } - if (path.charAt(path.length - 1) !== `/`) { - return path + `/` - } - return path -} - /** * Get cached page query result for given page path. * @param {string} pagePath Path to a page. @@ -40,7 +27,7 @@ const getCachedPageData = async ( ): QueryResult => { const { program, pages } = store.getState() const publicDir = path.join(program.directory, `public`) - if (pages.has(denormalize(pagePath)) || pages.has(pagePath)) { + if (pages.has(denormalizePagePath(pagePath)) || pages.has(pagePath)) { try { const pageData = await pageDataUtil.read({ publicDir }, pagePath)