Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Convert cache.js to TypeScript #20626

Merged
merged 9 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jest-transformer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
const babelPreset = require(`babel-preset-gatsby-package`)()
module.exports = require(`babel-jest`).createTransformer(babelPreset)
module.exports = require(`babel-jest`).createTransformer({
...babelPreset,
overrides: [
...(babelPreset.overrides || []),
{
test: `**/*.ts`,
plugins: [[`@babel/plugin-transform-typescript`, { isTSX: true }]],
},
],
})
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ module.exports = {
`__tests__/fixtures`,
],
transform: {
"^.+\\.js$": `<rootDir>/jest-transformer.js`,
"^.+\\.tsx?$": `<rootDir>/node_modules/ts-jest/preprocessor.js`,
"^.+\\.[jt]sx?$": `<rootDir>/jest-transformer.js`,
},
moduleNameMapper: {
"^highlight.js$": `<rootDir>/node_modules/highlight.js/lib/index.js`,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"@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",
"@types/jest": "^24.0.23",
"@types/node": "^12.12.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/__tests__/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jest.mock(`fs-extra`, () => {
ensureDirSync: jest.fn(),
}
})
const Cache = require(`../cache`)
const Cache = require(`../cache`).default
const fs = require(`fs-extra`)
const manager = require(`cache-manager`)

Expand Down
58 changes: 0 additions & 58 deletions packages/gatsby/src/utils/cache.js

This file was deleted.

75 changes: 75 additions & 0 deletions packages/gatsby/src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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<T = unknown>(key): Promise<T | undefined> {
return new Promise(resolve => {
// eslint-disable-next-line no-unused-expressions
this.cache?.get<T>(key, (err, res) => {
resolve(err ? undefined : res)
})
})
}

set<T>(
pvdz marked this conversation as resolved.
Show resolved Hide resolved
key: string,
value: T,
args: CachingConfig = { ttl: TTL }
): Promise<T | undefined> {
return new Promise(resolve => {
// eslint-disable-next-line no-unused-expressions
pvdz marked this conversation as resolved.
Show resolved Hide resolved
this.cache?.set(key, value, args, err => {
resolve(err ? undefined : value)
})
})
}
}
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/get-cache.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ES2015"],
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,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"
Expand Down Expand Up @@ -3839,6 +3844,13 @@
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/fs-extra@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.0.1.tgz#a2378d6e7e8afea1564e44aafa2e207dadf77686"
integrity sha512-J00cVDALmi/hJOYsunyT52Hva5TnJeKP5yd1r+mH/ZU0mbYZflR0Z5kw5kITtKTRYMhm1JMClOFYdHnQszEvqw==
dependencies:
"@types/node" "*"

"@types/get-port@^0.0.4":
version "0.0.4"
resolved "https://registry.yarnpkg.com/@types/get-port/-/get-port-0.0.4.tgz#eb6bb7423d9f888b632660dc7d2fd3e69a35643e"
Expand Down