From 385290c1cc5696366208eef5b66d0000875730b7 Mon Sep 17 00:00:00 2001 From: fyears Date: Sun, 7 Nov 2021 12:14:14 +0800 Subject: [PATCH] find hidden files --- src/misc.ts | 25 ++++++++++++++++++++++--- tests/misc.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 tests/misc.test.ts diff --git a/src/misc.ts b/src/misc.ts index 1d1a7a7..883bf75 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -5,9 +5,28 @@ import { base32 } from "rfc4648"; export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "ftp"; -export const ignoreHiddenFiles = (item: string) => { - const basename = path.basename(item); - return basename === "." || basename[0] !== "."; +/** + * If any part of the file starts with '.' or '_' then it's a hidden file. + * @param item + * @param loose + * @returns + */ +export const isHiddenPath = (item: string, loose: boolean = true) => { + const k = path.posix.normalize(item); // TODO: only unix path now + const k2 = k.split("/"); // TODO: only unix path now + // console.log(k2) + for (const singlePart of k2) { + if (singlePart === "." || singlePart === ".." || singlePart === "") { + continue; + } + if (singlePart[0] === ".") { + return true; + } + if (loose && singlePart[0] === "_") { + return true; + } + } + return false; }; /** diff --git a/tests/misc.test.ts b/tests/misc.test.ts new file mode 100644 index 0000000..d69f2a4 --- /dev/null +++ b/tests/misc.test.ts @@ -0,0 +1,40 @@ +import * as fs from "fs"; +import * as path from "path"; +import { expect } from "chai"; + +import * as misc from '../src/misc' + +describe("Misc tests", () => { + it("should find hidden file correctly", () => { + let item = ''; + expect(misc.isHiddenPath(item)).to.be.false; + + item = '.' + expect(misc.isHiddenPath(item)).to.be.false; + + item = '..' + expect(misc.isHiddenPath(item)).to.be.false; + + item = '/x/y/z/../././../a/b/c' + expect(misc.isHiddenPath(item)).to.be.false; + + item = '.hidden' + expect(misc.isHiddenPath(item)).to.be.true; + + item = '_hidden_loose' + expect(misc.isHiddenPath(item)).to.be.true; + expect(misc.isHiddenPath(item, false)).to.be.false; + + item = '/sdd/_hidden_loose' + expect(misc.isHiddenPath(item)).to.be.true; + + item = 'what/../_hidden_loose/what/what/what' + expect(misc.isHiddenPath(item)).to.be.true; + + item = 'what/../_hidden_loose/what/what/what' + expect(misc.isHiddenPath(item, false)).to.be.false; + + item = 'what/../_hidden_loose/../.hidden/what/what/what' + expect(misc.isHiddenPath(item, false)).to.be.true; + }); +});