UNPKG

1.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const log_1 = require("./log");
4const path_1 = require("./path");
5const deps = {
6 m: {},
7 get HTTP() { return this.m.HTTP = this.m.HTTP || require('http-call').default; },
8 get loadJSONFile() { return this.m.loadJSONFile = this.m.loadJSONFile || require('load-json-file'); },
9 get writeJSONFile() { return this.m.writeJSONFile = this.m.writeJSONFile || require('write-json-file'); },
10};
11/**
12 * reads a json file in using load-json-file
13 * this will automatically join the paths if you pass multiple strings with path.join()
14 * can accept http urls
15 */
16function readJSON(filepaths) {
17 async function readJSONHTTP(url) {
18 const { body } = await deps.HTTP.get(url);
19 return body;
20 }
21 if (typeof filepaths === 'string' && filepaths.match(/https?:/))
22 return readJSONHTTP(filepaths);
23 const filepath = path_1.join(filepaths);
24 log_1.log('readJSON', filepath);
25 return deps.loadJSONFile(filepath);
26}
27exports.readJSON = readJSON;
28/**
29 * writes a json file with write-json-file
30 * this will automatically join the paths if you pass an array of strings
31 */
32function writeJSON(filepaths, data, options = {}) {
33 const filepath = path_1.join(filepaths);
34 log_1.log('writeJSON', filepath);
35 return deps.writeJSONFile(filepath, data, Object.assign({ indent: ' ' }, options));
36}
37exports.writeJSON = writeJSON;