UNPKG

1.48 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 resolvePackage(id, paths) {
22 return require.resolve(id, paths);
23}
24exports.resolvePackage = resolvePackage;
25function loadJSON(path) {
26 debug('loadJSON %s', path);
27 // let loadJSON
28 // try { loadJSON = require('load-json-file') } catch {}
29 // if (loadJSON) return loadJSON.sync(path)
30 return new Promise((resolve, reject) => {
31 fs.readFile(path, 'utf8', (err, d) => {
32 try {
33 if (err)
34 reject(err);
35 else
36 resolve(JSON.parse(d));
37 }
38 catch (error) {
39 reject(error);
40 }
41 });
42 });
43}
44exports.loadJSON = loadJSON;
45function compact(a) {
46 return a.filter((a) => Boolean(a));
47}
48exports.compact = compact;
49function uniq(arr) {
50 return arr.filter((a, i) => {
51 return !arr.find((b, j) => j > i && b === a);
52 });
53}
54exports.uniq = uniq;