forked from dhis2/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisGitTemplateSpecifier.js
More file actions
162 lines (137 loc) · 4.38 KB
/
isGitTemplateSpecifier.js
File metadata and controls
162 lines (137 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const githubHosts = new Set(['github.com', 'www.github.com'])
const ownerPattern = /^[a-zA-Z0-9_.-]+$/
const parseRef = (rawTemplateSource, refPart) => {
if (refPart === undefined) {
return null
}
if (!refPart) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Ref cannot be empty after "#".`
)
}
if (refPart.includes(':')) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use "owner/repo" or "owner/repo#ref".`
)
}
return refPart
}
const parseGithubUrlSource = (sourceWithoutRef) => {
const parsedUrl = new URL(sourceWithoutRef)
if (!githubHosts.has(parsedUrl.host)) {
throw new Error(
`Unsupported template host "${parsedUrl.host}". Only github.com repositories are supported.`
)
}
const pathParts = parsedUrl.pathname.split('/').filter(Boolean).slice(0, 2)
if (pathParts.length < 2) {
throw new Error(
`Invalid GitHub repository path in "${sourceWithoutRef}". Use "owner/repo".`
)
}
return {
owner: pathParts[0],
repo: pathParts[1],
}
}
const parseGithubShorthandSource = (rawTemplateSource, sourceWithoutRef) => {
const separatorIndex = sourceWithoutRef.indexOf('/')
const hasSingleSeparator =
separatorIndex > 0 &&
separatorIndex === sourceWithoutRef.lastIndexOf('/')
if (!hasSingleSeparator) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use "owner/repo" or "owner/repo#ref".`
)
}
const owner = sourceWithoutRef.slice(0, separatorIndex)
const repo = sourceWithoutRef.slice(separatorIndex + 1)
if (
!ownerPattern.test(owner) ||
!repo ||
/\s/.test(repo) ||
repo.includes('/')
) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use "owner/repo" or "owner/repo#ref".`
)
}
return {
owner,
repo,
}
}
const isValidRefPart = (refPart) => {
if (refPart === undefined) {
return true
}
return Boolean(refPart) && !refPart.includes(':')
}
const isValidShorthandSource = (sourceWithoutRef) => {
const separatorIndex = sourceWithoutRef.indexOf('/')
const hasSingleSeparator =
separatorIndex > 0 &&
separatorIndex === sourceWithoutRef.lastIndexOf('/')
if (!hasSingleSeparator) {
return false
}
const owner = sourceWithoutRef.slice(0, separatorIndex)
const repo = sourceWithoutRef.slice(separatorIndex + 1)
return (
ownerPattern.test(owner) &&
Boolean(repo) &&
!/\s/.test(repo) &&
!repo.includes('/')
)
}
const parseGitTemplateSpecifier = (templateSource) => {
const rawTemplateSource = String(templateSource || '').trim()
if (!rawTemplateSource) {
throw new Error('Template source cannot be empty.')
}
const [sourceWithoutRef, refPart, ...rest] = rawTemplateSource.split('#')
if (rest.length > 0) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use at most one "#" to specify a ref.`
)
}
const ref = parseRef(rawTemplateSource, refPart)
const sourceInfo = sourceWithoutRef.startsWith('https://')
? parseGithubUrlSource(sourceWithoutRef)
: parseGithubShorthandSource(rawTemplateSource, sourceWithoutRef)
const owner = sourceInfo.owner
let repo = sourceInfo.repo
if (repo.endsWith('.git')) {
repo = repo.slice(0, -4)
}
if (!owner || !repo) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Missing GitHub owner or repository name.`
)
}
return {
owner,
repo,
ref,
repoUrl: `https://github.com/${owner}/${repo}.git`,
raw: rawTemplateSource,
}
}
const isGitTemplateSpecifier = (templateSource) => {
const rawTemplateSource = String(templateSource || '').trim()
if (!rawTemplateSource) {
return false
}
if (rawTemplateSource.startsWith('https://')) {
return true
}
const [sourceWithoutRef, refPart, ...rest] = rawTemplateSource.split('#')
if (rest.length > 0 || !isValidRefPart(refPart)) {
return false
}
return isValidShorthandSource(sourceWithoutRef)
}
module.exports = {
isGitTemplateSpecifier,
parseGitTemplateSpecifier,
}