add sync download
This commit is contained in:
@@ -9,9 +9,8 @@ import inquirer from 'inquirer';
|
||||
import { packLib, unpackLib } from './publish.ts';
|
||||
import chalk from 'chalk';
|
||||
import { installDeps } from '@/uitls/npm.ts';
|
||||
import cryptojs from 'crypto-js';
|
||||
import { upload } from '@/module/download/upload.ts';
|
||||
const MD5 = cryptojs.MD5;
|
||||
import { getHash } from '@/uitls/hash.ts';
|
||||
/**
|
||||
* 获取package.json 中的 basename, version, user, appKey
|
||||
* @returns
|
||||
@@ -150,10 +149,7 @@ const command = new Command('deploy')
|
||||
console.error('error', error);
|
||||
}
|
||||
});
|
||||
export const getHash = (file: string) => {
|
||||
const content = fs.readFileSync(file, 'utf-8');
|
||||
return MD5(content).toString();
|
||||
};
|
||||
|
||||
type UploadFileOptions = {
|
||||
key: string;
|
||||
version: string;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { program as app, Command } from '@/program.ts';
|
||||
|
||||
const command = new Command('sync')
|
||||
.option('-d --dir <dir>')
|
||||
.description('同步项目')
|
||||
.action(() => {
|
||||
console.log('同步项目');
|
||||
});
|
||||
const syncUpload = new Command('upload').description('上传项目').action(() => {
|
||||
console.log('上传项目');
|
||||
});
|
||||
const syncDownload = new Command('download').description('下载项目').action(() => {
|
||||
console.log('下载项目');
|
||||
});
|
||||
|
||||
command.addCommand(syncUpload);
|
||||
command.addCommand(syncDownload);
|
||||
app.addCommand(command);
|
||||
87
src/command/sync/modules/base.ts
Normal file
87
src/command/sync/modules/base.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { Config, SyncList } from './type.ts';
|
||||
import { fileIsExist } from '@/uitls/file.ts';
|
||||
|
||||
export type SyncOptions = {
|
||||
dir?: string;
|
||||
configFilename?: string;
|
||||
baseURL?: string;
|
||||
};
|
||||
export class SyncBase {
|
||||
config: Config;
|
||||
#filename: string;
|
||||
#dir: string;
|
||||
baseURL: string;
|
||||
constructor(opts?: SyncOptions) {
|
||||
const filename = opts?.configFilename || 'kevisual.json';
|
||||
const dir = opts?.dir || process.cwd();
|
||||
this.#filename = filename;
|
||||
this.#dir = path.resolve(dir);
|
||||
this.baseURL = opts?.baseURL ?? '';
|
||||
this.init();
|
||||
}
|
||||
async init() {
|
||||
try {
|
||||
const dir = this.#dir;
|
||||
const filename = this.#filename;
|
||||
const filepath = path.join(dir, filename);
|
||||
if (!fileIsExist(filepath)) throw new Error('config file not found');
|
||||
const config = JSON.parse(fs.readFileSync(filepath, 'utf-8'));
|
||||
this.config = config;
|
||||
return config;
|
||||
} catch (err) {
|
||||
this.config = {} as Config;
|
||||
return {} as Config;
|
||||
}
|
||||
}
|
||||
|
||||
async getSyncList(): Promise<SyncList[]> {
|
||||
const config = this.config!;
|
||||
const sync = config?.sync || {};
|
||||
const syncKeys = Object.keys(sync);
|
||||
const baseURL = this.baseURL;
|
||||
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') {
|
||||
return {
|
||||
filepath,
|
||||
url: value,
|
||||
auth: checkAuth(value, baseURL),
|
||||
};
|
||||
}
|
||||
return {
|
||||
filepath,
|
||||
...value,
|
||||
auth: checkAuth(value.url, baseURL),
|
||||
};
|
||||
});
|
||||
|
||||
return syncList;
|
||||
}
|
||||
async getDir(filepath: string, check = false) {
|
||||
const dir = path.dirname(filepath);
|
||||
if (check) {
|
||||
if (!fileIsExist(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
async download() {
|
||||
// const syncList = await this.getSyncList();
|
||||
// for (const item of syncList) {
|
||||
// }
|
||||
}
|
||||
async upload() {
|
||||
// need check permission
|
||||
}
|
||||
}
|
||||
21
src/command/sync/modules/type.ts
Normal file
21
src/command/sync/modules/type.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export type SyncConfig = {
|
||||
type?: 'sync'; // 是否可以同步
|
||||
url: string; // 文件具体的 url 的地址
|
||||
};
|
||||
export interface Config {
|
||||
name?: string; // 项目名称
|
||||
version?: string; // 项目版本号
|
||||
ignore?: string[]; // 忽略的目录或则文件,默认忽略 node_modules 使用 fast-glob 去匹配
|
||||
|
||||
sync: {
|
||||
[key: string]: SyncConfig | string;
|
||||
};
|
||||
}
|
||||
|
||||
export type SyncList = {
|
||||
filepath: string;
|
||||
/**
|
||||
* 是否需要鉴权, baseURL 为 kevisual 服务时,需要鉴权
|
||||
*/
|
||||
auth?: boolean;
|
||||
} & SyncConfig;
|
||||
33
src/command/sync/sync.ts
Normal file
33
src/command/sync/sync.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { program as app, Command } from '@/program.ts';
|
||||
import { SyncBase } from './modules/base.ts';
|
||||
import { baseURL } from '@/module/query.ts';
|
||||
import { fetchLink } from '@/module/download/install.ts';
|
||||
import fs from 'node:fs';
|
||||
|
||||
const command = new Command('sync')
|
||||
.option('-d --dir <dir>')
|
||||
.description('同步项目')
|
||||
.action(() => {
|
||||
console.log('同步项目');
|
||||
});
|
||||
const syncUpload = new Command('upload').description('上传项目').action(() => {
|
||||
console.log('上传项目');
|
||||
});
|
||||
const syncDownload = new Command('download')
|
||||
.option('-d --dir <dir>', '配置目录')
|
||||
.description('下载项目')
|
||||
.action(async () => {
|
||||
console.log('下载项目');
|
||||
const sync = new SyncBase({ baseURL: baseURL });
|
||||
const syncList = await sync.getSyncList();
|
||||
console.log(syncList);
|
||||
for (const item of syncList) {
|
||||
const { content } = await fetchLink(item.url, { setToken: item.auth, returnContent: true });
|
||||
await sync.getDir(item.filepath, true);
|
||||
fs.writeFileSync(item.filepath, content);
|
||||
}
|
||||
});
|
||||
|
||||
command.addCommand(syncUpload);
|
||||
command.addCommand(syncDownload);
|
||||
app.addCommand(command);
|
||||
Reference in New Issue
Block a user