Skip to content

Commit

Permalink
refactor(utils): extract loadSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Dec 6, 2020
1 parent edde1e3 commit 37046a5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/fauda.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { promises as fs } from 'fs'
import { isObject, merge, reduceRight } from 'lodash'
import { merge, reduceRight } from 'lodash'
import { JsonObject } from 'type-fest'
import { loadArgs, loadEnv, loadFile } from './loaders'
import { normalize } from './normalizer'
import { FaudaOptions } from './types'
import { loadSchema } from './utils/loadSchema'

function normalizeOptions(options: Partial<FaudaOptions>): FaudaOptions {
const defaultOptions: FaudaOptions = {
Expand All @@ -15,16 +15,6 @@ function normalizeOptions(options: Partial<FaudaOptions>): FaudaOptions {
return { ...defaultOptions, ...options }
}

async function loadSchema(schema: string | JsonObject): Promise<JsonObject> {
try {
return isObject(schema)
? schema
: JSON.parse(await fs.readFile(schema, 'utf8'))
} catch (err) {
throw new Error('load: Error loading schema\n' + err.message)
}
}

async function loadFromAll(
namespace: string,
{ args, cwd, env }: FaudaOptions
Expand Down
55 changes: 55 additions & 0 deletions src/utils/loadSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { loadSchema } from './loadSchema'

test('return the schema when given an object', async () => {
expect(await loadSchema(`test/fixtures/schema.json`)).toMatchInlineSnapshot(`
Object {
"$schema": "http://json-schema.org/draft-07/schema",
"properties": Object {
"$schema": Object {
"description": "Path to my app's schema.",
"type": "string",
},
"mode": Object {
"default": "\${NODE_ENV}",
"description": "Mode of the app.",
"enum": Array [
"development",
"test",
"production",
],
"type": "string",
},
"open": Object {
"default": false,
"description": "Open in a browser tab if true.",
"type": "boolean",
},
"port": Object {
"default": 3000,
"description": "The port the server listens to.",
"type": "number",
},
"publicPages": Object {
"default": Array [
"/hi",
],
"description": "A list of public pages.",
"items": Object {
"type": "string",
},
"type": "array",
},
},
"title": "My awesome app configuration",
"type": "object",
}
`)
})

test('load and return the schema when given a string', async () => {
expect(await loadSchema({ foo: 'bar ' })).toMatchInlineSnapshot(`
Object {
"foo": "bar ",
}
`)
})
15 changes: 15 additions & 0 deletions src/utils/loadSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { promises as fs } from 'fs'
import { isObject } from 'lodash'
import { JsonObject } from 'type-fest'

export async function loadSchema(
schema: string | JsonObject
): Promise<JsonObject> {
try {
return isObject(schema)
? schema
: JSON.parse(await fs.readFile(schema, 'utf8'))
} catch (err) {
throw new Error('load: Error loading schema\n' + err.message)
}
}

0 comments on commit 37046a5

Please sign in to comment.