-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.23 KB
/
index.js
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
'use strict';
var path = require('path');
var dirname = path.dirname;
var resolve = require('resolve');
var caller = require('caller');
var normalizeArgs = require('./normalize-args');
module.exports = function (opts) {
opts = normalizeArgs(arguments);
var currentDirectory = process.cwd();
var shimDirectory = dirname(caller());
var localCli = resolveSync(opts.path, currentDirectory);
var localPkg = null;
if (localCli) {
localPkg = resolveSync(opts.pkgPath, currentDirectory);
}
var globalCli = resolveSync(opts.relative, shimDirectory);
if (globalCli === localCli) {
globalCli = null;
}
var globalPkg = null;
if (globalCli) {
globalPkg = resolveSync(
opts.globalPkg,
shimDirectory
);
}
if (!(localCli || globalCli)) {
throw new Error('fallback-cli could not find local or global');
}
var callbackOptions = {
moduleName: opts.moduleName,
localCli: localCli,
localPkg: localPkg,
globalCli: globalCli,
globalPkg: globalPkg,
cli: localCli || globalCli,
pkg: localCli ? localPkg : globalPkg,
location: localCli ? 'local' : 'global'
};
return opts.run(callbackOptions);
};
function resolveSync(path, basedir) {
try {
return resolve.sync(path, {basedir: basedir});
} catch (e) {
return null;
}
}