From 5cebf1e919b37b427f45de78a7a31e212e75d5bf Mon Sep 17 00:00:00 2001 From: cyhhao Date: Mon, 12 Dec 2022 17:50:08 +0800 Subject: [PATCH] migrate mock class --- src/index.ts | 4 +-- src/storage/ETHStorage.ts | 71 +++++++----------------------------- src/storage/MockStorage.ts | 74 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 src/storage/MockStorage.ts diff --git a/src/index.ts b/src/index.ts index 07fafa8..5e600cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import GitRemoteHelper from './git/git-remote-helper' import { ApiBaseParams } from './git/git-remote-helper' import Git from './git/git' import { log } from './git/log' -import { ETHStorage } from './storage/ETHStorage' +import { MockStorage } from './storage/MockStorage' @@ -18,7 +18,7 @@ GitRemoteHelper({ * This will always be invoked when the remote helper is invoked */ init: async (p: ApiBaseParams) => { - git = new Git(p, new ETHStorage(p.remoteUrl)) + git = new Git(p, new MockStorage(p.remoteUrl)) return }, /** diff --git a/src/storage/ETHStorage.ts b/src/storage/ETHStorage.ts index 1187bbd..ca7cfb5 100644 --- a/src/storage/ETHStorage.ts +++ b/src/storage/ETHStorage.ts @@ -1,11 +1,5 @@ -import { promises as fs } from 'fs' -import pathUtil from 'path' import { Ref, Status, Storage } from "./storage" -import { superpathjoin as join } from 'superpathjoin' -const mockPath = process.env.HOME + "/.git3/mock" -fs.mkdir(mockPath, { recursive: true }) -const log = console.error -log("mock path", mockPath) + export class ETHStorage implements Storage { repoURI: string @@ -13,62 +7,23 @@ export class ETHStorage implements Storage { constructor(repoURI: string) { this.repoURI = repoURI } - - async listRefs(): Promise { - let stPath = join(mockPath, "refs.json") - try { - let refsJson = await fs.readFile(stPath) - let dict = JSON.parse(refsJson.toString()) - let list = [] - for (let key in dict) { - list.push({ ref: key, sha: dict[key] }) - } - return list - } - - catch (e) { - return [] - } - + download(path: string): Promise<[Status, Buffer]> { + throw new Error("Method not implemented.") } - async setRef(path: string, sha: string): Promise { - let dict - let stPath = join(mockPath, "refs.json") - try { - let refsJson = await fs.readFile(stPath) - dict = JSON.parse(refsJson.toString()) - } - catch (e) { - dict = {} - await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) - } - - dict[path] = sha - await fs.writeFile(stPath, JSON.stringify(dict)) - return Status.SUCCEED + upload(path: string, file: Buffer): Promise { + throw new Error("Method not implemented.") } - async removeRef(path: string): Promise { - let stPath = join(mockPath, "refs.json") - let refsJson = await fs.readFile(stPath) - let dict = JSON.parse(refsJson.toString()) - delete dict[path] - await fs.writeFile(stPath, JSON.stringify(dict)) - return Status.SUCCEED + remove(path: string): Promise { + throw new Error("Method not implemented.") } - - async remove(path: string): Promise { + listRefs(): Promise { throw new Error("Method not implemented.") } - async download(path: string): Promise<[Status, Buffer]> { - let buffer = await fs.readFile(join(mockPath, path)) - return [Status.SUCCEED, buffer] + setRef(path: string, sha: string): Promise { + throw new Error("Method not implemented.") } - - - async upload(path: string, file: Buffer): Promise { - let stPath = join(mockPath, path) - await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) - await fs.writeFile(stPath, file) - return Status.SUCCEED + removeRef(path: string): Promise { + throw new Error("Method not implemented.") } + } diff --git a/src/storage/MockStorage.ts b/src/storage/MockStorage.ts new file mode 100644 index 0000000..e663037 --- /dev/null +++ b/src/storage/MockStorage.ts @@ -0,0 +1,74 @@ +import { promises as fs } from 'fs' +import pathUtil from 'path' +import { Ref, Status, Storage } from "./storage" +import { superpathjoin as join } from 'superpathjoin' +const mockPath = process.env.HOME + "/.git3/mock" +fs.mkdir(mockPath, { recursive: true }) +const log = console.error +log("mock path", mockPath) + +export class MockStorage implements Storage { + repoURI: string + + constructor(repoURI: string) { + this.repoURI = repoURI + } + + async listRefs(): Promise { + let stPath = join(mockPath, "refs.json") + try { + let refsJson = await fs.readFile(stPath) + let dict = JSON.parse(refsJson.toString()) + let list = [] + for (let key in dict) { + list.push({ ref: key, sha: dict[key] }) + } + return list + } + + catch (e) { + return [] + } + + } + async setRef(path: string, sha: string): Promise { + let dict + let stPath = join(mockPath, "refs.json") + try { + let refsJson = await fs.readFile(stPath) + dict = JSON.parse(refsJson.toString()) + } + catch (e) { + dict = {} + await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) + } + + dict[path] = sha + await fs.writeFile(stPath, JSON.stringify(dict)) + return Status.SUCCEED + } + async removeRef(path: string): Promise { + let stPath = join(mockPath, "refs.json") + let refsJson = await fs.readFile(stPath) + let dict = JSON.parse(refsJson.toString()) + delete dict[path] + await fs.writeFile(stPath, JSON.stringify(dict)) + return Status.SUCCEED + } + + async remove(path: string): Promise { + throw new Error("Method not implemented.") + } + async download(path: string): Promise<[Status, Buffer]> { + let buffer = await fs.readFile(join(mockPath, path)) + return [Status.SUCCEED, buffer] + } + + + async upload(path: string, file: Buffer): Promise { + let stPath = join(mockPath, path) + await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) + await fs.writeFile(stPath, file) + return Status.SUCCEED + } +}