-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refresh types on Deno CLI cache (#66)
* feat: refresh types on Deno CLI cache * fix: use cache command * chore: move rules to eslintrc * chore: remove eslint comments Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2c57aaa
commit 534ea80
Showing
6 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { promises as fs } from 'fs' | ||
import { join } from 'path' | ||
|
||
import fetch from 'node-fetch' | ||
|
||
import type { DenoBridge } from './bridge.js' | ||
|
||
const TYPES_URL = 'https://edge.netlify.com' | ||
|
||
const ensureLatestTypes = async (deno: DenoBridge, customTypesURL?: string) => { | ||
const typesURL = customTypesURL ?? TYPES_URL | ||
|
||
let [localVersion, remoteVersion] = [await getLocalVersion(deno), ''] | ||
|
||
try { | ||
remoteVersion = await getRemoteVersion(typesURL) | ||
} catch (error) { | ||
deno.log('Could not check latest version of types:', error) | ||
|
||
return | ||
} | ||
|
||
if (localVersion === remoteVersion) { | ||
deno.log('Local version of types is up-to-date:', localVersion) | ||
|
||
return | ||
} | ||
|
||
deno.log('Local version of types is outdated, updating:', localVersion) | ||
|
||
try { | ||
await deno.run(['cache', '-r', typesURL]) | ||
} catch (error) { | ||
deno.log('Could not download latest types:', error) | ||
|
||
return | ||
} | ||
|
||
try { | ||
await writeVersionFile(deno, remoteVersion) | ||
} catch { | ||
// no-op | ||
} | ||
} | ||
|
||
const getLocalVersion = async (deno: DenoBridge) => { | ||
const versionFilePath = join(deno.cacheDirectory, 'types-version.txt') | ||
|
||
try { | ||
const version = await fs.readFile(versionFilePath, 'utf8') | ||
|
||
return version | ||
} catch { | ||
// no-op | ||
} | ||
} | ||
|
||
const getRemoteVersion = async (typesURL: string) => { | ||
const versionURL = new URL('/version.txt', typesURL) | ||
const res = await fetch(versionURL.toString()) | ||
|
||
if (res.status !== 200) { | ||
throw new Error('Unexpected status code from version endpoint') | ||
} | ||
|
||
const version = await res.text() | ||
|
||
return version | ||
} | ||
|
||
const writeVersionFile = async (deno: DenoBridge, version: string) => { | ||
const versionFilePath = join(deno.cacheDirectory, 'types-version.txt') | ||
|
||
await fs.writeFile(versionFilePath, version) | ||
} | ||
|
||
export { ensureLatestTypes } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { promises as fs } from 'fs' | ||
import { join } from 'path' | ||
|
||
import test from 'ava' | ||
import nock from 'nock' | ||
import { stub } from 'sinon' | ||
import tmp from 'tmp-promise' | ||
|
||
import { DenoBridge } from '../src/bridge.js' | ||
import { ensureLatestTypes } from '../src/types.js' | ||
|
||
test('`ensureLatestTypes` updates the Deno CLI cache if the local version of types is outdated', async (t) => { | ||
const mockURL = 'https://edge.netlify' | ||
const mockVersion = '123456789' | ||
const latestVersionMock = nock(mockURL).get('/version.txt').reply(200, mockVersion) | ||
|
||
const tmpDir = await tmp.dir() | ||
const deno = new DenoBridge({ | ||
cacheDirectory: tmpDir.path, | ||
}) | ||
|
||
const mock = stub(deno, 'run').resolves() | ||
|
||
await ensureLatestTypes(deno, mockURL) | ||
|
||
const versionFile = await fs.readFile(join(tmpDir.path, 'types-version.txt'), 'utf8') | ||
|
||
t.true(latestVersionMock.isDone()) | ||
t.is(mock.callCount, 1) | ||
t.deepEqual(mock.firstCall.firstArg, ['cache', '-r', mockURL]) | ||
t.is(versionFile, mockVersion) | ||
|
||
mock.restore() | ||
|
||
await fs.rmdir(tmpDir.path, { recursive: true }) | ||
}) | ||
|
||
test('`ensureLatestTypes` does not update the Deno CLI cache if the local version of types is up-to-date', async (t) => { | ||
const mockURL = 'https://edge.netlify' | ||
const mockVersion = '987654321' | ||
|
||
const tmpDir = await tmp.dir() | ||
const versionFilePath = join(tmpDir.path, 'types-version.txt') | ||
|
||
await fs.writeFile(versionFilePath, mockVersion) | ||
|
||
const latestVersionMock = nock(mockURL).get('/version.txt').reply(200, mockVersion) | ||
const deno = new DenoBridge({ | ||
cacheDirectory: tmpDir.path, | ||
}) | ||
const mock = stub(deno, 'run').resolves() | ||
|
||
await ensureLatestTypes(deno, mockURL) | ||
|
||
t.true(latestVersionMock.isDone()) | ||
t.is(mock.callCount, 0) | ||
|
||
mock.restore() | ||
|
||
await fs.rmdir(tmpDir.path, { recursive: true }) | ||
}) | ||
|
||
test('`ensureLatestTypes` does not throw if the types URL is not available', async (t) => { | ||
const mockURL = 'https://edge.netlify' | ||
const latestVersionMock = nock(mockURL).get('/version.txt').reply(500) | ||
|
||
const tmpDir = await tmp.dir() | ||
const deno = new DenoBridge({ | ||
cacheDirectory: tmpDir.path, | ||
}) | ||
|
||
const mock = stub(deno, 'run').resolves() | ||
|
||
await ensureLatestTypes(deno, mockURL) | ||
|
||
t.true(latestVersionMock.isDone()) | ||
t.is(mock.callCount, 0) | ||
|
||
mock.restore() | ||
|
||
await fs.rmdir(tmpDir.path, { recursive: true }) | ||
}) |