-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathisGitTemplateSpecifier.js
More file actions
125 lines (105 loc) · 3.54 KB
/
isGitTemplateSpecifier.js
File metadata and controls
125 lines (105 loc) · 3.54 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
const githubHosts = new Set(['github.com', 'www.github.com'])
const shorthandPattern = /^([a-zA-Z0-9_.-]+)\/([^\s/]+)$/
const parseRefAndSubdir = (rawTemplateSource, refAndSubdir) => {
if (refAndSubdir === undefined) {
return { ref: null, subdir: null }
}
if (!refAndSubdir) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Ref cannot be empty after "#".`
)
}
const [parsedRef, ...subdirParts] = refAndSubdir.split(':')
const ref = parsedRef || null
const subdir = subdirParts.length > 0 ? subdirParts.join(':') : null
if (!ref) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Ref cannot be empty after "#".`
)
}
if (subdir !== null && !subdir.trim()) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Subdirectory cannot be empty after ":".`
)
}
return { ref, subdir }
}
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 match = sourceWithoutRef.match(shorthandPattern)
if (!match) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use "owner/repo", "owner/repo#ref", or "owner/repo#ref:subdir".`
)
}
return {
owner: match[1],
repo: match[2],
}
}
const parseGitTemplateSpecifier = (templateSource) => {
const rawTemplateSource = String(templateSource || '').trim()
if (!rawTemplateSource) {
throw new Error('Template source cannot be empty.')
}
const [sourceWithoutRef, refAndSubdir, ...rest] =
rawTemplateSource.split('#')
if (rest.length > 0) {
throw new Error(
`Invalid template source "${rawTemplateSource}". Use at most one "#" to specify a ref.`
)
}
const { ref, subdir } = parseRefAndSubdir(rawTemplateSource, refAndSubdir)
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,
subdir,
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
}
return /^[a-zA-Z0-9_.-]+\/[^\s/]+(?:#.+)?$/.test(rawTemplateSource)
}
module.exports = {
isGitTemplateSpecifier,
parseGitTemplateSpecifier,
}