Merge pull request #6038 from abpframework/feat/3857

Enabled negated glob patterns in copyResourcesTask clean
pull/6043/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit 6a04461ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -76,7 +76,8 @@ module.exports = {
"@libs": "./wwwroot/libs"
},
clean: [
"@libs"
"@libs",
"!@libs/**/foo.txt"
],
mappings: {
@ -85,7 +86,7 @@ module.exports = {
````
* **aliases** section defines standard aliases (placeholders) that can be used in the mapping paths. **@node_modules** and **@libs** are required (by the standard packages), you can define your own aliases to reduce duplication.
* **clean** section is a list of folders to clean before copying the files.
* **clean** section is a list of folders to clean before copying the files. Glob matching and negation is enabled, so you can fine-tune what to delete and keep. The example above will clean everything inside `./wwwroot/libs`, but keep any `foo.txt` files.
* **mappings** section is a list of mappings of files/folders to copy. This example does not copy any resource itself, but depends on a standard package.
An example mapping configuration is shown below:

@ -4,7 +4,9 @@
var gulp = require("gulp"),
merge = require("merge-stream"),
rimraf = require("rimraf"),
fse = require('fs-extra'),
glob = require('glob'),
micromatch = require('micromatch'),
path = require("path"),
extendObject = require('extend-object');
@ -41,13 +43,42 @@
return undefined;
}
}
function cleanFiles() {
if (resourceMapping.clean) {
for (var i = 0; i < resourceMapping.clean.length; i++) {
rimraf.sync(replaceAliases(resourceMapping.clean[i]) + '/**/*', { force: true });
}
}
function cleanDirsAndFiles(patterns) {
const { dirs, files } = findDirsAndFiles(patterns);
files.forEach(file => fse.unlinkSync(file));
dirs.forEach(dir => {
try {
fse.rmdirSync(dir);
} catch (_) {}
});
}
function findDirsAndFiles(patterns) {
const dirs = [];
const files = [];
const list = glob.sync('**/*', { dot: true });
const matches = micromatch(list, normalizeGlob(patterns), {
dot: true,
});
matches.forEach(match => {
if (!fse.pathExistsSync(match)) return;
(fse.statSync(match).isDirectory() ? dirs : files).push(match);
});
return { dirs, files };
}
function normalizeGlob(patterns) {
return patterns.map(pattern => {
const prefix = /\*$/.test(pattern) ? '' : '/**';
return replaceAliases(pattern).replace(/(!?)\.\//, '$1') + prefix;
});
}
function normalizeResourceMapping(resourcemapping) {
@ -64,13 +95,7 @@
extendObject(defaultSettings.aliases, resourcemapping.aliases);
resourcemapping.aliases = defaultSettings.aliases;
if (!resourcemapping.clean) {
resourcemapping.clean = [];
}
for (var i = 0; i < defaultSettings.clean.length; ++i) {
resourcemapping.clean.push(defaultSettings.clean[i]);
}
resourcemapping.clean = resourcemapping.clean || defaultSettings.clean;
return resourcemapping;
}
@ -112,7 +137,7 @@
rootPath = path;
resourceMapping = normalizeResourceMapping(buildResourceMapping(rootPath));
cleanFiles();
cleanDirsAndFiles(resourceMapping.clean);
var tasks = [];

File diff suppressed because it is too large Load Diff

@ -7,10 +7,12 @@
"dependencies": {
"ansi-colors": "^4.1.1",
"extend-object": "^1.0.0",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"gulp": "^4.0.2",
"merge-stream": "^2.0.0",
"path": "^0.12.7",
"rimraf": "^3.0.2"
"micromatch": "^4.0.2",
"path": "^0.12.7"
},
"gitHead": "bb4ea17d5996f01889134c138d00b6c8f858a431"
}

Loading…
Cancel
Save