-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
85 lines (75 loc) · 1.97 KB
/
script.js
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
import { getFileContent } from "./get-file-content.js";
import { createRepositoryFromFolder } from "./create-repository-from-folder.js";
/**
* Updates branch protection based on a settings from a template repository
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {object} options
* @param {string} options.pathToFolders
*/
export async function script(octokit, repository, { pathToFolders }) {
if (!pathToFolders) {
throw new Error(`--path-to-folders is required`);
}
const owner = repository.owner.login;
const repo = repository.name;
try {
const { data } = await octokit.request(
"GET /repos/{owner}/{repo}/contents/{path}",
{
owner,
repo,
path: pathToFolders,
}
);
if (!Array.isArray(data)) {
octokit.info(
"'%s' is not a folder in %s. It's a %s",
pathToFolders,
repository.full_name,
data.type
);
return;
}
let pkg;
let pkgLock;
for (const item of data) {
if (item.type !== "dir") {
octokit.info(
"'%s/%s' is not a folder in %s. It's a %s",
pathToFolders,
item.name,
repository.full_name,
item.type
);
continue;
}
pkg = pkg || (await getFileContent(octokit, owner, repo, "package.json"));
pkgLock =
pkgLock ||
(await getFileContent(octokit, owner, repo, "package-lock.json"));
const readme = await getFileContent(
octokit,
owner,
repo,
item.path + "/README.md"
);
await createRepositoryFromFolder(octokit, repository, item.path, {
readme,
package: pkg,
packageLock: pkgLock,
});
}
} catch (error) {
if (error.status === 404) {
octokit.info(
"'%s' does not exist in %s",
pathToFolders,
repository.full_name
);
return;
}
throw error;
}
}