mirror of
https://github.com/remotely-save/remotely-save.git
synced 2024-06-07 21:10:45 +00:00
find hidden files
This commit is contained in:
parent
371089e3ee
commit
385290c1cc
25
src/misc.ts
25
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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
40
tests/misc.test.ts
Normal file
40
tests/misc.test.ts
Normal file
@ -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;
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user