-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite package on typescript #23
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
module.exports = { | ||
extends: 'gemini-testing', | ||
extends: ['gemini-testing', 'plugin:@typescript-eslint/recommended'], | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['@typescript-eslint'], | ||
root: true | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
build |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ node_js: | |
- '6' | ||
- '8' | ||
script: | ||
- npm build | ||
- npm test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А здесь у тебя за счет |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ Config is described with a combination of a functions: | |
var parser = root(section({ | ||
system: section({ | ||
parallelLimit: option({ | ||
defaultValue: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
parseEnv: Number, | ||
parseCli: Number, | ||
validate: function() {...} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
class MissingOptionError extends Error { | ||
constructor(optionName) { | ||
export class MissingOptionError extends Error { | ||
optionName: string; | ||
|
||
constructor(optionName: string) { | ||
const message = `${optionName} is required`; | ||
super(message); | ||
this.name = 'MissingOptionError'; | ||
|
@@ -10,8 +12,10 @@ class MissingOptionError extends Error { | |
} | ||
} | ||
|
||
class UnknownKeysError extends Error { | ||
constructor(keys) { | ||
export class UnknownKeysError extends Error { | ||
keys: Array<string>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А это зачем? |
||
|
||
constructor(keys: Array<string>) { | ||
const message = `Unknown options: ${keys.join(', ')}`; | ||
super(message); | ||
this.name = 'UnknownKeysError'; | ||
|
@@ -21,5 +25,3 @@ class UnknownKeysError extends Error { | |
Error.captureStackTrace(this, UnknownKeysError); | ||
} | ||
} | ||
|
||
module.exports = {MissingOptionError, UnknownKeysError}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {root, section, map, option} from './core'; | ||
export {MissingOptionError, UnknownKeysError} from './errors'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import _ from 'lodash'; | ||
|
||
import type {LazyObject} from './types/lazy'; | ||
|
||
export const isLazy = Symbol('isLazy'); | ||
|
||
type SimpleOrLazyObject<T> = T | LazyObject<T>; | ||
|
||
export function buildLazyObject<T>(keys: Array<keyof T>, getKeyGetter: (key: keyof T) => () => (SimpleOrLazyObject<T[keyof T]>)): LazyObject<T> { | ||
const target = { | ||
[isLazy]: true | ||
} as LazyObject<T>; | ||
|
||
for (const key of keys) { | ||
defineLazy(target, key, getKeyGetter(key)); | ||
} | ||
|
||
return target; | ||
} | ||
|
||
export function forceParsing<T>(lazyObject: LazyObject<T>): T { | ||
return _.cloneDeep(lazyObject); | ||
} | ||
|
||
function defineLazy<T>(object: LazyObject<T>, key: keyof T, getter: () => SimpleOrLazyObject<T[keyof T]>): void { | ||
let defined = false; | ||
let value: T[keyof T]; | ||
|
||
Object.defineProperty(object, key, { | ||
get(): T[keyof T] { | ||
if (!defined) { | ||
defined = true; | ||
const val = getter(); | ||
|
||
value = isLazyObject(val) ? forceParsing(val) : val; | ||
} | ||
|
||
return value; | ||
}, | ||
enumerable: true | ||
}); | ||
} | ||
|
||
function isLazyObject<T>(value: T): value is LazyObject<T> { | ||
return _.isObject(value) && hasOwnProperty(value, isLazy) && value[isLazy] === true; | ||
} | ||
|
||
function hasOwnProperty<T extends object, K extends PropertyKey>(obj: T, prop: K): obj is T & Record<K, unknown> { | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type {LazyObject} from './lazy'; | ||
import type {MapParser} from './map'; | ||
import type {OptionParser} from './option'; | ||
import type {SectionParser} from './section'; | ||
|
||
export type ParsedConfig<Config> = {[Key in keyof Config]: LazyObject<Config[Key]>}; | ||
|
||
export type Parser<Config, Result> = OptionParser<Config, Result> | SectionParser<Config, Result> | MapParser<Config, Result>; | ||
|
||
export interface Rooted<T> { | ||
root: T; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type {isLazy} from '../lazy'; | ||
|
||
export type LazyObject<T> = T & { | ||
[isLazy]: true; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type {RootPrefixes, ConfigParserArg} from './root'; | ||
|
||
export type LocatorArg<Config> = RootPrefixes & ConfigParserArg<Config>; | ||
|
||
export type Prefixes = Required<RootPrefixes> & { | ||
namePrefix: string; | ||
}; | ||
|
||
export interface Node<Options> { | ||
name: string; | ||
parent: string | null; | ||
option: Options; | ||
envVar?: string; | ||
cliOption?: string; | ||
} | ||
|
||
export interface Locator<Options> extends Node<Options> { | ||
nested<Key extends keyof Options>(key: Key): Locator<Options[Key]>; | ||
resetOption(newOption: Options): Locator<Options>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как-то не вяжется, что аргумент называется |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type {Rooted} from './common'; | ||
import type {LazyObject} from './lazy'; | ||
import type {Locator} from './locator'; | ||
|
||
export interface MapParser<Config, Result> { | ||
(locator: Locator<Config>, config: Rooted<Result>): LazyObject<Config>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Немного странная последовательность опций. Я бы в такой последовательности описывал - https://github.com/gemini-testing/hermione-storybook/blob/master/.eslintrc.js