@bratel/dgit 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/nodejs.yml +17 -17
- package/README.en_US.md +20 -22
- package/README.md +25 -26
- package/eslint.config.mjs +15 -0
- package/lib/cmd/action.d.ts +3 -3
- package/lib/cmd/action.js +80 -78
- package/lib/cmd/main.js +17 -15
- package/lib/cmd/prompt.d.ts +4 -4
- package/lib/cmd/prompt.js +82 -76
- package/lib/cmd/utils.d.ts +5 -5
- package/lib/cmd/utils.js +25 -19
- package/lib/dgit.d.ts +2 -2
- package/lib/dgit.js +162 -157
- package/lib/log.d.ts +2 -2
- package/lib/log.js +11 -9
- package/lib/repo.d.ts +1 -1
- package/lib/repo.js +6 -4
- package/lib/request.d.ts +6 -6
- package/lib/request.js +29 -26
- package/lib/type.d.ts +1 -1
- package/package.json +51 -55
- package/renovate.json +19 -0
- package/src/cmd/action.ts +115 -108
- package/src/cmd/main.ts +51 -49
- package/src/cmd/prompt.ts +93 -92
- package/src/cmd/type.ts +27 -27
- package/src/cmd/utils.ts +78 -73
- package/src/dgit.ts +238 -228
- package/src/log.ts +9 -7
- package/src/repo.ts +6 -4
- package/src/request.ts +91 -92
- package/src/type.ts +36 -36
- package/test/dgit.test.ts +122 -119
- package/tsconfig.json +51 -51
- package/tsconfig.tsbuildinfo +1 -0
- package/.eslintignore +0 -6
- package/.eslintrc.js +0 -30
- package/x-npmrc +0 -2
package/lib/cmd/utils.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare
|
|
1
|
+
import type { GithubLinkInfo, PackageInfo } from './type';
|
|
2
|
+
export declare function GetPackageInfo(): PackageInfo;
|
|
3
3
|
export declare const GITHUB_ADDRESS = "https://github.com/";
|
|
4
4
|
export declare const isHttpsLink: (link: string) => boolean;
|
|
5
|
-
export declare
|
|
5
|
+
export declare function ParseGithubHttpsLink(httpsLink: string): GithubLinkInfo;
|
|
6
6
|
export declare const TextEllipsis: (text: string, maxLen: number) => string;
|
|
7
|
-
export declare
|
|
8
|
-
export declare
|
|
7
|
+
export declare function MakeDirs(dirs: string): void;
|
|
8
|
+
export declare function AddExtraRandomQs(origin: string): string;
|
package/lib/cmd/utils.js
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.TextEllipsis = exports.isHttpsLink = exports.GITHUB_ADDRESS = void 0;
|
|
4
|
+
exports.GetPackageInfo = GetPackageInfo;
|
|
5
|
+
exports.ParseGithubHttpsLink = ParseGithubHttpsLink;
|
|
6
|
+
exports.MakeDirs = MakeDirs;
|
|
7
|
+
exports.AddExtraRandomQs = AddExtraRandomQs;
|
|
4
8
|
const tslib_1 = require("tslib");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const buffer =
|
|
9
|
+
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
|
|
10
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
11
|
+
function GetPackageInfo() {
|
|
12
|
+
const buffer = node_fs_1.default.readFileSync(node_path_1.default.resolve(__dirname, '../../package.json'));
|
|
9
13
|
return JSON.parse(buffer.toString());
|
|
10
|
-
}
|
|
14
|
+
}
|
|
11
15
|
exports.GITHUB_ADDRESS = 'https://github.com/';
|
|
12
|
-
|
|
13
|
-
exports.
|
|
16
|
+
const isHttpsLink = (link) => link.trim().startsWith(exports.GITHUB_ADDRESS);
|
|
17
|
+
exports.isHttpsLink = isHttpsLink;
|
|
18
|
+
function ParseGithubHttpsLink(httpsLink) {
|
|
14
19
|
let nextLink = httpsLink.trim().slice(exports.GITHUB_ADDRESS.length);
|
|
15
20
|
let index = nextLink.indexOf('/');
|
|
16
21
|
if (index === -1)
|
|
@@ -62,26 +67,27 @@ exports.ParseGithubHttpsLink = (httpsLink) => {
|
|
|
62
67
|
relativePath,
|
|
63
68
|
type,
|
|
64
69
|
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
exports.
|
|
70
|
+
}
|
|
71
|
+
const TextEllipsis = (text, maxLen) => (text.length >= maxLen ? `${text.slice(0, maxLen)}...` : text);
|
|
72
|
+
exports.TextEllipsis = TextEllipsis;
|
|
73
|
+
function MakeDirs(dirs) {
|
|
68
74
|
const mkdirs = (dir, callback) => {
|
|
69
|
-
if (
|
|
75
|
+
if (node_fs_1.default.existsSync(dir)) {
|
|
70
76
|
callback && callback();
|
|
71
77
|
return;
|
|
72
78
|
}
|
|
73
|
-
mkdirs(
|
|
74
|
-
|
|
79
|
+
mkdirs(node_path_1.default.dirname(dir), () => {
|
|
80
|
+
node_fs_1.default.mkdirSync(dir);
|
|
75
81
|
callback && callback();
|
|
76
82
|
});
|
|
77
83
|
};
|
|
78
|
-
if (
|
|
84
|
+
if (node_fs_1.default.existsSync(dirs))
|
|
79
85
|
return;
|
|
80
86
|
mkdirs(dirs);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (origin.
|
|
87
|
+
}
|
|
88
|
+
function AddExtraRandomQs(origin) {
|
|
89
|
+
if (origin.includes('?')) {
|
|
84
90
|
return `${origin}&_t=${Math.random()}`;
|
|
85
91
|
}
|
|
86
92
|
return `${origin}?_t=${Math.random()}`;
|
|
87
|
-
}
|
|
93
|
+
}
|
package/lib/dgit.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { DgitGlobalOption,
|
|
2
|
-
declare
|
|
1
|
+
import type { DgitGlobalOption, DgitLifeCycle, DgitLoadGitTree, RepoOptionType } from './type';
|
|
2
|
+
declare function dgit(repoOption: RepoOptionType, dPath: string, dgitOptions?: DgitGlobalOption, hooks?: DgitLifeCycle & DgitLoadGitTree): Promise<void>;
|
|
3
3
|
export default dgit;
|
package/lib/dgit.js
CHANGED
|
@@ -1,179 +1,184 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
|
|
5
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
6
|
+
const node_process_1 = tslib_1.__importDefault(require("node:process"));
|
|
6
7
|
const async_1 = tslib_1.__importDefault(require("async"));
|
|
7
|
-
const
|
|
8
|
+
const utils_1 = require("./cmd/utils");
|
|
8
9
|
const log_1 = require("./log");
|
|
10
|
+
const repo_1 = tslib_1.__importDefault(require("./repo"));
|
|
9
11
|
const request_1 = require("./request");
|
|
10
|
-
const utils_1 = require("./cmd/utils");
|
|
11
12
|
const UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36';
|
|
12
13
|
const DEFAULT_PARALLEL_LIMIT = 10;
|
|
13
14
|
const MAX_PARALLEL_LIMIT = 100;
|
|
14
15
|
const JSON_STRINGIFY_PADDING = 2;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
parallelLimit > MAX_PARALLEL_LIMIT && (parallelLimit = MAX_PARALLEL_LIMIT);
|
|
36
|
-
const { onSuccess, onError, onProgress, onFinish, onRetry, onResolved, beforeLoadTree, afterLoadTree, } = hooks || {};
|
|
37
|
-
let onSuccessResolve = () => { };
|
|
38
|
-
let onErrorReject = () => { };
|
|
39
|
-
const prom = new Promise((resolve, reject) => {
|
|
40
|
-
onSuccessResolve = resolve;
|
|
41
|
-
onErrorReject = reject;
|
|
42
|
-
});
|
|
43
|
-
const { getRepoTreeUrl, getDownloadUrl } = repo_1.default(owner, repoName, ref, proxy);
|
|
44
|
-
const url = getRepoTreeUrl();
|
|
45
|
-
const headers = {
|
|
46
|
-
'User-Agent': UserAgent,
|
|
47
|
-
Authorization: token ? `token ${token}` : undefined,
|
|
48
|
-
};
|
|
49
|
-
const auth = username && password ?
|
|
50
|
-
{
|
|
51
|
-
user: username,
|
|
52
|
-
pass: password,
|
|
53
|
-
sendImmediately: true,
|
|
54
|
-
} :
|
|
55
|
-
undefined;
|
|
56
|
-
const options = {
|
|
57
|
-
url, headers, auth,
|
|
58
|
-
};
|
|
59
|
-
const destPath = path_1.default.isAbsolute(dPath) ? dPath : path_1.default.resolve(process.cwd(), dPath);
|
|
60
|
-
logger(' request repo tree options.');
|
|
61
|
-
logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
|
|
62
|
-
try {
|
|
63
|
-
logger(' loading remote repo tree...');
|
|
64
|
-
beforeLoadTree && beforeLoadTree();
|
|
65
|
-
const body = yield request_1.requestGetPromise(options, dgitOptions || {}, {
|
|
66
|
-
onRetry() {
|
|
67
|
-
logger(` request ${url} failed. Retrying...`);
|
|
68
|
-
onRetry && onRetry();
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
logger(' loading remote repo tree succeed.');
|
|
72
|
-
afterLoadTree && afterLoadTree();
|
|
73
|
-
const result = JSON.parse(body);
|
|
74
|
-
if (!result.tree || result.tree.length <= 0) {
|
|
75
|
-
throw new Error('404 repo not found!');
|
|
16
|
+
function dgit(repoOption, dPath, dgitOptions, hooks) {
|
|
17
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const { username, password, token, githubLink, proxy = '', } = repoOption;
|
|
19
|
+
let { owner, repoName, ref = 'master', relativePath = '.', } = repoOption;
|
|
20
|
+
if (githubLink && (0, utils_1.isHttpsLink)(githubLink)) {
|
|
21
|
+
const parseResult = (0, utils_1.ParseGithubHttpsLink)(githubLink);
|
|
22
|
+
owner = parseResult.owner;
|
|
23
|
+
repoName = parseResult.repoName;
|
|
24
|
+
ref = parseResult.ref;
|
|
25
|
+
relativePath = parseResult.relativePath;
|
|
26
|
+
}
|
|
27
|
+
if (!owner || !repoName) {
|
|
28
|
+
throw new Error('invalid repo option.');
|
|
29
|
+
}
|
|
30
|
+
const logger = (0, log_1.createLogger)(dgitOptions);
|
|
31
|
+
const { exclude = [], include = [] } = dgitOptions || {};
|
|
32
|
+
let { parallelLimit = DEFAULT_PARALLEL_LIMIT } = dgitOptions || {};
|
|
33
|
+
if (!parallelLimit || parallelLimit <= 0) {
|
|
34
|
+
logger('parallelLimit value is invalid.');
|
|
35
|
+
parallelLimit = DEFAULT_PARALLEL_LIMIT;
|
|
76
36
|
}
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
37
|
+
parallelLimit > MAX_PARALLEL_LIMIT && (parallelLimit = MAX_PARALLEL_LIMIT);
|
|
38
|
+
const { onSuccess, onError, onProgress, onFinish, onRetry, onResolved, beforeLoadTree, afterLoadTree, } = hooks || {};
|
|
39
|
+
let onSuccessResolve = () => { };
|
|
40
|
+
let onErrorReject = () => { };
|
|
41
|
+
const prom = new Promise((resolve, reject) => {
|
|
42
|
+
onSuccessResolve = resolve;
|
|
43
|
+
onErrorReject = reject;
|
|
44
|
+
});
|
|
45
|
+
const { getRepoTreeUrl, getDownloadUrl } = (0, repo_1.default)(owner, repoName, ref, proxy);
|
|
46
|
+
const url = getRepoTreeUrl();
|
|
47
|
+
const headers = {
|
|
48
|
+
'User-Agent': UserAgent,
|
|
49
|
+
'Authorization': token ? `token ${token}` : undefined,
|
|
50
|
+
};
|
|
51
|
+
const auth = username && password
|
|
52
|
+
? {
|
|
53
|
+
user: username,
|
|
54
|
+
pass: password,
|
|
55
|
+
sendImmediately: true,
|
|
83
56
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
57
|
+
: undefined;
|
|
58
|
+
const options = {
|
|
59
|
+
url,
|
|
60
|
+
headers,
|
|
61
|
+
auth,
|
|
62
|
+
};
|
|
63
|
+
const destPath = node_path_1.default.isAbsolute(dPath) ? dPath : node_path_1.default.resolve(node_process_1.default.cwd(), dPath);
|
|
64
|
+
logger(' request repo tree options.');
|
|
65
|
+
logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
|
|
66
|
+
try {
|
|
67
|
+
logger(' loading remote repo tree...');
|
|
68
|
+
beforeLoadTree && beforeLoadTree();
|
|
69
|
+
const body = yield (0, request_1.requestGetPromise)(options, dgitOptions || {}, {
|
|
70
|
+
onRetry() {
|
|
71
|
+
logger(` request ${url} failed. Retrying...`);
|
|
72
|
+
onRetry && onRetry();
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
logger(' loading remote repo tree succeed.');
|
|
76
|
+
afterLoadTree && afterLoadTree();
|
|
77
|
+
const result = JSON.parse(body);
|
|
78
|
+
if (!result.tree || result.tree.length <= 0) {
|
|
79
|
+
throw new Error('404 repo not found!');
|
|
87
80
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
81
|
+
const treeNodeList = result.tree;
|
|
82
|
+
const includeTreeNodeList = treeNodeList.filter((node) => {
|
|
83
|
+
const nPath = node_path_1.default.resolve(__dirname, node.path);
|
|
84
|
+
const rPath = node_path_1.default.resolve(__dirname, relativePath);
|
|
85
|
+
if (!nPath.startsWith(rPath) || node.type !== 'blob') {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (exclude.some(v => nPath.startsWith(node_path_1.default.resolve(rPath, v)))
|
|
89
|
+
&& include.every(v => !nPath.startsWith(node_path_1.default.resolve(rPath, v)))) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
});
|
|
94
|
+
if (includeTreeNodeList.length <= 0) {
|
|
95
|
+
throw new Error(`404 repo ${relativePath} not found!`);
|
|
97
96
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
97
|
+
const totalStatus = includeTreeNodeList.reduce((prev, cur) => {
|
|
98
|
+
if (cur.type === 'blob') {
|
|
99
|
+
prev.size += cur.size;
|
|
100
|
+
prev.count++;
|
|
101
|
+
}
|
|
102
|
+
return prev;
|
|
103
|
+
}, { size: 0, count: 0 });
|
|
104
|
+
let currentSize = 0;
|
|
105
|
+
let currentCount = 0;
|
|
106
|
+
onResolved
|
|
107
|
+
&& onResolved({
|
|
108
|
+
currentSize,
|
|
109
|
+
currentCount,
|
|
110
|
+
totalSize: totalStatus.size,
|
|
111
|
+
totalCount: totalStatus.count,
|
|
112
|
+
});
|
|
113
|
+
logger(' include files resolved.');
|
|
114
|
+
logger('', JSON.stringify({
|
|
104
115
|
currentSize,
|
|
105
116
|
currentCount,
|
|
106
117
|
totalSize: totalStatus.size,
|
|
107
118
|
totalCount: totalStatus.count,
|
|
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
|
-
const ws = fs_1.default.createWriteStream(targetPath);
|
|
133
|
-
logger(` downloading from ${downloadUrl}...`);
|
|
134
|
-
request_1.requestOnStream(downloadUrl, ws, dgitOptions || {}, {
|
|
135
|
-
onSuccess() {
|
|
136
|
-
currentCount++;
|
|
137
|
-
currentSize += node.size;
|
|
138
|
-
logger(` write file ${node.path} succeed.
|
|
119
|
+
}));
|
|
120
|
+
async_1.default.eachLimit(includeTreeNodeList, parallelLimit, (node, callback) => {
|
|
121
|
+
const downloadUrl = getDownloadUrl(node.path);
|
|
122
|
+
const rPath = node_path_1.default.resolve(destPath, relativePath);
|
|
123
|
+
const tPath = node_path_1.default.resolve(destPath, node.path);
|
|
124
|
+
const root = node_path_1.default.resolve(destPath, '.');
|
|
125
|
+
let targetPath;
|
|
126
|
+
if (rPath === tPath) {
|
|
127
|
+
targetPath = node_path_1.default.resolve(destPath, node_path_1.default.basename(tPath));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
targetPath = tPath.replace(rPath, root);
|
|
131
|
+
}
|
|
132
|
+
logger('', node.path, relativePath, targetPath);
|
|
133
|
+
if (!node_fs_1.default.existsSync(node_path_1.default.dirname(targetPath))) {
|
|
134
|
+
(0, utils_1.MakeDirs)(node_path_1.default.dirname(targetPath));
|
|
135
|
+
}
|
|
136
|
+
const ws = node_fs_1.default.createWriteStream(targetPath);
|
|
137
|
+
logger(` downloading from ${downloadUrl}...`);
|
|
138
|
+
(0, request_1.requestOnStream)(downloadUrl, ws, dgitOptions || {}, {
|
|
139
|
+
onSuccess() {
|
|
140
|
+
currentCount++;
|
|
141
|
+
currentSize += node.size;
|
|
142
|
+
logger(` write file ${node.path} succeed.
|
|
139
143
|
size: [${currentSize}/${totalStatus.size}],
|
|
140
144
|
count: [${currentCount}/${totalStatus.count}]`);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
145
|
+
onProgress
|
|
146
|
+
&& onProgress({
|
|
147
|
+
totalCount: totalStatus.count,
|
|
148
|
+
totalSize: totalStatus.size,
|
|
149
|
+
currentSize,
|
|
150
|
+
currentCount,
|
|
151
|
+
}, node);
|
|
152
|
+
callback();
|
|
153
|
+
},
|
|
154
|
+
onError(err) {
|
|
155
|
+
logger('', err);
|
|
156
|
+
callback(new Error(` request ${downloadUrl} failed.`));
|
|
157
|
+
},
|
|
158
|
+
onRetry() {
|
|
159
|
+
logger(` request ${downloadUrl} failed. Retrying...`);
|
|
160
|
+
onRetry && onRetry();
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}, (err) => {
|
|
164
|
+
if (err) {
|
|
165
|
+
onError && onError(err);
|
|
166
|
+
onFinish && onFinish();
|
|
167
|
+
onErrorReject(err);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
onSuccess && onSuccess();
|
|
171
|
+
onFinish && onFinish();
|
|
172
|
+
onSuccessResolve();
|
|
173
|
+
}
|
|
158
174
|
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
onSuccessResolve();
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
catch (error) {
|
|
173
|
-
onError && onError(error);
|
|
174
|
-
onFinish && onFinish();
|
|
175
|
-
onErrorReject(error);
|
|
176
|
-
}
|
|
177
|
-
return prom;
|
|
178
|
-
});
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
onError && onError(error);
|
|
178
|
+
onFinish && onFinish();
|
|
179
|
+
onErrorReject(error);
|
|
180
|
+
}
|
|
181
|
+
return prom;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
179
184
|
exports.default = dgit;
|
package/lib/log.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DgitGlobalOption } from './type';
|
|
2
|
-
export declare
|
|
1
|
+
import type { DgitGlobalOption } from './type';
|
|
2
|
+
export declare function createLogger(option?: DgitGlobalOption): (...message: any[]) => void;
|
package/lib/log.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLogger =
|
|
3
|
+
exports.createLogger = createLogger;
|
|
4
4
|
const DEFAULT_PREFIX = '[dgit-logger]';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
function createLogger(option) {
|
|
6
|
+
return (...message) => {
|
|
7
|
+
if (option && option.log) {
|
|
8
|
+
const prefix = option
|
|
9
|
+
? option.logPrefix || DEFAULT_PREFIX
|
|
10
|
+
: DEFAULT_PREFIX;
|
|
11
|
+
console.log(prefix, ...message, '\n');
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
package/lib/repo.d.ts
CHANGED
package/lib/repo.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
function repoUtils(owner, repoName, ref, proxy) {
|
|
4
|
+
return {
|
|
5
|
+
getRepoTreeUrl: () => `https://api.github.com/repos/${owner}/${repoName}/git/trees/${ref}?recursive=1`,
|
|
6
|
+
getDownloadUrl: (path) => `${proxy ? `${proxy}/` : ''}https://raw.githubusercontent.com/${owner}/${repoName}/${ref}/${path}`,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
7
9
|
exports.default = repoUtils;
|
package/lib/request.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import {
|
|
3
|
-
import { DgitGlobalOption, DgitLifeCycle } from './type';
|
|
4
|
-
|
|
5
|
-
export declare
|
|
6
|
-
export declare
|
|
1
|
+
import type fs from 'node:fs';
|
|
2
|
+
import type { CoreOptions, UrlOptions } from 'request';
|
|
3
|
+
import type { DgitGlobalOption, DgitLifeCycle } from './type';
|
|
4
|
+
type RequestOption = UrlOptions & CoreOptions;
|
|
5
|
+
export declare function requestGetPromise(options: RequestOption, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any>;
|
|
6
|
+
export declare function requestOnStream(url: string, ws: fs.WriteStream, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): void;
|
|
7
7
|
export {};
|
package/lib/request.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.requestGetPromise = requestGetPromise;
|
|
4
|
+
exports.requestOnStream = requestOnStream;
|
|
4
5
|
const tslib_1 = require("tslib");
|
|
5
6
|
const request_1 = tslib_1.__importDefault(require("request"));
|
|
6
7
|
const utils_1 = require("./cmd/utils");
|
|
7
8
|
const log_1 = require("./log");
|
|
8
9
|
const REQUEST_RETRY_DELAY = 1500;
|
|
9
10
|
const DEFAULT_MAX_RETRY_COUNT = 5;
|
|
10
|
-
|
|
11
|
+
function requestGet(options, maxRetryCount, hooks) {
|
|
11
12
|
const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
|
|
12
13
|
request_1.default.get(options, (err, _, body) => {
|
|
13
14
|
if (err) {
|
|
@@ -25,33 +26,35 @@ const requestGet = (options, maxRetryCount, hooks) => {
|
|
|
25
26
|
onSuccess && onSuccess(body);
|
|
26
27
|
onFinish && onFinish();
|
|
27
28
|
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
}
|
|
30
|
+
function requestGetPromise(options, dgitOptions, hooks) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
|
|
33
|
+
const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
|
|
34
|
+
const newHooks = {
|
|
35
|
+
onSuccess(data) {
|
|
36
|
+
resolve(data);
|
|
37
|
+
onSuccess && onSuccess(data);
|
|
38
|
+
},
|
|
39
|
+
onError(err) {
|
|
40
|
+
reject(err);
|
|
41
|
+
onError && onError(err);
|
|
42
|
+
},
|
|
43
|
+
onFinish,
|
|
44
|
+
onRetry,
|
|
45
|
+
};
|
|
46
|
+
requestGet(options, maxRetryCount, newHooks);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function requestOnStream(url, ws, dgitOptions, hooks) {
|
|
47
50
|
const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
|
|
48
|
-
const logger = log_1.createLogger(dgitOptions);
|
|
51
|
+
const logger = (0, log_1.createLogger)(dgitOptions);
|
|
49
52
|
const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
|
|
50
53
|
const fn = (retryCount) => {
|
|
51
|
-
const downloadUrl = utils_1.AddExtraRandomQs(url);
|
|
54
|
+
const downloadUrl = (0, utils_1.AddExtraRandomQs)(url);
|
|
52
55
|
logger(` dowloading from ${downloadUrl}...`);
|
|
53
|
-
request_1.default(encodeURI(downloadUrl))
|
|
54
|
-
.on('error', err => {
|
|
56
|
+
(0, request_1.default)(encodeURI(downloadUrl))
|
|
57
|
+
.on('error', (err) => {
|
|
55
58
|
if (retryCount <= 0) {
|
|
56
59
|
onError && onError(err);
|
|
57
60
|
onFinish && onFinish();
|
|
@@ -72,4 +75,4 @@ exports.requestOnStream = (url, ws, dgitOptions, hooks) => {
|
|
|
72
75
|
logger(` ${url}, write stream failed.`);
|
|
73
76
|
});
|
|
74
77
|
fn(maxRetryCount);
|
|
75
|
-
}
|
|
78
|
+
}
|
package/lib/type.d.ts
CHANGED