初始化:GitHub 同步插件
This commit is contained in:
commit
b1b1c821cc
14 files changed
+925
No files matched your search
@@ -0,0 +1,66 @@
|
||||
// 上传一个项目:补 .gitignore/README → 建仓库 → 提交 → 推送 → 记录状态。
|
||||
import { loadConfig, saveConfig, readToken } from './store.js'
|
||||
import { repoName, projectName, ensureReadme, ensureGitignore } from './project.js'
|
||||
import { getLogin, ensureRepo } from './github.js'
|
||||
import { gitAvailable, commitAndPush } from './git.js'
|
||||
|
||||
export class UploadError extends Error {}
|
||||
|
||||
/** 把错误翻译成一句人话。 */
|
||||
export function friendly(error) {
|
||||
const message = error?.message ?? String(error)
|
||||
if (/fetch failed|ENOTFOUND|ECONNREFUSED|ETIMEDOUT|EAI_AGAIN/i.test(message)) {
|
||||
return '连不上 GitHub,请检查网络后重试。'
|
||||
}
|
||||
return message.length > 200 ? `${message.slice(0, 197)}…` : message
|
||||
}
|
||||
|
||||
/** 读当前配置里的令牌;没有就抛出可直接展示的错误。 */
|
||||
export function requireToken() {
|
||||
const token = readToken()
|
||||
if (token === '') {
|
||||
throw new UploadError('还没有绑定 GitHub 令牌。先发 `/github token <你的令牌>`(令牌不会写进对话记录)。')
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
export async function uploadOne(dir, { description = '' } = {}) {
|
||||
const cfg = loadConfig()
|
||||
const token = requireToken()
|
||||
if (!gitAvailable()) throw new UploadError('这台机器上没有 git,无法上传。')
|
||||
let login = cfg.login
|
||||
if (login === '') {
|
||||
login = await getLogin(token)
|
||||
cfg.login = login
|
||||
saveConfig(cfg)
|
||||
}
|
||||
const name = repoName(dir)
|
||||
ensureGitignore(dir)
|
||||
const readme = ensureReadme(dir, { name, description })
|
||||
const repo = await ensureRepo({
|
||||
token,
|
||||
login,
|
||||
name,
|
||||
privateRepo: cfg.private,
|
||||
description: (description || cfg.description || '').trim() || `${projectName(dir)},由 DSH 同步`,
|
||||
})
|
||||
const when = new Date().toLocaleString('zh-CN', { hour12: false })
|
||||
const head = description.trim() === '' ? `同步 ${projectName(dir)} 源码` : description.trim()
|
||||
const result = await commitAndPush(dir, {
|
||||
url: repo.cloneUrl,
|
||||
token,
|
||||
login,
|
||||
message: `${head}\n\n由 DSH 自动上传 · ${when}`,
|
||||
})
|
||||
const rec = {
|
||||
repo: `${login}/${name}`,
|
||||
url: repo.url,
|
||||
lastPush: Date.now(),
|
||||
lastCommit: result.commit,
|
||||
files: result.files,
|
||||
readme: readme.written ? readme.reason : 'kept',
|
||||
}
|
||||
cfg.repos[dir] = rec
|
||||
saveConfig(cfg)
|
||||
return { ...rec, created: repo.created, pushed: result.pushed }
|
||||
}
|
||||
Reference in new issue
Block a user