11import { PackageJson , TsConfigJson } from "type-fest" ;
2- import detectIndent from "detect-indent"
3- import stripBom from "strip-bom"
4- import parseJson from "parse-json"
5- import fs from "graceful-fs"
2+ import detectIndent from "detect-indent" ;
3+ import stripBom from "strip-bom" ;
4+ import parseJson from "parse-json" ;
5+ import fs from "graceful-fs" ;
66
77type MaybePromise < T > = T | Promise < T > ;
88
99interface Options {
1010 /** @default utf-8 */
11- encoding : BufferEncoding
11+ encoding : BufferEncoding ;
1212 /**
1313 * If `false`, FS or JSON errors will be ignored
1414 * @default true
1515 * */
16- throws : boolean
16+ throws : boolean ;
1717 // ideally this lib should integrate with json validator
1818 /** @default "throw" (silent if throws: false) */
19- ifFieldIsMissing : "throw" | "skip" | "add"
19+ ifFieldIsMissing : "throw" | "skip" | "add" ;
2020 /**
2121 * - throw - throws (silent if throws: false)
2222 *- skip - won't call the function
2323 * - pass - pass the `undefined` value
2424 * @default "throw"
2525 * */
26- ifFieldIsMissingForSetter : "throw" | "skip" | "pass"
26+ ifFieldIsMissingForSetter : "throw" | "skip" | "pass" ;
2727 /**
2828 * - null - disable formatting
2929 * - hard - one hard tab \t
3030 * - number - number of spaces
3131 * @default "preserve"
3232 * */
33- tabSize : null | number | "preserve" | "hard"
33+ tabSize : null | number | "preserve" | "hard" ;
3434}
3535
3636type GettersDeep < T extends object > = {
3737 [ K in keyof T ] : ( oldValue : T [ K ] , json : T ) => T [ K ]
3838 //T[K] extends object ? ((oldValue: T[K]) => unknown)/* | GettersDeep<T[K]> */ : (oldValue: T[K]) => unknown
39- }
39+ } ;
4040
4141export type ModifyJsonFileFunction < T extends object > = (
4242 path : string ,
43- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
43+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
4444 options ?: Options
4545) => Promise < void > ;
4646
4747type ModifyJsonFileGenericFunction = < T extends object > (
4848 path : string ,
49- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
49+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
5050 options ?: Partial < Options >
5151) => Promise < void > ;
5252
@@ -57,11 +57,11 @@ const loadJsonFile = async (filePath: string, { encoding, tabSize }: Pick<Option
5757 ) ;
5858 return {
5959 json : parseJson ( contents , filePath ) ,
60- indent : tabSize === "preserve" ?
61- detectIndent ( contents ) . indent :
62- tabSize === "hard" ? "\t" : tabSize === null ?
63- undefined : " " . repeat ( tabSize )
64- }
60+ indent : tabSize === "preserve" ?
61+ detectIndent ( contents ) . indent :
62+ tabSize === "hard" ? "\t" : tabSize === null ?
63+ undefined : " " . repeat ( tabSize )
64+ } ;
6565} ;
6666
6767/** It's just Error */
@@ -77,22 +77,22 @@ const loadJsonFile = async (filePath: string, { encoding, tabSize }: Pick<Option
7777 */
7878export const modifyJsonFile : ModifyJsonFileGenericFunction = async (
7979 path ,
80- modifyFields ,
80+ modifyFields ,
8181 options
8282) => {
83- const {
83+ const {
8484 encoding = "utf-8" ,
85- throws = true ,
85+ throws = true ,
8686 ifFieldIsMissing = "throw" ,
8787 ifFieldIsMissingForSetter = "throw" ,
8888 tabSize = "preserve"
8989 } = options || { } ;
9090 try {
91- let { json, indent} = await loadJsonFile ( path , { encoding, tabSize } ) ;
91+ let { json, indent } = await loadJsonFile ( path , { encoding, tabSize } ) ;
9292 // todo remove restriction or not?
9393 if ( ! json || typeof json !== "object" || Array . isArray ( json ) ) throw new TypeError ( `${ path } : JSON root type must be object` ) ;
9494 if ( typeof modifyFields === "function" ) {
95- json = await modifyFields ( json )
95+ json = await modifyFields ( json ) ;
9696 } else {
9797 for ( const [ name , value ] of Object . entries ( modifyFields ) ) {
9898 if ( ! ( name in json ) ) {
@@ -109,11 +109,11 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (
109109 path ,
110110 JSON . stringify ( json , undefined , indent )
111111 ) ;
112- } catch ( err ) {
112+ } catch ( err ) {
113113 // if (err.innerError) throw new Error(err.message);
114114 if ( throws ) throw err ;
115115 }
116- }
116+ } ;
117117
118118// todo: use read-pkg / write-pkg for normalization
119119
0 commit comments