-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdocusaurus-lib-list-remote.js
More file actions
130 lines (112 loc) · 4.6 KB
/
docusaurus-lib-list-remote.js
File metadata and controls
130 lines (112 loc) · 4.6 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
import { request } from "@octokit/request";
import { minimatch } from "minimatch";
// const octokit = new Octokit({
// // userAgent: "docusaurus-lib-list-remote" // TODO customize per usage?!
// });
const listRemote = {
minimatchOpts: {
matchBase: true,
nonegate: true, // no need to support negation i.e. only positive files matching
},
OCTOKIT_TREE_FILE_TYPE: 'blob',
/**
* Create GitHub repository object of `https://github.com/<repoOwner>/<repoName>`
* with a given `primaryBranch` (which can also be a `tree sha`).
*
* Example for https://github.com/xmtp-labs/web-starter:
* - repoOwner: xmtp-labs
* - repoName: web-starter
* - primaryBranch: main
* pointing to https://github.com/xmtp-labs/web-starter/tree/main
*
* Note that tree sha `f4d4f39a575dbd03df37941391f8b088f6cf48aa` could also be used as `primaryBranch` value
* to reference to https://github.com/xmtp-labs/web-starter/tree/f4d4f39a575dbd03df37941391f8b088f6cf48aa.
*
* @param {String} repoOwner Repository object created with `createRepo`.
* @param {String} repoName Array of filters for files to be included.
* @param {String} primaryBranch Array of filters for files to be excluded.
*/
createRepo: (repoOwner, repoName, primaryBranch) => {
return {
'owner': repoOwner,
'name': repoName,
'branch': primaryBranch
}
},
/**
* Helper to builds `sourceBaseUrl` value for `docusaurus-plugin-remote-content` plugin from `repo` object.
*
* @param {object} repo Repository object created with `createRepo`.
*/
buildRepoRawBaseUrl: (repo) => {
return `https://raw.githubusercontent.com/${repo.owner}/${repo.name}/${repo.branch}`
},
/**
* Lists files in repository `repo` that match `includeFilters` filter but excluding those matching `excludeFilters` filters.
* It uses [minimatch](https://github.com/isaacs/minimatch) for filters syntax.
*
* Example:
* List all *.md files but those matching `internal/docs*.md` (note that the include filter below had to be escaped with \)
*
* listDocuments(repo, ['**\/*.md'], ['internal/docs*.md'])
*
* @param {object} repo Repository object created with `createRepo`.
* @param {String[]} [includeFilters] Array of filters for files to be included.
* @param {String[]} [excludeFilters] (Optional) Array of filters for files to be excluded.
*/
listDocuments: function (repo, includeFilters, excludeFilters = []) {
const req = 'GET /repos/{owner}/{repo}/git/trees/{tree_sha}?recursive=1'
return request(req, {
owner: repo.owner,
repo: repo.name,
tree_sha: repo.branch
}).then(repoTreeResponse => {
console.log(this)
const repoFilePaths = this.extractFilesFromTree(repoTreeResponse.data.tree);
console.log(`\nRetrieved all file paths: ${repoFilePaths}`)
const resultingFilePaths = this.applyFilters(repoFilePaths, includeFilters, excludeFilters)
console.log(`\nResulting file paths: ${resultingFilePaths}`)
return resultingFilePaths
})
},
extractFilesFromTree: function (treeElements) {
console.log("\nExtracting files from tree elements")
return treeElements
.filter(treeElement => treeElement.type === this.OCTOKIT_TREE_FILE_TYPE) // files only
.map(treeElement => treeElement.path)
},
applyFilters: function (paths, includeFilters, excludeFilters) {
console.log(`\nApplying include filters (${includeFilters}) to (${paths})`)
const pathsFilteredIncluded = this.applyIncludeFilters(paths, includeFilters)
console.log(`\nApplying exclude filters (${excludeFilters}) to (${pathsFilteredIncluded})`)
return this.applyExcludeFilters(pathsFilteredIncluded, excludeFilters)
},
applyIncludeFilters: function (paths, filters) {
const unique = function (a) {
return Array.from(new Set(a));
}
const pathsFilteredArray = filters.map(filter => {
return minimatch.match(paths, filter, this.minimatchOpts)
})
let pathsFilteredFlattened = [].concat(...pathsFilteredArray)
return unique(pathsFilteredFlattened)
},
applyExcludeFilters: function (paths, excludeFilters) {
const excludedPathsArray = excludeFilters.map(excludeFilter => {
return minimatch.match(paths, excludeFilter, minimatchOpts)
})
const excludedPaths = [].concat(...excludedPathsArray)
return paths.filter(p => !excludedPaths.includes(p))
},
}
// module.exports = {
// createRepo,
// buildRepoRawBaseUrl,
// listDocuments,
// // NOTE exposed for tests only
// extractFilesFromTree,
// applyFilters,
// applyIncludeFilters,
// applyExcludeFilters,
// };
export default listRemote