From 6d63b320751e175de0fb87ea84b4fa1d0ccf4dce Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 16 Jan 2020 10:48:07 +0000 Subject: [PATCH] chore(gatsby): Convert cache.js to TypeScript (#20626) --- jest.config.js | 3 +- package.json | 1 + .../utils/__tests__/{cache.js => cache.ts} | 41 ++++++--- packages/gatsby/src/utils/cache.js | 58 ------------- packages/gatsby/src/utils/cache.ts | 83 +++++++++++++++++++ packages/gatsby/src/utils/get-cache.js | 4 +- tsconfig.json | 2 + yarn.lock | 5 ++ 8 files changed, 123 insertions(+), 74 deletions(-) rename packages/gatsby/src/utils/__tests__/{cache.js => cache.ts} (71%) delete mode 100644 packages/gatsby/src/utils/cache.js create mode 100644 packages/gatsby/src/utils/cache.ts diff --git a/jest.config.js b/jest.config.js index 8012a0a5395c9..186e9d3fc6ab8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -34,8 +34,7 @@ module.exports = { `__tests__/fixtures`, ], transform: { - "^.+\\.js$": `/jest-transformer.js`, - "^.+\\.tsx?$": `/jest-transformer.js`, + "^.+\\.[jt]sx?$": `/jest-transformer.js`, }, moduleNameMapper: { "^highlight.js$": `/node_modules/highlight.js/lib/index.js`, diff --git a/package.json b/package.json index 3185030a9f8dc..e4f31b8877f09 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "@babel/plugin-transform-typescript": "^7.7.4", "@babel/runtime": "^7.7.7", "@lerna/prompt": "3.18.5", + "@types/cache-manager": "^2.10.1", "@types/express": "^4.17.2", "@types/fs-extra": "^8.0.1", "@types/got": "^9.6.9", diff --git a/packages/gatsby/src/utils/__tests__/cache.js b/packages/gatsby/src/utils/__tests__/cache.ts similarity index 71% rename from packages/gatsby/src/utils/__tests__/cache.js rename to packages/gatsby/src/utils/__tests__/cache.ts index 85e7c07e18599..586528ad9bcef 100644 --- a/packages/gatsby/src/utils/__tests__/cache.js +++ b/packages/gatsby/src/utils/__tests__/cache.ts @@ -1,3 +1,7 @@ +import Cache from "../cache" +import fs from "fs-extra" +import manager from "cache-manager" + const mockErrorValue = jest.fn() const mockResultValue = jest.fn() @@ -6,10 +10,10 @@ jest.mock(`cache-manager`, () => { caching: jest.fn(), multiCaching: jest.fn(() => { return { - get: jest.fn((key, callback) => { + get: jest.fn((_key, callback) => { callback(mockErrorValue(), mockResultValue()) }), - set: jest.fn((key, value, args, callback) => { + set: jest.fn((_key, _value, _args, callback) => { callback(mockErrorValue()) }), } @@ -21,16 +25,14 @@ jest.mock(`fs-extra`, () => { ensureDirSync: jest.fn(), } }) -const Cache = require(`../cache`) -const fs = require(`fs-extra`) -const manager = require(`cache-manager`) beforeEach(() => { - manager.caching.mockReset() - fs.ensureDirSync.mockReset() + ;(manager.caching as jest.Mock).mockReset() + ;(fs.ensureDirSync as jest.Mock).mockReset() }) -const getCache = (options = { name: `__test__` }) => new Cache(options).init() +const getCache = (options = { name: `__test__` }): Cache => + new Cache(options).init() describe(`cache`, () => { it(`it can be instantiated`, () => { @@ -40,6 +42,8 @@ describe(`cache`, () => { it(`it can swap out cache store`, () => { const store = { custom: true, + get: jest.fn(), + set: jest.fn(), } new Cache({ @@ -96,7 +100,7 @@ describe(`cache`, () => { it(`both are promises`, () => { const cache = getCache() - const containsThenMethod = result => + const containsThenMethod = (result): void => expect(result).toEqual( expect.objectContaining({ then: expect.any(Function) }) ) @@ -104,6 +108,20 @@ describe(`cache`, () => { containsThenMethod(cache.get(`a`)) containsThenMethod(cache.set(`a`, `b`)) }) + + it(`throws if set is called without initting`, () => { + const cache = new Cache({ name: `__test__` }) + return expect(cache.set(`a`, `b`)).rejects.toThrowError( + `Cache wasn't initialised yet, please run the init method first` + ) + }) + + it(`throws if get is called without initting`, () => { + const cache = new Cache({ name: `__test__` }) + return expect(cache.get(`a`)).rejects.toThrowError( + `Cache wasn't initialised yet, please run the init method first` + ) + }) }) describe(`set`, () => { @@ -125,10 +143,9 @@ describe(`cache`, () => { describe(`get`, () => { it(`resolves to the found value`, () => { const cache = getCache() - mockResultValue.mockReturnValueOnce(`result`) - return expect(cache.get()).resolves.toBe(`result`) + return expect(cache.get(``)).resolves.toBe(`result`) }) it(`resolves to undefined on caching error`, () => { @@ -136,7 +153,7 @@ describe(`cache`, () => { mockErrorValue.mockReturnValueOnce(true) - return expect(cache.get()).resolves.toBeUndefined() + return expect(cache.get(``)).resolves.toBeUndefined() }) }) }) diff --git a/packages/gatsby/src/utils/cache.js b/packages/gatsby/src/utils/cache.js deleted file mode 100644 index 7dbac24859a62..0000000000000 --- a/packages/gatsby/src/utils/cache.js +++ /dev/null @@ -1,58 +0,0 @@ -const fs = require(`fs-extra`) -const manager = require(`cache-manager`) -const fsStore = require(`cache-manager-fs-hash`) -const path = require(`path`) - -const MAX_CACHE_SIZE = 250 -const TTL = Number.MAX_SAFE_INTEGER - -class Cache { - constructor({ name = `db`, store = fsStore } = {}) { - this.name = name - this.store = store - } - - get directory() { - return path.join(process.cwd(), `.cache/caches/${this.name}`) - } - - init() { - fs.ensureDirSync(this.directory) - - const caches = [ - { - store: `memory`, - max: MAX_CACHE_SIZE, - }, - { - store: this.store, - options: { - path: this.directory, - ttl: TTL, - }, - }, - ].map(cache => manager.caching(cache)) - - this.cache = manager.multiCaching(caches) - - return this - } - - get(key) { - return new Promise(resolve => { - this.cache.get(key, (err, res) => { - resolve(err ? undefined : res) - }) - }) - } - - set(key, value, args = {}) { - return new Promise(resolve => { - this.cache.set(key, value, args, err => { - resolve(err ? undefined : value) - }) - }) - } -} - -module.exports = Cache diff --git a/packages/gatsby/src/utils/cache.ts b/packages/gatsby/src/utils/cache.ts new file mode 100644 index 0000000000000..3bd5cf62b3ed9 --- /dev/null +++ b/packages/gatsby/src/utils/cache.ts @@ -0,0 +1,83 @@ +import manager, { Store, StoreConfig, CachingConfig } from "cache-manager" +import fs from "fs-extra" +import fsStore from "cache-manager-fs-hash" +import path from "path" + +const MAX_CACHE_SIZE = 250 +const TTL = Number.MAX_SAFE_INTEGER + +interface ICacheProperties { + name?: string + store?: Store +} + +export default class Cache { + public name: string + public store: Store + public cache?: manager.Cache + + constructor({ name = `db`, store = fsStore }: ICacheProperties = {}) { + this.name = name + this.store = store + } + + get directory(): string { + return path.join(process.cwd(), `.cache/caches/${this.name}`) + } + + init(): Cache { + fs.ensureDirSync(this.directory) + + const configs: StoreConfig[] = [ + { + store: `memory`, + max: MAX_CACHE_SIZE, + ttl: TTL, + }, + { + store: this.store, + ttl: TTL, + options: { + path: this.directory, + ttl: TTL, + }, + }, + ] + + const caches = configs.map(cache => manager.caching(cache)) + + this.cache = manager.multiCaching(caches) + + return this + } + + get(key): Promise { + return new Promise(resolve => { + if (!this.cache) { + throw new Error( + `Cache wasn't initialised yet, please run the init method first` + ) + } + this.cache.get(key, (err, res) => { + resolve(err ? undefined : res) + }) + }) + } + + set( + key: string, + value: T, + args: CachingConfig = { ttl: TTL } + ): Promise { + return new Promise(resolve => { + if (!this.cache) { + throw new Error( + `Cache wasn't initialised yet, please run the init method first` + ) + } + this.cache.set(key, value, args, err => { + resolve(err ? undefined : value) + }) + }) + } +} diff --git a/packages/gatsby/src/utils/get-cache.js b/packages/gatsby/src/utils/get-cache.js index af5db0dbf9c39..d27bcb721547c 100644 --- a/packages/gatsby/src/utils/get-cache.js +++ b/packages/gatsby/src/utils/get-cache.js @@ -1,6 +1,6 @@ -const Cache = require(`./cache`) +const Cache = require(`./cache`).default -let caches = new Map() +const caches = new Map() module.exports = function getCache(name) { let cache = caches.get(name) diff --git a/tsconfig.json b/tsconfig.json index 1828e6d49f73b..0b0873225b647 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,7 @@ { "compilerOptions": { + "target": "ESNext", + "moduleResolution": "node", "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, diff --git a/yarn.lock b/yarn.lock index c92466dc3de48..1eb45d0467706 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3790,6 +3790,11 @@ "@types/connect" "*" "@types/node" "*" +"@types/cache-manager@^2.10.1": + version "2.10.1" + resolved "https://registry.yarnpkg.com/@types/cache-manager/-/cache-manager-2.10.1.tgz#c7bc354be7988659e139e10fc7bde4b221f7f128" + integrity sha512-oJhVIOeC8dX9RZ7OtEZvZ/6cHF5aQWoRcuJ7KwK3Xb69hIIdElpWzfJ35fOXvYOgoyRXA34jFrD8lqEjDWz37w== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"