UNPKG

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