Skip to content

Commit

Permalink
util: fix GS
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniaky committed Aug 22, 2023
1 parent 278e4d7 commit 4bb97b6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 119 deletions.
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"license": "Apache-2.0",
"dependencies": {
"glob": "^9.3.5",
"glob": "^10.3.3",
"undici": "^5.23.0"
},
"publishConfig": {
Expand Down
33 changes: 17 additions & 16 deletions packages/util/src/GS.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { globSync } from "glob";
import { existsSync, statSync } from "node:fs";
import { isAbsolute } from "node:path";
import { type } from "node:os";
import { join } from "node:path";
import { IgnoreFiles } from "./IgnoreFiles";

export class GS {
found: string[] = [];
ignore: IgnoreFiles;
declare ignore: IgnoreFiles;

constructor(
public pattern: string | string[],
Expand All @@ -19,28 +19,29 @@ export class GS {
});

if (Array.isArray(this.pattern)) {
this.pattern = this.pattern.map(path => this.#normalizePath(path));
this.pattern = this.pattern.flatMap(path => [
join(this.#normalizePath(path), "**"),
join("**", this.#normalizePath(path)),
]);
} else {
this.pattern = this.#normalizePath(this.pattern);
this.pattern = [
join(this.#normalizePath(this.pattern), "**"),
join("**", this.#normalizePath(this.pattern)),
];
}

this.found = globSync(this.pattern, {
dot: true,
ignore: this.ignore.list,
windowsPathsNoEscape: type() === "Windows_NT",
});

if (this.found.includes(".")) {
this.found.splice(this.found.indexOf("."), 1);
}
}

#normalizePath(path: string) {
try {
path = path.replace(/\\/g, "/");

if (!isAbsolute(path))
path = path.replace(/^(\.|~)$|^(\.|~)\/|^\/|\/$/g, "") || "**";

path = (existsSync(path) && statSync(path).isDirectory()) ? path.replace(/\/$/, "") + "/**" : path;
} catch {
path = path.replace(/\/$/, "") + "/**";
}
return path;
return join(...path.split(/[\\/]/));
}
}
134 changes: 56 additions & 78 deletions packages/util/src/IgnoreFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { readdirSync, existsSync, statSync, readFileSync } from "node:fs";
import { dirname, isAbsolute, join } from "node:path";
import { globSync } from "glob";
import { readFileSync } from "node:fs";
import { type } from "node:os";
import { dirname, join } from "node:path";

export const blockedFiles = {
common: [".git", ".vscode", ".cache"],
Expand All @@ -22,113 +24,89 @@ export interface IgnoreFilesOptions {
}

export class IgnoreFiles {
fileName: string;
declare fileName: string;
filesIgnore: string[] = [];
list: string[] = [];
list: string[] = allBlockedFiles;
paths: string[] = [];

constructor(options: IgnoreFilesOptions) {
if (Array.isArray(options.path)) {
for (const path of options.path) {
this.paths.push(this.#normalizePath(path));
}
} else {
this.paths.push(this.#normalizePath(options.path));
this.fileName = options.fileName;

if (options.optionalIgnoreList?.length) {
this.list.push(...IgnoreFiles.normalizePaths(options.optionalIgnoreList));
}

this.fileName = options.fileName;
for (let i = 0; i < this.list.length; i++) {
this.list[i] = IgnoreFiles.normalizeIgnore(this.list[i]);
}

if (this.fileName && this.paths.length)
this.filesIgnore = this.#findIgnoreFiles(this.fileName, this.paths);
if (Array.isArray(options.path)) {
this.paths = IgnoreFiles.normalizePaths(options.path);
} else if (options.path) {
this.paths.push(IgnoreFiles.normalizePath(options.path));
}

options.path = this.paths.flatMap(path => this.#makeBothCase(path));
if (this.fileName)
this.filesIgnore = IgnoreFiles.findIgnoreFiles(this.fileName, this.paths, this.list);

this.list = (options.optionalIgnoreList ?? [])
.flatMap(path => this.#makeBothCase(path))
.concat(this.#getIgnoreList())
.concat(this.#resolveIgnorePatterns(allBlockedFiles, options.path));
if (this.filesIgnore.length)
this.list.push(...this.processIgnoreFiles(this.filesIgnore));
}

#findIgnoreFiles(fileName: string, paths: string[]) {
return paths.flatMap(path => this.#recursivelyReadDirSync(path)
.filter(file => file.endsWith(fileName) && existsSync(file) && statSync(file).isFile()));
}
static findIgnoreFiles(fileName: string, paths: string[], ignore: string[]) {
const files = [];

#makeBothCase(s: string) {
if (isAbsolute(s))
return [
s.replace(/^./, a => a.toLowerCase()),
s.replace(/^./, a => a.toUpperCase()),
];
for (const path of paths) {
files.push(globSync(join(path, "**", fileName), {
dot: true,
ignore,
windowsPathsNoEscape: type() === "Windows_NT",
}));
}

return [s];
return files.flat();
}

#getIgnoreList() {
return this.#resolveIgnoreFile(this.filesIgnore);
static normalizeIgnore(ignore: string, path = "**") {
return join(path, ignore, "**").replace(/\\/g, "/");
}

#normalizePath(path: string) {
path = path.replace(/\\/g, "/").replace(/[*]/g, "") || ".";
if (path.length > 1) path = path.replace(/\/$/, "");
return path;
static normalizePath(path: string) {
return join(...path.split(/[\\/]/));
}

#resolveIgnorePatterns(ignore: string[], paths: string[]) {
return paths.flatMap(path => {
path = this.#normalizePath(path);

return ignore.flatMap(a => [
a,
`${isAbsolute(path) ? a : `**/${a}`}/**`,
`${path}/**/${a}`,
`${path}/**/${a}/**`,
]);
});
static normalizePaths(paths: string[]) {
for (let i = 0; i < paths.length; i++) {
paths[i] = this.normalizePath(paths[i]);
}

return paths;
}

#resolveIgnoreFile(ignoreFile: string | string[]) {
if (Array.isArray(ignoreFile)) {
const ignored = <string[]>[];
static processIgnoreFile(file: string) {
const dir = dirname(file).replace(/^\.$/, "") || "**";

for (const file of ignoreFile)
ignored.push(...this.#resolveIgnoreFile(file));
const list = readFileSync(file, "utf8")
.replace(/\s*#.*/g, "")
.split(/[\r\n]+/)
.filter(Boolean);

return ignored;
}
const ignore = [];

if (typeof ignoreFile === "string" && existsSync(ignoreFile) && statSync(ignoreFile).isFile()) {
const readed = readFileSync(ignoreFile, "utf8")
?.replace(/\s*#.*/g, "")
.split(/\r?\n/)
.filter(Boolean) ?? [];

return this.#resolveIgnorePatterns(readed, this.#makeBothCase(dirname(ignoreFile)));
for (const iterator of list) {
ignore.push(this.normalizeIgnore(iterator, dir));
}

return [];
return ignore;
}

#recursivelyReadDirSync(path: string): string[] {
if (!existsSync(path)) return [];
if (statSync(path).isFile())
return this.#recursivelyReadDirSync(dirname(path));

const files = readdirSync(this.#normalizePath(path), { withFileTypes: true });

const cache: string[] = [];
processIgnoreFiles(files = this.filesIgnore) {
const ignore = [];

for (const file of files) {
const fileOrDir = join(path, file.name);

if (file.isDirectory()) {
if (allBlockedFilesRegex.test(fileOrDir)) continue;
cache.push(...this.#recursivelyReadDirSync(fileOrDir));
} else {
cache.push(fileOrDir);
}
ignore.push(IgnoreFiles.processIgnoreFile(file));
}

return cache;
return ignore.flat();
}
}
26 changes: 2 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ glob-parent@^6.0.2:
dependencies:
is-glob "^4.0.3"

glob@^10.2.5:
glob@^10.2.5, glob@^10.3.3:
version "10.3.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-10.3.3.tgz#8360a4ffdd6ed90df84aa8d52f21f452e86a123b"
integrity sha512-92vPiMb/iqpmEgsOoIDvTjc50wf9CCCvMzsi6W0JLPeUKE8TWP1a73PgqSrqy7iAZxaSD1YdzU7QZR5LF51MJw==
Expand All @@ -693,16 +693,6 @@ glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^9.3.5:
version "9.3.5"
resolved "https://registry.yarnpkg.com/glob/-/glob-9.3.5.tgz#ca2ed8ca452781a3009685607fdf025a899dfe21"
integrity sha512-e1LleDykUz2Iu+MTYdkSsuWX8lvAjAcs0Xef0lNIu0S2wOAzuTxCJtcd9S3cijlwYF18EsU3rzb8jPVobxDh9Q==
dependencies:
fs.realpath "^1.0.0"
minimatch "^8.0.2"
minipass "^4.2.4"
path-scurry "^1.6.1"

globals@^13.19.0:
version "13.20.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-13.20.0.tgz#ea276a1e508ffd4f1612888f9d1bad1e2717bf82"
Expand Down Expand Up @@ -924,13 +914,6 @@ minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2:
dependencies:
brace-expansion "^1.1.7"

minimatch@^8.0.2:
version "8.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-8.0.4.tgz#847c1b25c014d4e9a7f68aaf63dedd668a626229"
integrity sha512-W0Wvr9HyFXZRGIDgCicunpQ299OKXs9RgZfaukz4qAW/pJhcpUfupc9c+OObPOFueNy8VSrZgEmDtk6Kh4WzDA==
dependencies:
brace-expansion "^2.0.1"

minimatch@^9.0.0, minimatch@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.1.tgz#8a555f541cf976c622daf078bb28f29fb927c253"
Expand All @@ -943,11 +926,6 @@ minimist@^1.2.5:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minipass@^4.2.4:
version "4.2.8"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.2.8.tgz#f0010f64393ecfc1d1ccb5f582bcaf45f48e1a3a"
integrity sha512-fNzuVyifolSLFL4NzpF+wEF4qrgqaaKX0haXPQEdQ7NKAN+WecoKMHV09YcuL/DHxrUsYQOK3MiuDf7Ip2OXfQ==

"minipass@^5.0.0 || ^6.0.2 || ^7.0.0":
version "7.0.1"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.0.1.tgz#dff63464407cd8b83d7f008c0f116fa8c9b77ebf"
Expand Down Expand Up @@ -1028,7 +1006,7 @@ path-parse@^1.0.6:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==

path-scurry@^1.10.1, path-scurry@^1.6.1:
path-scurry@^1.10.1:
version "1.10.1"
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-1.10.1.tgz#9ba6bf5aa8500fe9fd67df4f0d9483b2b0bfc698"
integrity sha512-MkhCqzzBEpPvxxQ71Md0b1Kk51W01lrYvlMzSUaIzNsODdd7mqhiimSZlr+VegAz5Z6Vzt9Xg2ttE//XBhH3EQ==
Expand Down

0 comments on commit 4bb97b6

Please sign in to comment.