Skip to content

Commit cebeab6

Browse files
committed
style: formatting
1 parent fbc4b1e commit cebeab6

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/index.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
import { 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

77
type MaybePromise<T> = T | Promise<T>;
88

99
interface 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

3636
type 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

4141
export 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

4747
type 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
*/
7878
export 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

tests/index.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import test from "ava";
2-
3-
import jsonfile from "jsonfile";
4-
import path from "path";
52
import del from "del";
6-
import nanoid from "nanoid";
7-
import { modifyJsonFile, modifyPackageJsonFile } from "../build/"
83
import fs from "fs/promises";
4+
import jsonfile from "jsonfile";
5+
import path from "path";
6+
7+
import { modifyJsonFile, modifyPackageJsonFile } from "../build/";
98

109
const jsonFilePath = path.join(__dirname, "testing-file.json");
1110

11+
// that's fine until tests are running with --serial flag
12+
let dataToWrite;
13+
1214
test.beforeEach(async () => {
13-
await jsonfile.writeFile(jsonFilePath, {
15+
await jsonfile.writeFile(jsonFilePath, dataToWrite ?? {
1416
"name": "package",
1517
"main": "index.js",
1618
"author": "eldar",
@@ -19,11 +21,11 @@ test.beforeEach(async () => {
1921
"fdir": ">=2"
2022
}
2123
}, { spaces: 4 });
22-
})
24+
});
2325

2426
test.afterEach.always(async () => {
2527
await del(jsonFilePath);
26-
})
28+
});
2729

2830
test("modifies JSON file", async t => {
2931
await modifyJsonFile(jsonFilePath, {
@@ -32,15 +34,16 @@ test("modifies JSON file", async t => {
3234
dependencies: {
3335
"type-fest": "^1.0.0"
3436
}
35-
})
37+
});
3638
const modifiedJsonFle = await fs.readFile(jsonFilePath, "utf8");
3739
t.snapshot(modifiedJsonFle);
38-
})
40+
});
3941

4042
test("modifies package.json file with async function", async t => {
4143
await modifyPackageJsonFile(jsonFilePath, async ({ main }) => {
4244
return { types: main, name: "ahaha" };
43-
})
45+
});
4446
const modifiedJsonFle = await fs.readFile(jsonFilePath, "utf8");
4547
t.snapshot(modifiedJsonFle);
46-
})
48+
});
49+

0 commit comments

Comments
 (0)