Skip to content

Commit b880ae9

Browse files
committed
exposed utility function to prompt for installation of missing packages
1 parent 531ed97 commit b880ae9

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

lib/index.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
let AssetManager = require("./manager");
44
let resolvePath = require("./util/resolve");
5-
let { abort, repr } = require("./util");
5+
let { loadExtension, abort, repr } = require("./util");
66
let browserslist = require("browserslist");
77
let SerializedRunner = require("./util/runner");
88

@@ -47,7 +47,7 @@ module.exports = (referenceDir, config, { watch, fingerprint, sourcemaps, compac
4747

4848
let { bucket, plugin } = plugins[type];
4949
if(!plugin.call) {
50-
plugin = load(plugin);
50+
plugin = loadExtension(plugin, "ERROR: missing plugin");
5151
}
5252
let build = plugin(pluginConfig, assetManager,
5353
{ browsers, sourcemaps, compact });
@@ -96,14 +96,3 @@ function makeWatcher(watchDirs, referenceDir) {
9696
});
9797
return watcher;
9898
}
99-
100-
function load(pkg) {
101-
try {
102-
return require(pkg);
103-
} catch(err) {
104-
if(err.code !== "MODULE_NOT_FOUND") {
105-
throw err;
106-
}
107-
abort(`ERROR: missing plugin - please install ${repr(pkg)}`);
108-
}
109-
}

lib/util/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
let path = require("path");
44
let crypto = require("crypto");
55

6-
exports.abort = (msg, code = 1) => {
7-
console.error(msg);
8-
process.exit(code);
6+
exports.abort = abort;
7+
exports.repr = repr;
8+
9+
// attempts to `require` a module, prompting the user to install the
10+
// corresponding package if it is unavailable
11+
exports.loadExtension = (pkg, errorMessage, supplier = pkg) => {
12+
try {
13+
return require(pkg);
14+
} catch(err) {
15+
if(err.code !== "MODULE_NOT_FOUND") {
16+
throw err;
17+
}
18+
abort(`${errorMessage} - please install ${repr(supplier)}`);
19+
}
920
};
1021

1122
exports.generateFingerprint = (filepath, data) => {
@@ -30,12 +41,17 @@ exports.promisify = fn => {
3041
});
3142
};
3243

33-
exports.repr = (value, jsonify = true) => {
44+
function abort(msg, code = 1) {
45+
console.error(msg);
46+
process.exit(code);
47+
}
48+
49+
function repr(value, jsonify = true) {
3450
if(jsonify) {
3551
value = JSON.stringify(value);
3652
}
3753
return `\`${value}\``;
38-
};
54+
}
3955

4056
function generateHash(str) {
4157
let hash = crypto.createHash("md5");

0 commit comments

Comments
 (0)