From 27120fdcf2531ea13dab74ffd6e61aa87e774a02 Mon Sep 17 00:00:00 2001 From: Damien Senger Date: Thu, 23 Apr 2020 22:29:55 +0200 Subject: [PATCH] chore(gatsby): Migrate utils/page-data to TypeScript This commit migrates the existing `src/utils/page-data.ts` file to TypeScript by trying to stick as close as possible to the original JavaScript file. To take advantage of existing Redux typings, I tried to extend the typings from the `IGatsbyPage` interface describing page data. --- packages/gatsby/src/utils/page-data.js | 52 ----------------- packages/gatsby/src/utils/page-data.ts | 79 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 52 deletions(-) delete mode 100644 packages/gatsby/src/utils/page-data.js create mode 100644 packages/gatsby/src/utils/page-data.ts diff --git a/packages/gatsby/src/utils/page-data.js b/packages/gatsby/src/utils/page-data.js deleted file mode 100644 index 374bdae1df472..0000000000000 --- a/packages/gatsby/src/utils/page-data.js +++ /dev/null @@ -1,52 +0,0 @@ -const fs = require(`fs-extra`) -const path = require(`path`) -const { store } = require(`../redux`) - -const fixedPagePath = pagePath => (pagePath === `/` ? `index` : pagePath) - -const getFilePath = ({ publicDir }, pagePath) => - path.join(publicDir, `page-data`, fixedPagePath(pagePath), `page-data.json`) - -const read = async ({ publicDir }, pagePath) => { - const filePath = getFilePath({ publicDir }, pagePath) - const rawPageData = await fs.readFile(filePath, `utf-8`) - return JSON.parse(rawPageData) -} - -const remove = async ({ publicDir }, pagePath) => { - const filePath = getFilePath({ publicDir }, pagePath) - if (fs.existsSync(filePath)) { - return await fs.remove(filePath) - } - return Promise.resolve() -} - -const write = async ({ publicDir }, page, result) => { - const filePath = getFilePath({ publicDir }, page.path) - const body = { - componentChunkName: page.componentChunkName, - path: page.path, - matchPath: page.matchPath, - result, - } - const bodyStr = JSON.stringify(body) - // transform asset size to kB (from bytes) to fit 64 bit to numbers - const pageDataSize = Buffer.byteLength(bodyStr) / 1000 - - store.dispatch({ - type: `ADD_PAGE_DATA_STATS`, - payload: { - filePath, - size: pageDataSize, - }, - }) - - await fs.outputFile(filePath, bodyStr) -} - -module.exports = { - read, - write, - remove, - fixedPagePath, -} diff --git a/packages/gatsby/src/utils/page-data.ts b/packages/gatsby/src/utils/page-data.ts new file mode 100644 index 0000000000000..b4294c11846ff --- /dev/null +++ b/packages/gatsby/src/utils/page-data.ts @@ -0,0 +1,79 @@ +import fs from "fs-extra" +import path from "path" +import { IGatsbyPage } from "../redux/types" +import { store } from "../redux" + +interface IPageDataContext { + publicDir: string +} + +interface IPageData + extends Pick { + result: any +} + +export const fixedPagePath = (pagePath: string): string => + pagePath === `/` ? `index` : pagePath + +const getFilePath = ( + { publicDir }: IPageDataContext, + pagePath: string +): string => + path.join(publicDir, `page-data`, fixedPagePath(pagePath), `page-data.json`) + +export const read = async ( + { publicDir }: IPageDataContext, + pagePath: string +): Promise => { + const filePath = getFilePath({ publicDir }, pagePath) + const rawPageData = await fs.readFile(filePath, `utf-8`) + + return JSON.parse(rawPageData) +} + +export const remove = async ( + { publicDir }: IPageDataContext, + pagePath: string +): Promise => { + const filePath = getFilePath({ publicDir }, pagePath) + + if (fs.existsSync(filePath)) { + return await fs.remove(filePath) + } + + return Promise.resolve() +} + +export const write = async ( + { publicDir }: IPageDataContext, + { componentChunkName, matchPath, path }: Exclude, + result: IPageData["result"] +): Promise => { + const filePath = getFilePath({ publicDir }, path) + const body = { + componentChunkName, + path, + matchPath, + result, + } + const bodyStr = JSON.stringify(body) + // transform asset size to kB (from bytes) to fit 64 bit to numbers + const pageDataSize = Buffer.byteLength(bodyStr) / 1000 + + store.dispatch({ + type: `ADD_PAGE_DATA_STATS`, + payload: { + filePath, + size: pageDataSize, + }, + }) + + await fs.outputFile(filePath, bodyStr) +} + +export default { + fixedPagePath, + read, + remove, + write, +}