migrate mock class

master
cyhhao 2 years ago
parent 26150120d4
commit 5cebf1e919

@ -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
},
/**

@ -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<Ref[]> {
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<Status> {
let dict
let stPath = join(mockPath, "refs.json")
try {
let refsJson = await fs.readFile(stPath)
dict = JSON.parse(refsJson.toString())
download(path: string): Promise<[Status, Buffer]> {
throw new Error("Method not implemented.")
}
catch (e) {
dict = {}
await fs.mkdir(pathUtil.dirname(stPath), { recursive: true })
upload(path: string, file: Buffer): Promise<Status> {
throw new Error("Method not implemented.")
}
dict[path] = sha
await fs.writeFile(stPath, JSON.stringify(dict))
return Status.SUCCEED
remove(path: string): Promise<Status> {
throw new Error("Method not implemented.")
}
async removeRef(path: string): Promise<Status> {
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
listRefs(): Promise<Ref[]> {
throw new Error("Method not implemented.")
}
async remove(path: string): Promise<Status> {
setRef(path: string, sha: string): Promise<Status> {
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]
removeRef(path: string): Promise<Status> {
throw new Error("Method not implemented.")
}
async upload(path: string, file: Buffer): Promise<Status> {
let stPath = join(mockPath, path)
await fs.mkdir(pathUtil.dirname(stPath), { recursive: true })
await fs.writeFile(stPath, file)
return Status.SUCCEED
}
}

@ -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<Ref[]> {
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<Status> {
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<Status> {
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<Status> {
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<Status> {
let stPath = join(mockPath, path)
await fs.mkdir(pathUtil.dirname(stPath), { recursive: true })
await fs.writeFile(stPath, file)
return Status.SUCCEED
}
}
Loading…
Cancel
Save