-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (64 loc) · 1.71 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*!
* base-cwd <https://github.com/jonschlinkert/base-cwd>
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var path = require('path');
var empty = require('empty-dir');
var isValid = require('is-valid-app');
var find = require('find-pkg');
module.exports = function(types, options) {
if (typeof types !== 'string' && !Array.isArray(types)) {
options = types;
types = undefined;
}
options = options || {};
var cached;
return function plugin(app) {
if (!isValid(app, 'base-cwd', types)) return;
var cwds = [app.cwd || process.cwd()];
Object.defineProperty(this, 'cwd', {
configurable: true,
enumerable: true,
set: function(cwd) {
cached = app.cache.cwd = path.resolve(cwd);
if (cwds[cwds.length - 1] !== cached) {
cwds.push(cached);
app.emit('cwd', cached);
}
},
get: function() {
if (typeof cached === 'string') {
return path.resolve(cached);
}
if (typeof app.options.cwd === 'string') {
return (cached = path.resolve(app.options.cwd));
}
var cwd = process.cwd();
if (options.findup === false) {
cached = cwd;
return cwd;
}
var isEmpty = empty.sync(cwd, function(fp) {
return !/\.DS_Store/.test(fp);
});
if (isEmpty) {
cached = cwd;
return cwd;
}
var pkgPath = find.sync(cwd);
if (pkgPath) {
var dir = path.dirname(pkgPath);
if (dir !== cwd) {
cached = app.cache.cwd = dir;
}
return dir;
}
return cwd;
}
});
return plugin;
}
};