Skip to content

Commit

Permalink
Update to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jan 15, 2020
1 parent 9c7e572 commit f668fc5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 22 deletions.
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
49 changes: 33 additions & 16 deletions packages/gatsby/src/utils/cache.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,75 @@
const fs = require(`fs-extra`)
const manager = require(`cache-manager`)
const fsStore = require(`cache-manager-fs-hash`)
const path = require(`path`)
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

class Cache {
constructor({ name = `db`, store = fsStore } = {}) {
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() {
get directory(): string {
return path.join(process.cwd(), `.cache/caches/${this.name}`)
}

init() {
init(): Cache {
fs.ensureDirSync(this.directory)

const caches = [
const configs: StoreConfig[] = [
{
store: `memory`,
max: MAX_CACHE_SIZE,
ttl: TTL,
},
{
store: this.store,
ttl: TTL,
options: {
path: this.directory,
ttl: TTL,
},
},
].map(cache => manager.caching(cache))
]

const caches = configs.map(cache => manager.caching(cache))

this.cache = manager.multiCaching(caches)

return this
}

get(key) {
get<T = unknown>(key): Promise<T | undefined> {
return new Promise(resolve => {
this.cache.get(key, (err, res) => {
// eslint-disable-next-line no-unused-expressions
this.cache?.get<T>(key, (err, res) => {
resolve(err ? undefined : res)
})
})
}

set(key, value, args = {}) {
set<T>(
key: string,
value: T,
args: CachingConfig = { ttl: TTL }
): Promise<T | undefined> {
return new Promise(resolve => {
this.cache.set(key, value, args, err => {
// eslint-disable-next-line no-unused-expressions
this.cache?.set(key, value, args, err => {
resolve(err ? undefined : value)
})
})
}
}

module.exports = Cache
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

0 comments on commit f668fc5

Please sign in to comment.