Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotreborn committed Aug 22, 2021
2 parents c4faf08 + 6c70716 commit daebb45
Show file tree
Hide file tree
Showing 35 changed files with 1,094 additions and 549 deletions.
83 changes: 53 additions & 30 deletions .derealize/backend-studio/git.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { Clone, Repository, Reference, Signature, Cred, Branch, StatusFile, ProxyOptions } from '@derealize/nodegit'
import {
Clone,
Repository,
Remote,
Reference,
Signature,
Cred,
Branch,
StatusFile,
ProxyOptions,
Credential,
FetchOption,
} from '@derealize/nodegit'
import type { CommitLog } from './backend.interface'

const proxyOpts = {}
// https://github.com/nodegit/nodegit/blob/master/CHANGELOG.md#changes-or-improvements
// const proxyOpts = new ProxyOptions()
// proxyOpts.url = 'socks5://127.0.0.1:10808'

export const checkBranch = async (repo: Repository, branch: string): Promise<void> => {
const getFetchOpts = (sshkey?: string): FetchOption => ({
proxyOpts,
callbacks: {
certificateCheck: () => 0,
credentials(_url: string, userName: string) {
if (sshkey) {
return Credential.sshKeyNew(userName, `${sshkey}.pub`, sshkey, '')
}
return Credential.defaultNew()
},
},
})

export const checkoutOriginBranch = async (repo: Repository, branch: string): Promise<void> => {
let ref = await repo.getCurrentBranch()
if (ref.name() !== `refs/heads/${branch}`) {
// https://github.com/nodegit/nodegit/issues/1261#issuecomment-431831338
Expand All @@ -17,22 +42,29 @@ export const checkBranch = async (repo: Repository, branch: string): Promise<voi
}
}

export const gitOpen = async (path: string): Promise<Repository> => {
export const forkBranch = async (repo: Repository, branch: string): Promise<void> => {
let ref = await repo.getCurrentBranch()
if (ref.name() !== `refs/heads/${branch}`) {
const commit = await repo.getHeadCommit()
ref = await repo.createBranch(branch, commit, true)
await repo.checkoutBranch(ref)
}
}

export const open = async (path: string): Promise<Repository> => {
const repo = await Repository.open(path)
return repo
}

export const gitClone = async (url: string, path: string, branch: string): Promise<Repository> => {
export const clone = async (url: string, path: string, branch: string, sshkey?: string): Promise<Repository> => {
const repo = await Clone.clone(url, path, {
checkoutBranch: branch,
fetchOpts: {
proxyOpts,
},
fetchOpts: getFetchOpts(sshkey),
})
return repo
}

export const gitCommit = async (repo: Repository, msg: string) => {
export const commit = async (repo: Repository, msg: string) => {
// https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js
const index = await repo.refreshIndex()
await index.addAll()
Expand All @@ -44,35 +76,26 @@ export const gitCommit = async (repo: Repository, msg: string) => {
await repo.createCommit('HEAD', committer, committer, msg, oid, [parent])
}

export const gitPull = async (repo: Repository) => {
await repo.fetch('origin', {
proxyOpts,
callbacks: {
credentials(_url: any, userName: string) {
return Cred.sshKeyFromAgent(userName)
},
},
})
export const pull = async (repo: Repository, branch: string, sshkey?: string) => {
await repo.fetch('origin', getFetchOpts(sshkey))

// https://github.com/nodegit/nodegit/blob/master/examples/pull.js
await repo.mergeBranches('derealize', 'origin/derealize')
await repo.mergeBranches(branch, `origin/${branch}`)
}

export const gitPush = async (repo: Repository) => {
export const push = async (repo: Repository, branch: string, sshkey?: string) => {
// https://github.com/nodegit/nodegit/blob/master/examples/push.js
const remote = await repo.getRemote('derealize')
const pushId = await remote.push(['refs/heads/derealize:refs/heads/derealize'], {
proxyOpts,
callbacks: {
credentials(_url: any, userName: string) {
return Cred.sshKeyFromAgent(userName)
},
},
})
const remote = await repo.getRemote('origin')
const pushId = await remote.push([`refs/heads/${branch}:refs/heads/${branch}`], getFetchOpts(sshkey))
}

export const switchOrigin = async (repo: Repository, url: string) => {
await Remote.delete(repo, 'origin')
await Remote.create(repo, 'origin', url)
}

// recommend engineers use 'rebase' instead 'merge' when merging code into the derealize branch
export const gitHistory = (repo: Repository): Promise<Array<CommitLog>> => {
// recommend engineers use 'rebase' instead 'merge' when merging code into the work branch
export const history = (repo: Repository): Promise<Array<CommitLog>> => {
// https://github.com/nodegit/nodegit/blob/master/examples/walk-history.js
return new Promise((resolve, reject) => {
repo
Expand Down
84 changes: 76 additions & 8 deletions .derealize/backend-studio/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import fs from 'fs/promises'
import * as fs from 'fs/promises'
import os from 'os'
import { constants } from 'fs'
import sysPath from 'path'
import log, { captureException } from './log'
import Project from './project'
import type { HistoryReply, BoolReply, TailwindConfigReply } from './backend.interface'
import type { ProjectIdParam, ImportPayloadStd } from '../interface'
import type { HistoryReply, BoolReply, TailwindConfigReply, SshKey } from './backend.interface'
import type { ProjectIdParam, ImportPayloadStd, MigrateGitOriginPayload } from '../interface'
import {
ElementPayload,
InsertElementPayload,
Expand All @@ -17,6 +20,7 @@ import { Apply, Insert, Delete } from './shift/react'
import { SetImage, RemoveImage } from './shift/image'
import { SetColor, RemoveColor } from './shift/colors'
import { npmStart } from './npm'
import { getSSHKeysPath } from './assetspath'

const projectsMap = new Map<string, Project>()

Expand All @@ -26,15 +30,23 @@ const getProject = (id: string): Project => {
return project
}

export const Import = async ({ projectId, url, path, branch }: ImportPayloadStd): Promise<BoolReply> => {
export const Import = async ({ projectId, path, giturl, sshkey, branch }: ImportPayloadStd): Promise<BoolReply> => {
let project = projectsMap.get(projectId)
if (!project) {
project = new Project(projectId, url, path, branch)
project = new Project(projectId, path, giturl, sshkey, branch)
projectsMap.set(projectId, project)
}

const result = await project.Import()
return result
let reply: BoolReply
if (project.giturl) {
reply = await project.GitClone()
if (!reply.result) return reply
} else {
reply = await project.GitOpen()
if (!reply.result) return reply
}
reply = await project.Flush()
return reply
}

export const Remove = async ({ projectId }: ProjectIdParam): Promise<BoolReply> => {
Expand All @@ -44,7 +56,7 @@ export const Remove = async ({ projectId }: ProjectIdParam): Promise<BoolReply>

export const Install = async ({ projectId }: ProjectIdParam): Promise<BoolReply> => {
const project = getProject(projectId)
const result = project.Install()
const result = await project.Install()
return result
}

Expand Down Expand Up @@ -76,12 +88,37 @@ export const Push = async ({ projectId, msg }: ProjectIdParam & { msg: string })
return reply
}

export const UpdateGitBranch = async ({
projectId,
branch,
}: ProjectIdParam & { branch: string }): Promise<BoolReply> => {
const project = getProject(projectId)
const reply = await project.UpdateGitBranch(branch)
return reply
}

export const MigrateGitOrigin = async ({
projectId,
giturl,
sshkey,
branch,
}: MigrateGitOriginPayload): Promise<BoolReply> => {
const project = getProject(projectId)
const reply = await project.MigrateGitOrigin(giturl, branch, sshkey)
return reply
}

export const History = async ({ projectId }: ProjectIdParam): Promise<HistoryReply> => {
const project = getProject(projectId)
const logs = await project.History()
return logs
}

export const CheckDirectoryEmpty = async ({ path }: { path: string }): Promise<BoolReply> => {
const files = await fs.readdir(path)
return { result: files.length === 0 }
}

// export const Dispose = async ({ projectId }: ProjectIdParam) => {
// const project = getProject(projectId)
// project?.Dispose()
Expand Down Expand Up @@ -201,3 +238,34 @@ export const ThemeRemoveColor = async ({ projectId, theme, key }: ThemeColorPayl
}
return { error }
}

const ExploreDirectory = async (dict: string): Promise<SshKey[]> => {
try {
await fs.access(dict, constants.R_OK)
const keys: Array<SshKey> = []
const files = await fs.readdir(dict)
for (const filename of files) {
const filePath = sysPath.resolve(dict, filename)
const stat = await fs.stat(filePath)
if (stat.isFile() && sysPath.extname(filename) === '.pub') {
await fs.access(filePath, constants.R_OK)
const privateKeyPath = sysPath.resolve(dict, sysPath.parse(filename).name)
await fs.access(privateKeyPath, constants.R_OK)
keys.push({
privateKeyPath,
publicKeyPath: filePath,
})
}
}
return keys
} catch (err) {
captureException(err)
}
return []
}

export const ExploreSSHKeys = async (): Promise<SshKey[]> => {
const globalKeys = await ExploreDirectory(`${os.homedir()}/.ssh`)
const localKeys = await ExploreDirectory(getSSHKeysPath())
return globalKeys.concat(localKeys)
}
Loading

0 comments on commit daebb45

Please sign in to comment.