-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50.js
More file actions
100 lines (86 loc) · 2.33 KB
/
Copy path50.js
File metadata and controls
100 lines (86 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const fs = require("fs");
const path = require("path");
const arrify = require("arrify");
const which = require("which");
const readPkgUp = require("read-pkg-up");
const hasOwn = (obj, key) =>
obj.hasOwnProperty(key) && obj[key] !== null && obj[key] !== undefined;
const hasPath = (obj, keys) => {
const [key, ...tail] = keys;
if (hasOwn(obj, key)) {
if (tail && tail.length) {
return hasPath(obj[key], tail);
} else {
return true;
}
} else {
return false;
}
};
const has = (obj, keypath) => hasPath(obj, keypath.split(/[./]/));
const { pkg, path: pkgPath } = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd()),
});
const appDirectory = path.dirname(pkgPath);
const fromRoot = (...p) => path.join(appDirectory, ...p);
const hasFile = (...p) => fs.existsSync(fromRoot(...p));
const hasPkgProp = props => arrify(props).some(prop => has(pkg, prop));
const hasPkgSubProp = pkgProp => props =>
hasPkgProp(arrify(props).map(p => `${pkgProp}.${p}`));
const hasPeerDep = hasPkgSubProp("peerDependencies");
const hasDep = hasPkgSubProp("dependencies");
const hasDevDep = hasPkgSubProp("devDependencies");
const hasAnyDep = args => [hasDep, hasDevDep, hasPeerDep].some(fn => fn(args));
const ifAnyDep = (deps, t, f) => (hasAnyDep(arrify(deps)) ? t : f);
function parseEnv(name, def) {
if (envIsSet(name)) {
try {
return JSON.parse(process.env[name] || "<fail>");
} catch (err) {
return process.env[name];
}
}
return def;
}
function envIsSet(name) {
return (
process.env.hasOwnProperty(name) &&
process.env[name] &&
process.env[name] !== "undefined"
);
}
function resolveBin(
modName,
{ executable = modName, cwd = process.cwd() } = {},
) {
let pathFromWhich;
try {
pathFromWhich = fs.realpathSync(which.sync(executable));
} catch (_error) {
// ignore _error
}
try {
const modPkgPath = require.resolve(`${modName}/package.json`);
const modPkgDir = path.dirname(modPkgPath);
const { bin } = require(modPkgPath);
const binPath = typeof bin === "string" ? bin : bin[executable];
const fullPathToBin = path.join(modPkgDir, binPath);
if (fullPathToBin === pathFromWhich) {
return executable;
}
return fullPathToBin.replace(cwd, ".");
} catch (error) {
if (pathFromWhich) {
return executable;
}
throw error;
}
}
module.exports = {
fromRoot,
hasFile,
hasPkgProp,
ifAnyDep,
parseEnv,
resolveBin,
};