watchPkg.js
3.36 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.watchPkg = watchPkg;
exports.watchPkgs = watchPkgs;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _core() {
const data = require("@umijs/core");
_core = function _core() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function getUmiPlugins(opts) {
return Object.keys(_objectSpread(_objectSpread({}, opts.pkg.dependencies), opts.pkg.devDependencies)).filter(name => {
return (0, _core().isPluginOrPreset)(_core().PluginType.plugin, name) || (0, _core().isPluginOrPreset)(_core().PluginType.preset, name);
});
}
function getUmiPluginsFromPkgPath(opts) {
let pkg = {};
if ((0, _fs().existsSync)(opts.pkgPath)) {
try {
pkg = JSON.parse((0, _fs().readFileSync)(opts.pkgPath, 'utf-8'));
} catch (e) {}
}
return getUmiPlugins({
pkg
});
}
function watchPkg(opts) {
const pkgPath = (0, _path().join)(opts.cwd, 'package.json');
const plugins = getUmiPluginsFromPkgPath({
pkgPath
});
const watcher = _utils().chokidar.watch(pkgPath, {
ignoreInitial: true
});
watcher.on('all', () => {
const newPlugins = getUmiPluginsFromPkgPath({
pkgPath
});
if (!_utils().lodash.isEqual(plugins, newPlugins)) {
// 已经重启了,只处理一次就够了
opts.onChange();
}
});
return () => {
watcher.close();
};
}
function watchPkgs(opts) {
const unwatchs = [watchPkg({
cwd: opts.cwd,
onChange: opts.onChange
})];
if ((0, _utils().winPath)(opts.cwd) !== (0, _utils().winPath)(process.cwd())) {
unwatchs.push(watchPkg({
cwd: process.cwd(),
onChange: opts.onChange
}));
}
return () => {
unwatchs.forEach(unwatch => {
unwatch();
});
};
}