mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Ignore certain file globs when under dir kube sync (#3452)
- .DS_Store - *.lock - *.swp Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
b5f4ab3c44
commit
f35178175b
3 files changed
+52
-7
No files matched your search
@@ -24,6 +24,7 @@ import type { CatalogEntity } from "../../common/catalog";
|
||||
import { catalogEntityRegistry } from "../../main/catalog";
|
||||
import { watch } from "chokidar";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import fse from "fs-extra";
|
||||
import type stream from "stream";
|
||||
import { Disposer, ExtendedObservableMap, iter, Singleton } from "../../common/utils";
|
||||
@@ -36,9 +37,23 @@ import { UserStore } from "../../common/user-store";
|
||||
import { ClusterStore, UpdateClusterModel } from "../../common/cluster-store";
|
||||
import { createHash } from "crypto";
|
||||
import { homedir } from "os";
|
||||
import globToRegExp from "glob-to-regexp";
|
||||
import { inspect } from "util";
|
||||
|
||||
const logPrefix = "[KUBECONFIG-SYNC]:";
|
||||
|
||||
/**
|
||||
* This is the list of globs of which files are ignored when under a folder sync
|
||||
*/
|
||||
const ignoreGlobs = [
|
||||
"*.lock", // kubectl lock files
|
||||
"*.swp", // vim swap files
|
||||
".DS_Store", // macOS specific
|
||||
].map(rawGlob => ({
|
||||
rawGlob,
|
||||
matcher: globToRegExp(rawGlob),
|
||||
}));
|
||||
|
||||
export class KubeconfigSyncManager extends Singleton {
|
||||
protected sources = observable.map<string, [IComputedValue<CatalogEntity[]>, Disposer]>();
|
||||
protected syncing = false;
|
||||
@@ -260,9 +275,10 @@ function diffChangedConfig(filePath: string, source: RootSource): Disposer {
|
||||
|
||||
async function watchFileChanges(filePath: string): Promise<[IComputedValue<CatalogEntity[]>, Disposer]> {
|
||||
const stat = await fse.stat(filePath); // traverses symlinks, is a race condition
|
||||
const isFolderSync = stat.isDirectory();
|
||||
const watcher = watch(filePath, {
|
||||
followSymlinks: true,
|
||||
depth: stat.isDirectory() ? 0 : 1, // DIRs works with 0 but files need 1 (bug: https://github.com/paulmillr/chokidar/issues/1095)
|
||||
depth: isFolderSync ? 0 : 1, // DIRs works with 0 but files need 1 (bug: https://github.com/paulmillr/chokidar/issues/1095)
|
||||
disableGlobbing: true,
|
||||
ignorePermissionErrors: true,
|
||||
usePolling: false,
|
||||
@@ -273,19 +289,36 @@ async function watchFileChanges(filePath: string): Promise<[IComputedValue<Catal
|
||||
});
|
||||
const rootSource = new ExtendedObservableMap<string, ObservableMap<string, RootSourceValue>>();
|
||||
const derivedSource = computed(() => Array.from(iter.flatMap(rootSource.values(), from => iter.map(from.values(), child => child[1]))));
|
||||
const stoppers = new Map<string, Disposer>();
|
||||
const cleanupFns = new Map<string, Disposer>();
|
||||
|
||||
watcher
|
||||
.on("change", (childFilePath) => {
|
||||
stoppers.get(childFilePath)();
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
const cleanup = cleanupFns.get(childFilePath);
|
||||
|
||||
if (!cleanup) {
|
||||
// file was previously ignored, do nothing
|
||||
return void logger.debug(`${logPrefix} ${inspect(childFilePath)} that should have been previously ignored has changed. Doing nothing`);
|
||||
}
|
||||
|
||||
cleanup();
|
||||
cleanupFns.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
})
|
||||
.on("add", (childFilePath) => {
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
if (isFolderSync) {
|
||||
const fileName = path.basename(childFilePath);
|
||||
|
||||
for (const ignoreGlob of ignoreGlobs) {
|
||||
if (ignoreGlob.matcher.test(fileName)) {
|
||||
return void logger.info(`${logPrefix} ignoring ${inspect(childFilePath)} due to ignore glob: ${ignoreGlob.rawGlob}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cleanupFns.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
})
|
||||
.on("unlink", (childFilePath) => {
|
||||
stoppers.get(childFilePath)();
|
||||
stoppers.delete(childFilePath);
|
||||
cleanupFns.get(childFilePath)?.();
|
||||
cleanupFns.delete(childFilePath);
|
||||
rootSource.delete(childFilePath);
|
||||
})
|
||||
.on("error", error => logger.error(`${logPrefix} watching file/folder failed: ${error}`, { filePath }));
|
||||
|
||||
Reference in new issue
Block a user