Skip to content

Commit

Permalink
Replace use of recursive-readdir package with local implementation (#…
Browse files Browse the repository at this point in the history
…4537)

* remove and reimplement recursive-readdir package

* remove minimatch dependency

* don't add unused package to browser approved package list

* rush change

* change var to const/let and remove unecessary comments

* remove unnecessary comment

* rename readdir export

* fix function import

Co-authored-by: Matthew Jordan <14913576+mattbjordan@users.noreply.github.com>
Co-authored-by: Paul Connelly <22944042+pmconne@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent 75240c8 commit 7ca2709
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/build-tools",
"comment": "Replace use of recursive-readdir package with local implementation",
"type": "none"
}
],
"packageName": "@itwin/build-tools"
}
4 changes: 2 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tools/build/ThirdPartyNotices.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## [recursive-readdir](/~https://github.com/jergason/recursive-readdir)

The MIT License (MIT)

Copyright (c) \<year> \<copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 0 additions & 1 deletion tools/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"glob": "^7.1.2",
"mocha": "^10.0.0",
"mocha-junit-reporter": "^2.0.2",
"recursive-readdir": "^2.2.2",
"rimraf": "^3.0.2",
"tree-kill": "^1.2.0",
"typedoc": "^0.22.11",
Expand Down
2 changes: 1 addition & 1 deletion tools/build/scripts/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const argv = require("yargs").argv;
const path = require("path");
const paths = require("./config/paths");
const fs = require("fs-extra");
const readDirectory = require("recursive-readdir");
const { readDirectory } = require("./utils/recursiveReaddir");

const __PUBLISH_EXTRACT_START__ = "__PUBLISH_EXTRACT_START__";
const __PUBLISH_EXTRACT_END__ = "__PUBLISH_EXTRACT_END__";
Expand Down
96 changes: 96 additions & 0 deletions tools/build/scripts/utils/recursiveReaddir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const fs = require("fs");
const p = require("path");

function matchesFile(ignorePath) {
return function (path, stats) {
return stats.isFile() && ignorePath === path;
};
}

function toMatcherFunction(ignoreEntry) {
if (typeof ignoreEntry == "function") {
return ignoreEntry;
} else {
return matchesFile(ignoreEntry);
}
}

function readdir(path, ignores, callback) {
if (typeof ignores == "function") {
callback = ignores;
ignores = [];
}

if (!callback) {
return new Promise(function (resolve, reject) {
readdir(path, ignores || [], function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}

ignores = ignores.map(toMatcherFunction);

let list = [];

fs.readdir(path, function (err, files) {
if (err) {
return callback(err);
}

let pending = files.length;
if (!pending) {
// we are done
return callback(null, list);
}

files.forEach(function (file) {
const filePath = p.join(path, file);
fs.stat(filePath, function (_err, stats) {
if (_err) {
return callback(_err);
}

if (
ignores.some(function (matcher) {
return matcher(filePath, stats);
})
) {
pending -= 1;
if (!pending) {
return callback(null, list);
}
return null;
}

if (stats.isDirectory()) {
readdir(filePath, ignores, function (__err, res) {
if (__err) {
return callback(__err);
}

list = list.concat(res);
pending -= 1;
if (!pending) {
return callback(null, list);
}
});
} else {
list.push(filePath);
pending -= 1;
if (!pending) {
return callback(null, list);
}
}
});
});
});
}

module.exports = {
readDirectory: readdir
};

0 comments on commit 7ca2709

Please sign in to comment.