allow glob-style input for --in-situ (#5646)

closes #5645
This commit is contained in:
Alex Lam S.L
2022-09-05 15:54:35 +01:00
committed by GitHub
parent 1d42e9ad55
commit 535212c69e

View File

@@ -278,6 +278,8 @@ if (specified["self"]) {
if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed"); if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed");
if (!options.wrap) options.wrap = "UglifyJS"; if (!options.wrap) options.wrap = "UglifyJS";
paths = UglifyJS.FILES; paths = UglifyJS.FILES;
} else if (paths.length) {
paths = simple_glob(paths);
} }
if (specified["in-situ"]) { if (specified["in-situ"]) {
if (output && output != "spidermonkey" || specified["reduce-test"] || specified["self"]) { if (output && output != "spidermonkey" || specified["reduce-test"] || specified["self"]) {
@@ -292,7 +294,7 @@ if (specified["in-situ"]) {
run(); run();
}); });
} else if (paths.length) { } else if (paths.length) {
simple_glob(paths).forEach(function(name) { paths.forEach(function(name) {
files[convert_path(name)] = read_file(name); files[convert_path(name)] = read_file(name);
}); });
run(); run();
@@ -491,33 +493,42 @@ function fatal(message) {
// A file glob function that only supports "*" and "?" wildcards in the basename. // A file glob function that only supports "*" and "?" wildcards in the basename.
// Example: "foo/bar/*baz??.*.js" // Example: "foo/bar/*baz??.*.js"
// Argument `glob` may be a string or an array of strings. // Argument `paths` must be an array of strings.
// Returns an array of strings. Garbage in, garbage out. // Returns an array of strings. Garbage in, garbage out.
function simple_glob(glob) { function simple_glob(paths) {
if (Array.isArray(glob)) { return paths.reduce(function(paths, glob) {
return [].concat.apply([], glob.map(simple_glob)); if (/\*|\?/.test(glob)) {
} var dir = path.dirname(glob);
if (glob.match(/\*|\?/)) { try {
var dir = path.dirname(glob); var entries = fs.readdirSync(dir).filter(function(name) {
try { try {
var entries = fs.readdirSync(dir); return fs.statSync(path.join(dir, name)).isFile();
} catch (ex) {} } catch (ex) {
if (entries) { return false;
var pattern = "^" + path.basename(glob) }
.replace(/[.+^$[\]\\(){}]/g, "\\$&") });
.replace(/\*/g, "[^/\\\\]*") } catch (ex) {}
.replace(/\?/g, "[^/\\\\]") + "$"; if (entries) {
var mod = process.platform === "win32" ? "i" : ""; var pattern = "^" + path.basename(glob)
var rx = new RegExp(pattern, mod); .replace(/[.+^$[\]\\(){}]/g, "\\$&")
var results = entries.sort().filter(function(name) { .replace(/\*/g, "[^/\\\\]*")
return rx.test(name); .replace(/\?/g, "[^/\\\\]") + "$";
}).map(function(name) { var mod = process.platform === "win32" ? "i" : "";
return path.join(dir, name); var rx = new RegExp(pattern, mod);
}); var results = entries.filter(function(name) {
if (results.length) return results; return rx.test(name);
}).sort().map(function(name) {
return path.join(dir, name);
});
if (results.length) {
[].push.apply(paths, results);
return paths;
}
}
} }
} paths.push(glob);
return [ glob ]; return paths;
}, []);
} }
function read_file(path, default_value) { function read_file(path, default_value) {