kup-cli 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/lib/main.js +25 -3
- package/lib/repo.js +72 -9
- package/lib/validate.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -76,8 +76,9 @@ Kup 会把 `file.md` 文件的内容更新到 `foo/bar` 仓库的编号为 `123`
|
|
|
76
76
|
1. 调用命令行时指定的 `--repo` 参数。
|
|
77
77
|
1. Markdown 文件内的 [元数据](https://github.com/cssmagic/kup/issues/1) 的 `repo` 字段。
|
|
78
78
|
1. 当前项目的 `package.json` 文件内的 `kup.repo` 字段。Kup 会从当前目录向上逐级寻找 `package.json` 文件。
|
|
79
|
+
1. 如果没有 `kup.repo` 字段,则 Kup 会尝试根据 `repository` 字段来猜测仓库名(在使用前会向用户确认)。
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
如果整个项目的同步目标都是同一个仓库,则建议采用 `kup.repo` 字段统一指定 `repo` 参数。
|
|
81
82
|
|
|
82
83
|
### 如何更方便地指定 `id` 参数?
|
|
83
84
|
|
package/lib/main.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import inquirer from 'inquirer'
|
|
2
|
+
|
|
1
3
|
import { isDebugging } from './util.js'
|
|
2
4
|
import { KupError, errorLine } from './error.js'
|
|
3
5
|
import { readTextFile } from './file.js'
|
|
@@ -34,7 +36,11 @@ async function main(argv) {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
// 通过各种方式获取 repo
|
|
37
|
-
// 优先级:
|
|
39
|
+
// 优先级:
|
|
40
|
+
// P1 命令行参数
|
|
41
|
+
// P2 文件元数据
|
|
42
|
+
// P3 package.json 中的 `kup.repo` 字段
|
|
43
|
+
// P4 package.json 中的 `repository` 字段(需确认)
|
|
38
44
|
let repoReal = ''
|
|
39
45
|
let repoSource = ''
|
|
40
46
|
if (repo) {
|
|
@@ -44,8 +50,24 @@ async function main(argv) {
|
|
|
44
50
|
repoReal = fileInfo.meta.repo
|
|
45
51
|
repoSource = 'meta'
|
|
46
52
|
} else {
|
|
47
|
-
|
|
48
|
-
if (
|
|
53
|
+
const repoResult = await getRepo(file)
|
|
54
|
+
if (repoResult.needsConfirm) {
|
|
55
|
+
const answer = await inquirer.prompt([
|
|
56
|
+
{
|
|
57
|
+
name: 'useGuessedRepo',
|
|
58
|
+
type: 'confirm',
|
|
59
|
+
message: `Kup guessed the GitHub repo "${ repoResult.repo }" from package.json#repository, use it?`,
|
|
60
|
+
default: true,
|
|
61
|
+
},
|
|
62
|
+
])
|
|
63
|
+
if (answer.useGuessedRepo) {
|
|
64
|
+
repoReal = repoResult.repo
|
|
65
|
+
repoSource = repoResult.source
|
|
66
|
+
}
|
|
67
|
+
} else if (repoResult.repo) {
|
|
68
|
+
repoReal = repoResult.repo
|
|
69
|
+
repoSource = repoResult.source
|
|
70
|
+
}
|
|
49
71
|
}
|
|
50
72
|
if (!repoReal) {
|
|
51
73
|
throw new KupError([
|
package/lib/repo.js
CHANGED
|
@@ -5,18 +5,54 @@ import { readTextFile } from './file.js'
|
|
|
5
5
|
import { validateRepo } from './validate.js'
|
|
6
6
|
|
|
7
7
|
async function getRepo(sourceFile = '') {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const json = await _getPkg(sourceFile)
|
|
9
|
+
if (!json) {
|
|
10
|
+
return {
|
|
11
|
+
repo: '',
|
|
12
|
+
source: '',
|
|
13
|
+
needsConfirm: false,
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const packageRepo = _getRepoFromPkgKup(json)
|
|
18
|
+
if (packageRepo.hasRepoField) {
|
|
19
|
+
return {
|
|
20
|
+
repo: packageRepo.repo,
|
|
21
|
+
source: packageRepo.repo ? 'package' : '',
|
|
22
|
+
needsConfirm: false,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const guessedRepo = _getRepoFromPkgRepo(json)
|
|
27
|
+
return {
|
|
28
|
+
repo: guessedRepo,
|
|
29
|
+
source: guessedRepo ? 'package.repository' : '',
|
|
30
|
+
needsConfirm: !!guessedRepo,
|
|
31
|
+
}
|
|
10
32
|
}
|
|
11
33
|
|
|
12
|
-
|
|
13
|
-
const
|
|
34
|
+
function _getRepoFromPkgKup(json) {
|
|
35
|
+
const hasRepoField = !!json?.kup && Object.prototype.hasOwnProperty.call(json.kup, 'repo')
|
|
14
36
|
const repo = json?.kup?.repo
|
|
15
37
|
const result = validateRepo(repo)
|
|
16
|
-
return
|
|
38
|
+
return {
|
|
39
|
+
repo: result.status ? repo : '',
|
|
40
|
+
hasRepoField,
|
|
41
|
+
}
|
|
17
42
|
}
|
|
18
|
-
|
|
19
|
-
|
|
43
|
+
function _getRepoFromPkgRepo(json) {
|
|
44
|
+
const repository = json?.repository
|
|
45
|
+
if (!repository) return ''
|
|
46
|
+
|
|
47
|
+
if (typeof repository === 'string') {
|
|
48
|
+
return normalizeRepositoryToRepo(repository)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof repository === 'object' && repository.url) {
|
|
52
|
+
return normalizeRepositoryToRepo(repository.url)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return ''
|
|
20
56
|
}
|
|
21
57
|
async function _getRepoFromGit() {
|
|
22
58
|
// TODO
|
|
@@ -45,9 +81,36 @@ async function _getPkg(sourceFile = '') {
|
|
|
45
81
|
return json
|
|
46
82
|
}
|
|
47
83
|
|
|
84
|
+
function normalizeRepositoryToRepo(repository) {
|
|
85
|
+
if (typeof repository !== 'string') return ''
|
|
86
|
+
|
|
87
|
+
const value = repository.trim()
|
|
88
|
+
if (!value) return ''
|
|
89
|
+
|
|
90
|
+
const candidates = [
|
|
91
|
+
/^github:([\w.-]+\/[\w.-]+)$/i,
|
|
92
|
+
/^(?:git\+)?https?:\/\/github\.com\/([\w.-]+\/[\w.-]+?)(?:\.git)?(?:\/)?$/i,
|
|
93
|
+
/^git@github\.com:([\w.-]+\/[\w.-]+?)(?:\.git)?$/i,
|
|
94
|
+
/^ssh:\/\/git@github\.com\/([\w.-]+\/[\w.-]+?)(?:\.git)?(?:\/)?$/i,
|
|
95
|
+
/^([\w.-]+\/[\w.-]+)$/,
|
|
96
|
+
]
|
|
97
|
+
|
|
98
|
+
for (const pattern of candidates) {
|
|
99
|
+
const matched = pattern.exec(value)
|
|
100
|
+
if (!matched) continue
|
|
101
|
+
|
|
102
|
+
const repo = matched[1]
|
|
103
|
+
const result = validateRepo(repo)
|
|
104
|
+
if (result.status) return repo
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return ''
|
|
108
|
+
}
|
|
109
|
+
|
|
48
110
|
export {
|
|
49
111
|
getRepo,
|
|
50
|
-
|
|
51
|
-
|
|
112
|
+
_getRepoFromPkgKup as __getRepoFromPkgKup,
|
|
113
|
+
_getRepoFromPkgRepo as __getRepoFromPkgRepo,
|
|
52
114
|
// __getRepoFromGit: _getRepoFromGit,
|
|
115
|
+
normalizeRepositoryToRepo as __normalizeRepositoryToRepo,
|
|
53
116
|
}
|
package/lib/validate.js
CHANGED