cli-helpers
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -22,3 +22,6 @@ /** | ||
*/ | ||
export declare function writeJSONFile(file: string, data: any): void; | ||
export declare function writeJSONFile(file: string, data: any, options?: { | ||
top_comment?: string; | ||
bottom_comment?: string; | ||
}): void; | ||
export declare class JSONFileError extends Error { | ||
@@ -25,0 +28,0 @@ file: string; |
@@ -9,2 +9,3 @@ "use strict"; | ||
const path_1 = require("path"); | ||
const json_1 = require("./json"); | ||
/** | ||
@@ -37,2 +38,3 @@ * @description look for the file from the given directory or parent directory recursively | ||
let text = (0, fs_1.readFileSync)(file).toString(); | ||
text = (0, json_1.removeComments)(text); | ||
try { | ||
@@ -49,3 +51,3 @@ return JSON.parse(text); | ||
*/ | ||
function writeJSONFile(file, data) { | ||
function writeJSONFile(file, data, options) { | ||
let text; | ||
@@ -58,3 +60,9 @@ try { | ||
} | ||
(0, fs_1.writeFileSync)(file, text); | ||
if (options?.top_comment) { | ||
text = options.top_comment + '\n' + text; | ||
} | ||
if (options?.bottom_comment) { | ||
text = text + '\n' + options.bottom_comment; | ||
} | ||
(0, fs_1.writeFileSync)(file, text.trim() + '\n'); | ||
} | ||
@@ -61,0 +69,0 @@ class JSONFileError extends Error { |
export * from './io'; | ||
export * from './file'; | ||
export * from './json'; |
@@ -19,1 +19,2 @@ "use strict"; | ||
__exportStar(require("./file"), exports); | ||
__exportStar(require("./json"), exports); |
{ | ||
"name": "cli-helpers", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Helper library for building cli applications", | ||
@@ -27,10 +27,16 @@ "keywords": [ | ||
"scripts": { | ||
"test": "tsc --noEmit", | ||
"mocha": "ts-mocha src/*.spec.ts", | ||
"test": "npm run mocha && tsc --noEmit", | ||
"clean": "rimraf dist", | ||
"build": "rimraf dist && tsc -p . && rimraf dist/tsconfig.tsbuildinfo", | ||
"build": "rimraf dist && tsc -p . && rimraf dist/tsconfig.tsbuildinfo rimraf dist/*.spec.*", | ||
"tsc": "tsc -p ." | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "4", | ||
"@types/mocha": "^10.0.8", | ||
"@types/node": "^20.12.5", | ||
"chai": "4", | ||
"mocha": "^10.7.3", | ||
"rimraf": "^5.0.5", | ||
"ts-mocha": "^10.0.0", | ||
"ts-node": "^10.9.2", | ||
@@ -37,0 +43,0 @@ "ts-node-dev": "^2.0.0", |
@@ -46,3 +46,4 @@ # cli-helpers | ||
**io helper functions**: | ||
<details> | ||
<summary>io helper functions</summary> | ||
@@ -66,3 +67,5 @@ ```typescript | ||
**file helper functions**: | ||
</details> | ||
<details> | ||
<summary>file helper functions</summary> | ||
@@ -90,3 +93,10 @@ ````typescript | ||
*/ | ||
export function writeJSONFile(file: string, data: any): void | ||
export function writeJSONFile( | ||
file: string, | ||
data: any, | ||
options?: { | ||
top_comment?: string | ||
bottom_comment?: string | ||
}, | ||
): void | ||
@@ -98,2 +108,16 @@ export class JSONFileError extends Error { | ||
</details> | ||
<details> | ||
<summary>json helper functions</summary> | ||
```typescript | ||
/** | ||
* @description remove comments from json text | ||
*/ | ||
export function removeComments(text: string): string | ||
``` | ||
</details> | ||
## License | ||
@@ -100,0 +124,0 @@ |
12154
11
221
129
10