Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
npm
common
Commits
a7e42939
Commit
a7e42939
authored
Jun 03, 2019
by
Vladislav Lagunov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Исправлен парсинг конфига из командной строки
parent
351874db
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
16 deletions
+7
-16
config-reader/index.ts
+7
-16
No files found.
config-reader/index.ts
View file @
a7e42939
...
...
@@ -4,7 +4,6 @@ import { absurd } from '../types';
import
{
WithDefault
,
RecordDecoder
,
Decoder
}
from
'../decoder'
;
import
{
Either
}
from
'../either'
;
/**
* Источник данных для чтения конфигов
*/
...
...
@@ -14,45 +13,40 @@ export type ConfigSource =
|
{
tag
:
'JSON'
,
value
:
object
}
|
{
tag
:
'Concat'
,
sources
:
ConfigSource
[]
}
export
function
cli
(
args
:
string
[],
prefix
:
string
=
''
):
ConfigSource
{
return
{
tag
:
'Cli'
,
args
,
prefix
};
}
export
function
queryString
(
value
:
string
,
transformKey
=
camelCase
):
ConfigSource
{
return
{
tag
:
'Location/search'
,
value
,
transformKey
};
}
export
function
json
(
value
:
object
):
ConfigSource
{
return
{
tag
:
'JSON'
,
value
};
}
export
function
concat
(...
sources
:
ConfigSource
[]):
ConfigSource
{
return
{
tag
:
'Concat'
,
sources
};
}
export
function
validate
<
A
>
(
config
:
ConfigSource
,
decoder
:
RecordDecoder
<
A
>
)
{
return
decoder
.
validate
(
merge
(
decoder
,
config
));
}
export
function
merge
<
A
>
(
decoder
:
RecordDecoder
<
A
>
,
...
srcs
:
ConfigSource
[]):
object
{
const
source
=
srcs
.
length
===
1
?
srcs
[
0
]
:
concat
(...
srcs
);
debugger
;
if
(
source
.
tag
===
'Cli'
)
{
const
value
:
Record
<
string
,
any
>
=
{};
const
output
:
Record
<
string
,
any
>
=
{};
for
(
let
i
=
0
;
i
<
source
.
args
.
length
;
i
++
)
{
const
prefixLen
=
'--'
.
length
+
source
.
prefix
.
length
;
if
(
!
source
.
args
[
i
].
startsWith
(
'--'
+
source
.
prefix
))
continue
;
const
rest
=
source
.
args
[
i
].
substr
(
prefixLen
);
const
[
k
,
v
]
=
rest
.
indexOf
(
'='
)
===
-
1
?
[
rest
,
source
.
args
[
++
i
]
||
''
]
:
source
.
args
[
i
].
split
(
'='
);
// Проверка того что это известно
if
(
!
decoder
.
_description
.
hasOwnProperty
(
k
))
continue
;
value
[
k
]
=
fromString
(
decoder
.
_description
[
k
],
v
);
const
[
key
,
value
]
=
rest
.
indexOf
(
'='
)
===
-
1
?
[
rest
,
source
.
args
[
++
i
]
||
''
]
:
rest
.
split
(
'='
);
if
(
!
decoder
.
_description
.
hasOwnProperty
(
key
))
continue
;
output
[
key
]
=
fromString
(
decoder
.
_description
[
key
],
value
);
}
return
value
;
return
output
;
}
if
(
source
.
tag
===
'Location/search'
)
{
...
...
@@ -153,7 +147,6 @@ export function fromString(d: Decoder<any>, value: string): unknown {
return
absurd
(
d
);
}
export
class
ConfigDescription
<
A
>
extends
t
.
ToDecoder
<
A
>
{
constructor
(
readonly
_decoder
:
Decoder
<
A
>
,
...
...
@@ -168,7 +161,6 @@ export class ConfigDescription<A> extends t.ToDecoder<A> {
// Информация о cli параметрe
export
type
InfoItem
=
{
type
:
string
;
default
?:
unknown
;
description
?:
string
};
export
function
configInfo
(
decoder
:
RecordDecoder
<
any
>
,
prefix
=
''
):
Record
<
string
,
InfoItem
>
{
return
Object
.
keys
(
decoder
.
_description
).
reduce
((
acc
,
k
)
=>
(
acc
[
k
]
=
go
(
decoder
.
_description
[
k
]),
acc
),
{});
...
...
@@ -247,7 +239,6 @@ export function configInfo(decoder: RecordDecoder<any>, prefix=''): Record<strin
}
}
declare
module
"../decoder"
{
interface
DecoderBase
<
A
>
{
withDescription
(
description
:
string
):
Decoder
<
A
>
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment