2020-07-29 13:42:51 +00:00
|
|
|
import {
|
|
|
|
readFileSync,
|
|
|
|
writeFileSync
|
|
|
|
} from "./filesystem";
|
2020-08-06 19:17:38 +00:00
|
|
|
import { sortElements, makeUnique } from "./types";
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
export function isValidJSON(path: string): boolean {
|
|
|
|
try {
|
|
|
|
let rawdata = readFileSync(path);
|
|
|
|
JSON.parse(rawdata);
|
|
|
|
return true;
|
|
|
|
} catch {
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
2020-08-06 19:17:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatJson(content: any) {
|
|
|
|
return JSON.stringify(content, null, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatSortJson(content: any) {
|
|
|
|
return JSON.stringify(sortElements(content), null, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatUniqueSortJson(content: any) {
|
|
|
|
return JSON.stringify(makeUnique(sortElements(content)), null, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatJsonFile(filename: string, silent: boolean = false) {
|
|
|
|
writeFileSync(filename, formatJson(JSON.parse(readFileSync(filename))));
|
|
|
|
console.log(`Formatted json file ${filename}`);
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatSortJsonFile(filename: string) {
|
2020-08-06 19:17:38 +00:00
|
|
|
writeFileSync(filename, formatSortJson(JSON.parse(readFileSync(filename))));
|
2020-07-29 13:42:51 +00:00
|
|
|
console.log(`Formatted json file ${filename}`);
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
export function readJsonFile(path: string): any {
|
|
|
|
return JSON.parse(readFileSync(path));
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:42:51 +00:00
|
|
|
export function writeJsonFile(path: string, data: any) {
|
|
|
|
writeFileSync(path, JSON.stringify(data, null, 4));
|
|
|
|
}
|