Skip to content

Commit

Permalink
refactor: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Nov 16, 2020
1 parent be01b0a commit 91daeb5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compile, compileFromFile, Options } from 'json-schema-to-typescript'
import { flow } from 'lodash'
import { JsonObject } from 'type-fest'

/**
* Options of json-schema-to-typescript.
Expand Down Expand Up @@ -48,9 +49,9 @@ function postProcess(source: string) {
* Generate types from a JSON schema object.
*/
export async function generateTypes(
schema: any,
schema: JsonObject,
options: Partial<Options> = {}
) {
): Promise<string> {
const mergedOptions = { ...defaultOptions, ...options }
return await compile(schema, '', mergedOptions).then(postProcess)
}
Expand All @@ -61,7 +62,7 @@ export async function generateTypes(
export async function generateTypesFromFile(
schemaPath: string,
options: Partial<Options> = {}
) {
): Promise<string> {
const mergedOptions = { ...defaultOptions, ...options }
return await compileFromFile(schemaPath, mergedOptions).then(postProcess)
}
3 changes: 2 additions & 1 deletion src/loaders/loadArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonObject } from 'type-fest'
import { loadFromArgs } from './loadArgs'

const TEST_CASES: [string, string[], {}][] = [
const TEST_CASES: [string, string[], JsonObject][] = [
['camelCase a flag', ['--foo-bar'], { fooBar: true }],
['accept a value as option', ['--foo=bar'], { foo: 'bar' }],
['accept a value as flags', ['--foo', 'bar'], { foo: 'bar' }],
Expand Down
16 changes: 9 additions & 7 deletions src/loaders/loadArgs.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { camelCase, chain } from 'lodash'
import getopts from 'getopts'
import { camelCase, chain } from 'lodash'
import { parseArray } from './utils'
import { JsonObject } from 'type-fest'

/**
* Loads configuration from command line arguments.
*
* Arguments name describe a path to the desired options using `-` as
* separator.
*
* @see https://github.com/jorgebucaran/getopts
* @example
*
* --server-port=1337
* --directives="['@graph0/directives', './directives']"
* --directives=@graph0/directives --directives=./directives
* ```
* $ --cooking-time=200
* $ --types="['Fettuccine', 'Tagliatelle']"
* $ --directives=Fettuccine --directives=Tagliatelle
* ```
* @see https://github.com/jorgebucaran/getopts
*/
export function loadFromArgs(args: string[]): {} {
export function loadFromArgs(args: string[]): JsonObject {
return chain(getopts(args))
.omit('_')
.mapKeys((_v, k) => camelCase(k))
Expand Down
11 changes: 6 additions & 5 deletions src/loaders/loadEnv.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { JsonObject } from 'type-fest'
import { loadFromEnv } from './loadEnv'

const TEST_CASES: [string, NodeJS.ProcessEnv, {}][] = [
const TEST_CASES: [string, NodeJS.ProcessEnv, JsonObject][] = [
[
'map name to camelcase without namespace',
{ FAUDA_FOO: 'bar' },
{ PASTA_FOO: 'bar' },
{ foo: 'bar' }
],
[
'parse an array value',
{ FAUDA_FOO: '["bar", 1337]' },
{ PASTA_FOO: '["bar", 1337]' },
{ foo: ['bar', 1337] }
],
['trim the value', { FAUDA_FOO: ' 1337 ' }, { foo: '1337' }],
['trim the value', { PASTA_FOO: ' 1337 ' }, { foo: '1337' }],
['remove variables without namespace', { FOO: '1337' }, {}]
]

test.each(TEST_CASES)('%s', (_title, env, expected) => {
expect(loadFromEnv(env, 'fauda')).toEqual(expected)
expect(loadFromEnv('pasta', env)).toEqual(expected)
})
4 changes: 2 additions & 2 deletions src/loaders/loadEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { parseArray } from './utils'
*
* @example
*
* GRAPH0_SERVER_PORT=1337
* GRAPH0_DIRECTIVES="['@graph0/directives', './directives']"
* PASTA_COOKING_TIME=200
* PASTA_TYPES="['Fettuccine', 'Tagliatelle']"
*/
export function loadFromEnv(
namespace: string,
Expand Down
5 changes: 3 additions & 2 deletions src/loaders/loadFile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { cosmiconfig, Options as CosmiconfigOptions } from 'cosmiconfig'
import loadTs from '@endemolshinegroup/cosmiconfig-typescript-loader'
import { cosmiconfig, Options as CosmiconfigOptions } from 'cosmiconfig'
import { JsonObject } from 'type-fest'

/**
* @internal
*/
export function getDefaultSearchPlaces(namespace: string) {
export function getDefaultSearchPlaces(namespace: string): string[] {
return [
'package.json',
`.${namespace}rc`,
Expand Down
3 changes: 2 additions & 1 deletion src/normalizer/expandVars.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonObject } from 'type-fest'
import { expandVars } from './expandVars'

const TEST_CASES: [string, NodeJS.ProcessEnv, {}, {}][] = [
const TEST_CASES: [string, NodeJS.ProcessEnv, JsonObject, JsonObject][] = [
[
'expand an existing env var to its value',
{ BAR: 'bar' },
Expand Down
7 changes: 4 additions & 3 deletions src/normalizer/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ test('throw an error for invalid values', async () => {
await expect(async () =>
validate({ cookingTime: 'nope', seasoning: {} }, await getSchema())
).rejects.toThrowErrorMatchingInlineSnapshot(`
".cookingTime should be number
.seasoning should be array"
`)
"validate: Validation failed
.cookingTime should be number
.seasoning should be array"
`)
})
13 changes: 8 additions & 5 deletions src/normalizer/validate.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import Ajv from 'ajv'
import { chain } from 'lodash'
import { JsonObject } from 'type-fest'

function createError(errors: Ajv.ErrorObject[]): Error {
const message = chain(errors)
.map(err => `${err.dataPath} ${err.message}`)
.join('\n')
.value()
return new Error(message)
return new Error(`validate: Validation failed\n${message}`)
}

/**
* @see https://github.com/ajv-validator/ajv
*/
export function validate<Configuration>(
input: any,
schema: any
input: JsonObject,
schema: JsonObject
): Configuration {
const ajv = new Ajv({
allErrors: true,
Expand All @@ -28,8 +29,10 @@ export function validate<Configuration>(
const isValid = ajv.validate(schema, output)

if (!isValid) {
throw createError(ajv.errors!)
throw ajv.errors
? createError(ajv.errors)
: new Error('validate: Unknown error')
}

return output
return (output as unknown) as Configuration
}
4 changes: 2 additions & 2 deletions test/utils/testProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class TestProject {
this.rootDir = path.join(FIXTURES_ROOT, snakeCase(variant))
}

async setup() {
async setup(): Promise<void> {
const { rootDir, variant } = this
const variantDir = path.dirname(variant)
const ext = path.extname(variant) || '.json'
Expand All @@ -29,7 +29,7 @@ export class TestProject {
} catch (e) {}
}

async teardown() {
async teardown(): Promise<void> {
const { rootDir } = this
await fs.rmdir(path.join(rootDir), { recursive: true })
}
Expand Down

0 comments on commit 91daeb5

Please sign in to comment.