UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3// tslint:disable no-implicit-dependencies
4const fs = require("fs");
5const debug = require('debug')('@oclif/config');
6function flatMap(arr, fn) {
7 return arr.reduce((arr, i) => arr.concat(fn(i)), []);
8}
9exports.flatMap = flatMap;
10function mapValues(obj, fn) {
11 return Object.entries(obj)
12 .reduce((o, [k, v]) => {
13 o[k] = fn(v, k);
14 return o;
15 }, {});
16}
17exports.mapValues = mapValues;
18function loadJSONSync(path) {
19 debug('loadJSONSync %s', path);
20 // let loadJSON
21 // try { loadJSON = require('load-json-file') } catch {}
22 // if (loadJSON) return loadJSON.sync(path)
23 return JSON.parse(fs.readFileSync(path, 'utf8'));
24}
25exports.loadJSONSync = loadJSONSync;
26function exists(path) {
27 // tslint:disable-next-line
28 return new Promise(resolve => fs.exists(path, resolve));
29}
30exports.exists = exists;
31function loadJSON(path) {
32 debug('loadJSON %s', path);
33 // let loadJSON
34 // try { loadJSON = require('load-json-file') } catch {}
35 // if (loadJSON) return loadJSON.sync(path)
36 return new Promise((resolve, reject) => {
37 fs.readFile(path, 'utf8', (err, d) => {
38 try {
39 if (err)
40 reject(err);
41 else
42 resolve(JSON.parse(d));
43 }
44 catch (err) {
45 reject(err);
46 }
47 });
48 });
49}
50exports.loadJSON = loadJSON;
51function compact(a) {
52 return a.filter((a) => !!a);
53}
54exports.compact = compact;
55function uniq(arr) {
56 return arr.filter((a, i) => {
57 return !arr.find((b, j) => j > i && b === a);
58 });
59}
60exports.uniq = uniq;