1+ import { loadConfig } from 'c12' ;
12import { LexerKey , LexerResult , NvtonOptions } from './types' ;
2- import { writeFile } from './utils' ;
3+ import { isBrowser , writeFile } from './utils' ;
4+ import { DEFAULT_CONFIG } from './constants' ;
35
46export class NVTON {
57 // TODO: unknown deep type
68 private data : Map < LexerKey , unknown > ;
7- private options : NvtonOptions ;
9+ private options : NvtonOptions = DEFAULT_CONFIG ;
810
9- constructor ( lexeme : LexerResult ) {
11+ private async loadDefaultConfig ( options ?: NvtonOptions ) {
12+ try {
13+ if ( ! isBrowser ) {
14+ // TODO: awaitable load fix config
15+ const { config } = await loadConfig ( {
16+ name : 'nvton' ,
17+ rcFile : false ,
18+ envName : false ,
19+ } ) ;
20+
21+ this . options = options ?? config ?? DEFAULT_CONFIG ;
22+ } else {
23+ this . options = options ?? DEFAULT_CONFIG ;
24+ }
25+ } catch ( e ) {
26+ this . options = options ?? DEFAULT_CONFIG ;
27+ }
28+ }
29+
30+ constructor ( lexeme : LexerResult , options ?: NvtonOptions ) {
1031 this . data = new Map ( ) ;
11- this . options = { } ;
32+ this . loadDefaultConfig ( options ) ;
1233 this . set ( lexeme ) ;
1334 }
1435
@@ -28,6 +49,10 @@ export class NVTON {
2849 }
2950
3051 public write ( path : string ) {
52+ if ( isBrowser ) {
53+ throw new Error ( `Browser setups don't support write function!` ) ;
54+ }
55+
3156 const filepath = path . endsWith ( '.nvton' ) ? path : `${ path } .nvton` ;
3257 // TODO: format .nvton file
3358 writeFile ( filepath , '' ) ;
0 commit comments