Skip to content

Commit

Permalink
feat: add system logging for Deno config file (#259)
Browse files Browse the repository at this point in the history
* feat: add system logging for Deno config file

* chore: update test
  • Loading branch information
eduardoboucas committed Dec 14, 2022
1 parent 7a37649 commit 27a628c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const bundle = async (

if (featureFlags.edge_functions_read_deno_config) {
// Look for a Deno config file and read it if one exists.
const denoConfig = await getDenoConfig(basePath)
const denoConfig = await getDenoConfig(logger, basePath)

// If the Deno config file defines an import map, read the file and add the
// imports to the global import map.
Expand Down
14 changes: 8 additions & 6 deletions node/deno_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { join } from 'path'
import tmp from 'tmp-promise'
import { expect, test } from 'vitest'

import { testLogger } from '../test/util.js'

import { getConfig } from './deno_config.js'

test('Returns `undefined` if no config file is found', async () => {
const { cleanup, path } = await tmp.dir({ unsafeCleanup: true })
const config = await getConfig(path)
const config = await getConfig(testLogger, path)

expect(config).toBeUndefined()

Expand All @@ -21,7 +23,7 @@ test('Returns an empty object if the config file cannot be parsed', async () =>

await fs.writeFile(configPath, '{')

const config = await getConfig(path)
const config = await getConfig(testLogger, path)

expect(config).toEqual({})

Expand All @@ -35,7 +37,7 @@ test('Throws a type error if the `importMap` contains anything other than a stri

await fs.writeFile(configPath, data)

await expect(getConfig(path)).rejects.toThrowError(TypeError)
await expect(getConfig(testLogger, path)).rejects.toThrowError(TypeError)

await cleanup()
})
Expand Down Expand Up @@ -84,7 +86,7 @@ test('Excludes unsupported properties', async () => {

await fs.writeFile(configPath, data)

const config = await getConfig(path)
const config = await getConfig(testLogger, path)

expect(Object.keys(config ?? {})).toEqual(['importMap'])

Expand All @@ -98,7 +100,7 @@ test('Resolves `importMap` into an absolute path', async () => {

await fs.writeFile(configPath, data)

const config = await getConfig(path)
const config = await getConfig(testLogger, path)

expect(config).toEqual({ importMap: join(path, 'import_map.json') })

Expand All @@ -112,7 +114,7 @@ test('Supports JSONC', async () => {

await fs.writeFile(configPath, `// This is a comment\n${data}`)

const config = await getConfig(path)
const config = await getConfig(testLogger, path)

expect(config).toEqual({ importMap: join(path, 'import_map.json') })

Expand Down
9 changes: 8 additions & 1 deletion node/deno_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join, resolve } from 'path'

import { parse as parseJSONC } from 'jsonc-parser'

import { Logger } from './logger.js'
import { isNodeError } from './utils/error.js'

interface DenoConfigFile {
Expand All @@ -11,8 +12,10 @@ interface DenoConfigFile {

const filenames = ['deno.json', 'deno.jsonc']

export const getConfig = async (basePath?: string) => {
export const getConfig = async (logger: Logger, basePath?: string) => {
if (basePath === undefined) {
logger.system('No base path specified, will not attempt to read Deno config')

return
}

Expand All @@ -21,9 +24,13 @@ export const getConfig = async (basePath?: string) => {
const config = await getConfigFromFile(candidatePath)

if (config !== undefined) {
logger.system('Loaded Deno config file from path', candidatePath)

return normalizeConfig(config, basePath)
}
}

logger.system('No Deno config file found at base path', basePath)
}

const getConfigFromFile = async (filePath: string) => {
Expand Down
2 changes: 1 addition & 1 deletion node/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const serve = async ({
const importMap = new ImportMap(importMaps)

// Look for a Deno config file and read it if one exists.
const denoConfig = await getDenoConfig(basePath)
const denoConfig = await getDenoConfig(logger, basePath)

// If the Deno config file defines an import map, read the file and add the
// imports to the global import map.
Expand Down

0 comments on commit 27a628c

Please sign in to comment.