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 2222 " build"
2323 ],
2424 "scripts" : {
25- "prepublishOnly" : " yarn test && tsc " ,
25+ "prepublishOnly" : " tsc && yarn test " ,
2626 "test" : " ava --serial"
2727 },
2828 "devDependencies" : {
4444 "detect-indent" : " ^6.1.0" ,
4545 "graceful-fs" : " ^4.2.6" ,
4646 "parse-json" : " ^5.2.0" ,
47- "strip-bom" : " ^5.0.0 " ,
47+ "strip-bom" : " 4 " ,
4848 "type-fest" : " ^1.2.0"
4949 },
5050 "ava" : {
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import stripBom from "strip-bom"
44import parseJson from "parse-json"
55import fs from "graceful-fs"
66
7+ type MaybePromise < T > = T | Promise < T > ;
8+
79interface Options {
810 /** @default utf-8 */
911 encoding : BufferEncoding
@@ -38,13 +40,13 @@ type GettersDeep<T extends object> = {
3840
3941export type ModifyJsonFileFunction < T extends object > = (
4042 path : string ,
41- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => T ) ,
43+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
4244 options ?: Options
4345) => Promise < void > ;
4446
4547type ModifyJsonFileGenericFunction = < T extends object > (
4648 path : string ,
47- modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => T ) ,
49+ modifyFields : Partial < T | GettersDeep < T > > | ( ( oldJson : T ) => MaybePromise < T > ) ,
4850 options ?: Partial < Options >
4951) => Promise < void > ;
5052
@@ -90,7 +92,7 @@ export const modifyJsonFile: ModifyJsonFileGenericFunction = async (
9092 // todo remove restriction or not?
9193 if ( ! json || typeof json !== "object" || Array . isArray ( json ) ) throw new TypeError ( `${ path } : JSON root type must be object` ) ;
9294 if ( typeof modifyFields === "function" ) {
93- json = modifyFields ( json )
95+ json = await modifyFields ( json )
9496 } else {
9597 for ( const [ name , value ] of Object . entries ( modifyFields ) ) {
9698 if ( ! ( name in json ) ) {
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import jsonfile from "jsonfile";
44import path from "path" ;
55import del from "del" ;
66import nanoid from "nanoid" ;
7- import { modifyJsonFile } from "../src/index "
7+ import { modifyJsonFile , modifyPackageJsonFile } from "../build/ "
88import fs from "fs/promises" ;
99
1010const jsonFilePath = path . join ( __dirname , "testing-file.json" ) ;
@@ -35,4 +35,12 @@ test("modifies JSON file", async t => {
3535 } )
3636 const modifiedJsonFle = await fs . readFile ( jsonFilePath , "utf8" ) ;
3737 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 ) ;
3846} )
Original file line number Diff line number Diff line change @@ -16,3 +16,12 @@ Generated by [AVA](https://avajs.dev).
1616 "type-fest": "^1.0.0"␊
1717 }␊
1818 }`
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:
18111811 dependencies :
18121812 ansi-regex "^5.0.0"
18131813
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 :
18201815 version "4.0.0"
18211816 resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
18221817 integrity sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==
18231818
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 =
18281823
18291824strip-json-comments@~2.0.1 :
18301825 version "2.0.1"
You can’t perform that action at this time.
0 commit comments