add check sync

This commit is contained in:
2025-05-16 02:54:30 +08:00
parent aab0662fbc
commit 4e007d48a4
7 changed files with 186 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import { Config, SyncList, SyncConfigType, SyncConfig } from './type.ts';
import { fileIsExist } from '@/uitls/file.ts';
import { getHash } from '@/uitls/hash.ts';
import glob from 'fast-glob';
import { isMatch } from 'micromatch';
import { logger } from '@/module/logger.ts';
export type SyncOptions = {
@@ -11,11 +12,18 @@ export type SyncOptions = {
configFilename?: string;
baseURL?: string;
};
const checkAuth = (value: string = '', baseURL: string = '') => {
if (value.startsWith(baseURL)) {
return true;
}
return false;
};
export class SyncBase {
config: Config;
#filename: string;
#dir: string;
baseURL: string;
defaultIgnore: string[] = ['node_modules/**', '.git/**'];
constructor(opts?: SyncOptions) {
const filename = opts?.configFilename || 'kevisual.json';
const dir = opts?.dir || process.cwd();
@@ -47,7 +55,7 @@ export class SyncBase {
return {} as Config;
}
}
getRelativeFile(filename?: string) {
getRelativePath(filename?: string) {
if (!filename) return false;
const dir = this.#dir;
const file = path.join(dir, filename);
@@ -57,6 +65,18 @@ export class SyncBase {
if (syncType === 'sync') return true;
return syncType === type;
}
getIngore(ignore: string[] = []) {
const defaultIgnore = [...this.defaultIgnore, ...ignore];
const set = new Set(defaultIgnore);
return new Array(...set);
}
getMatchList(opts?: { matchList?: string[]; ignore: string[]; matchObjectList?: { path: string; [key: string]: any }[] }) {
const { matchList = [], ignore = [], matchObjectList = [] } = opts || {};
const _ignore = this.getIngore(ignore);
const _matchList = matchList.filter((file) => !isMatch(file, _ignore));
const _matchObjectList = matchObjectList.filter((item) => !isMatch(item.path, _ignore));
return { matchList: _matchList, matchObjectList: _matchObjectList };
}
/**
*
* @param opts
@@ -73,13 +93,6 @@ export class SyncBase {
const syncList = syncKeys.map((key) => {
const value = sync[key];
const filepath = path.join(this.#dir, key); // 文件的路径
const checkAuth = (value: string = '', baseURL: string = '') => {
if (value.startsWith(baseURL)) {
return true;
}
return false;
};
if (typeof value === 'string') {
const auth = checkAuth(value, baseURL);
const type = auth ? 'sync' : 'none';
@@ -106,6 +119,25 @@ export class SyncBase {
}
return syncList;
}
async getCheckList() {
const checkDir = this.config?.checkDir || {};
const dirKeys = Object.keys(checkDir);
const files = dirKeys.map((key) => {
return { key, ...this.getRelativePath(key) };
});
return files
.map((item) => {
if (!item) return;
let auth = checkAuth(checkDir[item.key]?.url, this.baseURL);
return {
key: item.key,
...checkDir[item.key],
filepath: item?.absolute,
auth,
};
})
.filter((item) => item);
}
getMergeSync(sync: Config['sync'] = {}, fileSync: Config['sync'] = {}) {
const syncFileSyncKeys = Object.keys(fileSync);
const syncKeys = Object.keys(sync);
@@ -126,7 +158,7 @@ export class SyncBase {
const { registry, ignore = [], files = [], replace = {} } = item;
const cwd = this.#dir;
const glob_files = await glob(files, {
ignore,
ignore: this.getIngore(ignore),
onlyFiles: true,
cwd,
dot: true,