63 lines
2.0 KiB
JavaScript
Executable File
63 lines
2.0 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// 命令行上传:node bin/push.js [项目目录…] [--desc 说明] [--all]
|
|
// 和面板走同一套逻辑(补 README、建仓库、提交、推送),不依赖 dsh-go 桥接。
|
|
import { existsSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import { loadConfig, readToken } from '../lib/store.js'
|
|
import { discoverRoots, findProjects, projectName } from '../lib/project.js'
|
|
import { DSH_HOME } from '../lib/store.js'
|
|
|
|
const args = process.argv.slice(2)
|
|
const dirs = []
|
|
let description = ''
|
|
for (let i = 0; i < args.length; i++) {
|
|
const a = args[i]
|
|
if (a === '--desc' || a === '-d') description = args[++i] ?? ''
|
|
else if (a === '--all') dirs.push('__all__')
|
|
else if (a === '--help' || a === '-h') { printHelp(); process.exit(0) }
|
|
else dirs.push(a)
|
|
}
|
|
|
|
function printHelp() {
|
|
console.log(`用法:node bin/push.js [目录…] [--desc "说明"] [--all]
|
|
|
|
目录 要上传的项目路径,默认当前目录
|
|
--desc 这次提交的说明(也会在第一次生成 README 时用上)
|
|
--all 上传工作区里检测到的所有项目
|
|
`)
|
|
}
|
|
|
|
const token = readToken()
|
|
if (token === '') {
|
|
console.error('还没有配置 GitHub 令牌。先运行:node bin/token.js')
|
|
process.exit(2)
|
|
}
|
|
|
|
let targets = []
|
|
if (dirs.includes('__all__')) {
|
|
targets = findProjects(discoverRoots(loadConfig().roots, DSH_HOME)).map((p) => p.dir)
|
|
} else if (dirs.length === 0) {
|
|
targets = [process.cwd()]
|
|
} else {
|
|
targets = dirs.map((d) => resolve(d))
|
|
}
|
|
|
|
const { uploadOne } = await import('../lib/index.js')
|
|
let failed = 0
|
|
for (const dir of targets) {
|
|
if (!existsSync(dir)) {
|
|
console.error(`✗ ${dir} 不存在`)
|
|
failed++
|
|
continue
|
|
}
|
|
process.stdout.write(`→ ${projectName(dir)} … `)
|
|
try {
|
|
const rec = await uploadOne(dir, { description })
|
|
console.log(`✓ ${rec.repo}${rec.created ? '(新建)' : ''} · ${rec.files} 个文件 · ${rec.url}`)
|
|
} catch (error) {
|
|
console.log(`✗ ${error?.message ?? error}`)
|
|
failed++
|
|
}
|
|
}
|
|
if (failed > 0) process.exitCode = 1
|