File tree Expand file tree Collapse file tree 6 files changed +30
-16
lines changed Expand file tree Collapse file tree 6 files changed +30
-16
lines changed Original file line number Diff line number Diff line change 22
22
" build"
23
23
],
24
24
"scripts" : {
25
- "prepublishOnly" : " yarn test && tsc " ,
25
+ "prepublishOnly" : " tsc && yarn test " ,
26
26
"test" : " ava --serial"
27
27
},
28
28
"devDependencies" : {
44
44
"detect-indent" : " ^6.1.0" ,
45
45
"graceful-fs" : " ^4.2.6" ,
46
46
"parse-json" : " ^5.2.0" ,
47
- "strip-bom" : " ^5.0.0 " ,
47
+ "strip-bom" : " 4 " ,
48
48
"type-fest" : " ^1.2.0"
49
49
},
50
50
"ava" : {
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import stripBom from "strip-bom"
4
4
import parseJson from "parse-json"
5
5
import fs from "graceful-fs"
6
6
7
+ type MaybePromise < T > = T | Promise < T > ;
8
+
7
9
interface Options {
8
10
/** @default utf-8 */
9
11
encoding : BufferEncoding
@@ -38,13 +40,13 @@ type GettersDeep<T extends object> = {
38
40
39
41
export type ModifyJsonFileFunction < T extends object > = (
40
42
path : string ,
41
- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => T ) ,
43
+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
42
44
options ?: Options
43
45
) => Promise < void > ;
44
46
45
47
type ModifyJsonFileGenericFunction = < T extends object > (
46
48
path : string ,
47
- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => T ) ,
49
+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
48
50
options ?: Partial < Options >
49
51
) => Promise < void > ;
50
52
@@ -90,7 +92,7 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (
90
92
// todo remove restriction or not?
91
93
if ( ! json || typeof json !== "object" || Array . isArray ( json ) ) throw new TypeError ( `${ path } : JSON root type must be object` ) ;
92
94
if ( typeof modifyFields === "function" ) {
93
- json = modifyFields ( json )
95
+ json = await modifyFields ( json )
94
96
} else {
95
97
for ( const [ name , value ] of Object . entries ( modifyFields ) ) {
96
98
if ( ! ( name in json ) ) {
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import jsonfile from "jsonfile";
4
4
import path from "path" ;
5
5
import del from "del" ;
6
6
import nanoid from "nanoid" ;
7
- import { modifyJsonFile } from "../src/index "
7
+ import { modifyJsonFile , modifyPackageJsonFile } from "../build/ "
8
8
import fs from "fs/promises" ;
9
9
10
10
const jsonFilePath = path . join ( __dirname , "testing-file.json" ) ;
@@ -35,4 +35,12 @@ test("modifies JSON file", async t => {
35
35
} )
36
36
const modifiedJsonFle = await fs . readFile ( jsonFilePath , "utf8" ) ;
37
37
t . snapshot ( modifiedJsonFle ) ;
38
+ } )
39
+
40
+ test ( "modifies package.json file with async function" , async t => {
41
+ await modifyPackageJsonFile ( jsonFilePath , async ( { main } ) => {
42
+ return { types : main , name : "ahaha" } ;
43
+ } )
44
+ const modifiedJsonFle = await fs . readFile ( jsonFilePath , "utf8" ) ;
45
+ t . snapshot ( modifiedJsonFle ) ;
38
46
} )
Original file line number Diff line number Diff line change @@ -16,3 +16,12 @@ Generated by [AVA](https://avajs.dev).
16
16
"type-fest": "^1.0.0"␊
17
17
}␊
18
18
}`
19
+
20
+ ## modifies package.json file with async function
21
+
22
+ > Snapshot 1
23
+
24
+ `{␊
25
+ "types": "index.js",␊
26
+ "name": "ahaha"␊
27
+ }`
Original file line number Diff line number Diff line change @@ -1811,20 +1811,15 @@ strip-ansi@^6.0.0:
1811
1811
dependencies :
1812
1812
ansi-regex "^5.0.0"
1813
1813
1814
- strip-bom@^3.0.0 :
1815
- version "3.0.0"
1816
- resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
1817
- integrity sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=
1818
-
1819
- strip-bom@^4.0.0 :
1814
+ strip-bom@4, strip-bom@^4.0.0 :
1820
1815
version "4.0.0"
1821
1816
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
1822
1817
integrity sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==
1823
1818
1824
- strip-bom@^5 .0.0 :
1825
- version "5 .0.0"
1826
- resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-5 .0.0.tgz#88d2e135d154dca7a5e06b4a4ba9653b6bdc0dd2 "
1827
- integrity sha512-p+byADHF7SzEcVnLvc/r3uognM1hUhObuHXxJcgLCfD194XAkaLbjq3Wzb0N5G2tgIjH0dgT708Z51QxMeu60A= =
1819
+ strip-bom@^3 .0.0 :
1820
+ version "3 .0.0"
1821
+ resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3 .0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3 "
1822
+ integrity sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM =
1828
1823
1829
1824
strip-json-comments@~2.0.1 :
1830
1825
version "2.0.1"
You can’t perform that action at this time.
0 commit comments