@terencesun/githubdb
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -51,2 +51,3 @@ "use strict"; | ||
path: options.path, | ||
branch: options.branch, | ||
log: { | ||
@@ -53,0 +54,0 @@ debug: function () { }, |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -47,22 +58,37 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
this.path = options.path; | ||
this.branch = options.branch ? options.branch : null; | ||
} | ||
Github.prototype.connect = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4, this.isRepoExist()]; | ||
case 1: | ||
if (!!(_a.sent())) return [3, 3]; | ||
if (!!(_b.sent())) return [3, 3]; | ||
return [4, this.createRepo()]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
_b.sent(); | ||
_b.label = 3; | ||
case 3: return [4, this.isRepoPathExist()]; | ||
case 4: | ||
if (!!(_a.sent())) return [3, 6]; | ||
if (!!(_b.sent())) return [3, 6]; | ||
return [4, this.createPath()]; | ||
case 5: | ||
_a.sent(); | ||
_a.label = 6; | ||
case 6: return [2]; | ||
_b.sent(); | ||
_b.label = 6; | ||
case 6: | ||
_a = this.branch; | ||
if (!_a) return [3, 8]; | ||
return [4, this.isBranchExist()]; | ||
case 7: | ||
_a = !(_b.sent()); | ||
_b.label = 8; | ||
case 8: | ||
if (!_a) return [3, 10]; | ||
return [4, this.createBranch()]; | ||
case 9: | ||
_b.sent(); | ||
_b.label = 10; | ||
case 10: return [2]; | ||
} | ||
@@ -102,3 +128,3 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var e_2; | ||
var refObj, e_2; | ||
return __generator(this, function (_a) { | ||
@@ -108,7 +134,6 @@ switch (_a.label) { | ||
_a.trys.push([0, 2, , 3]); | ||
return [4, this.octokit.rest.repos.getContent({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
path: this.path, | ||
})]; | ||
refObj = {}; | ||
if (this.branch) | ||
refObj["ref"] = "refs/heads/".concat(this.branch); | ||
return [4, this.octokit.rest.repos.getContent(__assign({ owner: this.owner, repo: this.repo, path: this.path }, refObj))]; | ||
case 1: | ||
@@ -131,2 +156,20 @@ _a.sent(); | ||
}; | ||
Github.prototype.isBranchExist = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var branches, findRet; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.octokit.repos.listBranches({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
})]; | ||
case 1: | ||
branches = (_a.sent()).data; | ||
findRet = branches.find(function (x) { return x.name === _this.branch; }); | ||
return [2, findRet ? true : false]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Github.prototype.checkCreate = function () { | ||
@@ -139,7 +182,7 @@ var _this = this; | ||
timmer = setInterval(function () { return __awaiter(_this, void 0, void 0, function () { | ||
var repoRet, repoPathRet; | ||
var repoRet, repoPathRet, branchRet; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!lock) return [3, 3]; | ||
if (!!lock) return [3, 4]; | ||
lock = true; | ||
@@ -153,3 +196,6 @@ count += 1; | ||
repoPathRet = _a.sent(); | ||
if (repoRet && repoPathRet) { | ||
return [4, this.isBranchExist()]; | ||
case 3: | ||
branchRet = _a.sent(); | ||
if (repoRet && repoPathRet && branchRet) { | ||
clearInterval(timmer); | ||
@@ -159,4 +205,4 @@ resolve(true); | ||
lock = false; | ||
_a.label = 3; | ||
case 3: | ||
_a.label = 4; | ||
case 4: | ||
if (count > 5) { | ||
@@ -189,12 +235,45 @@ reject(false); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var branchInfo; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.octokit.rest.repos.createOrUpdateFileContents({ | ||
case 0: | ||
branchInfo = {}; | ||
if (this.branch) | ||
branchInfo["branch"] = this.branch; | ||
return [4, this.octokit.rest.repos.createOrUpdateFileContents(__assign(__assign({ owner: this.owner, repo: this.repo, path: this.path }, branchInfo), { message: "create path file", content: "" }))]; | ||
case 1: | ||
_a.sent(); | ||
return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Github.prototype.createBranch = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var branchData, defaultBranch, branchInfo, sha, ref; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.octokit.repos.get({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
path: this.path, | ||
message: "create path file", | ||
content: "", | ||
})]; | ||
case 1: | ||
branchData = (_a.sent()).data; | ||
defaultBranch = branchData.default_branch; | ||
return [4, this.octokit.repos.getBranch({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
branch: defaultBranch | ||
})]; | ||
case 2: | ||
branchInfo = (_a.sent()).data; | ||
sha = branchInfo.commit.sha; | ||
ref = "refs/heads/" + this.branch; | ||
return [4, this.octokit.git.createRef({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
ref: ref, | ||
sha: sha, | ||
})]; | ||
case 3: | ||
_a.sent(); | ||
@@ -208,10 +287,10 @@ return [2]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var ctxRaw, data, ctx; | ||
var refObj, ctxRaw, data, ctx; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.octokit.rest.repos.getContent({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
path: this.path, | ||
})]; | ||
case 0: | ||
refObj = {}; | ||
if (this.branch) | ||
refObj["ref"] = "refs/heads/".concat(this.branch); | ||
return [4, this.octokit.rest.repos.getContent(__assign({ owner: this.owner, repo: this.repo, path: this.path }, refObj))]; | ||
case 1: | ||
@@ -232,13 +311,10 @@ ctxRaw = _a.sent(); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var updateInfo; | ||
var branchInfo, updateInfo; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: return [4, this.octokit.repos.createOrUpdateFileContents({ | ||
owner: this.owner, | ||
repo: this.repo, | ||
path: this.path, | ||
message: "".concat(this.path, " file update at ").concat(new Date().toUTCString()), | ||
content: new Buffer(ctx).toString("base64"), | ||
sha: sha, | ||
})]; | ||
case 0: | ||
branchInfo = {}; | ||
if (this.branch) | ||
branchInfo["branch"] = this.branch; | ||
return [4, this.octokit.repos.createOrUpdateFileContents(__assign(__assign({ owner: this.owner, repo: this.repo, path: this.path }, branchInfo), { message: "".concat(this.path, " file update at ").concat(new Date().toUTCString()), content: new Buffer(ctx).toString("base64"), sha: sha }))]; | ||
case 1: | ||
@@ -245,0 +321,0 @@ updateInfo = _d.sent(); |
@@ -7,2 +7,3 @@ import type { GithubOptions, GithubRepoPathCtx, GithubRepoUpdateInfo, GithubInter } from "./interface"; | ||
private path; | ||
private branch; | ||
constructor(options: GithubOptions); | ||
@@ -12,7 +13,9 @@ connect(): Promise<void>; | ||
isRepoPathExist(): Promise<boolean>; | ||
isBranchExist(): Promise<boolean>; | ||
checkCreate(): Promise<boolean>; | ||
createRepo(): Promise<void>; | ||
createPath(): Promise<void>; | ||
createBranch(): Promise<void>; | ||
getRepoFileCtx(): Promise<GithubRepoPathCtx>; | ||
updateFile(ctx: string, sha: string): Promise<GithubRepoUpdateInfo>; | ||
} |
@@ -6,2 +6,3 @@ import type { OctokitOptions } from "@octokit/core/dist-types/types"; | ||
path: string; | ||
branch?: string; | ||
} | ||
@@ -26,2 +27,3 @@ export interface GithubRepoPathCtx { | ||
path: string; | ||
branch?: string; | ||
} | ||
@@ -28,0 +30,0 @@ export type DB_INSERTONE = (doc: object) => Promise<object>; |
{ | ||
"name": "@terencesun/githubdb", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "operate with github like mongodb for nodejs", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42728
682