@@ -15,13 +15,13 @@ async function load<T>({
1515 ENOENTResult,
1616} : {
1717 filePath : string
18- ENOENTResult ? : T
18+ ENOENTResult : T | undefined
1919} ) : Promise < T > {
2020 let data : never
2121 try {
2222 data = await loadJsonFile ( filePath )
2323 } catch ( err ) {
24- if ( typeof err === 'object' && err && 'code' in err ) {
24+ if ( err instanceof Error && 'code' in err ) {
2525 switch ( err . code ) {
2626 case 'JSONError' : {
2727 throw new Error ( `${ filePath } is not valid JSON` )
@@ -44,20 +44,21 @@ async function load<T>({
4444function generateConfig < T extends object > ( configOptions : {
4545 dir : string
4646 filename : string
47+ ENOENTResult : T | undefined
4748} ) : Config < T > {
4849 const filename = configOptions . filename
4950 const filePath = path . join ( configOptions . dir , filename )
5051
5152 const config : Config < T > = {
5253 async load ( ) {
53- return load < T > ( { filePath } )
54+ return load < T > ( { filePath, ENOENTResult : configOptions . ENOENTResult } )
5455 } ,
55- async update < T extends object > ( updater : ( obj : T ) => T ) : Promise < T > {
56+ async update ( updater : ( obj : T ) => T ) : Promise < T > {
5657 const data = await load < T > ( {
5758 filePath,
58- ENOENTResult : { } as T ,
59+ ENOENTResult : configOptions . ENOENTResult ,
5960 } )
60- const updatedData = updater ( data as T )
61+ const updatedData = updater ( data )
6162 await writeJsonFile ( filePath , updatedData , {
6263 indent : 2 ,
6364 mode : 0o666 ,
@@ -70,20 +71,25 @@ function generateConfig<T extends object>(configOptions: {
7071
7172export function projectConfig < T extends object > ( {
7273 cwd,
74+ ENOENTResult,
7375} : {
7476 cwd : string
77+ ENOENTResult : T | undefined
7578} ) : Config < T > {
7679 return generateConfig < T > ( {
7780 dir : cwd ,
7881 // dotfile, like .eslintrc.json or .travis.yml
7982 filename : `.blinkmrc.json` ,
83+ ENOENTResult,
8084 } )
8185}
8286
8387export function userConfig < T extends object > ( {
8488 name,
89+ ENOENTResult,
8590} : {
8691 name : string
92+ ENOENTResult : T
8793} ) : Config < T > {
8894 const p = os . platform ( )
8995
@@ -97,6 +103,7 @@ export function userConfig<T extends object>({
97103 const cfg = generateConfig < T > ( {
98104 dir : dirs . userConfig ( ) ,
99105 filename : name ,
106+ ENOENTResult,
100107 } )
101108
102109 return cfg
0 commit comments