@bratel/dgit 0.0.13 → 0.0.15

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/lib/cmd/prompt.js CHANGED
@@ -1,86 +1,92 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PasswordPrompt = exports.DownloadPrompt = exports.CreatePrompt = void 0;
3
+ exports.CreatePrompt = void 0;
4
+ exports.DownloadPrompt = DownloadPrompt;
5
+ exports.PasswordPrompt = PasswordPrompt;
4
6
  const tslib_1 = require("tslib");
5
7
  const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
6
- exports.CreatePrompt = (questions) => inquirer_1.default.prompt(questions);
7
- exports.DownloadPrompt = (currentInfo) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
8
- if (currentInfo.owner &&
9
- currentInfo.repoName &&
10
- currentInfo.ref &&
11
- currentInfo.relativePath &&
12
- currentInfo.dest)
13
- return currentInfo;
14
- const questions = [
15
- {
16
- type: 'input',
17
- name: 'owner',
18
- when() {
19
- return !currentInfo.owner;
8
+ const CreatePrompt = (questions) => inquirer_1.default.prompt(questions);
9
+ exports.CreatePrompt = CreatePrompt;
10
+ function DownloadPrompt(currentInfo) {
11
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
12
+ if (currentInfo.owner
13
+ && currentInfo.repoName
14
+ && currentInfo.ref
15
+ && currentInfo.relativePath
16
+ && currentInfo.dest) {
17
+ return currentInfo;
18
+ }
19
+ const questions = [
20
+ {
21
+ type: 'input',
22
+ name: 'owner',
23
+ when() {
24
+ return !currentInfo.owner;
25
+ },
26
+ validate(input) {
27
+ return input && input.length > 0;
28
+ },
29
+ message: 'input github ownername.',
20
30
  },
21
- validate(input) {
22
- return input && input.length > 0;
31
+ {
32
+ type: 'input',
33
+ name: 'repoName',
34
+ when() {
35
+ return !currentInfo.repoName;
36
+ },
37
+ validate(input) {
38
+ return input && input.length > 0;
39
+ },
40
+ message: 'input github repoName.',
23
41
  },
24
- message: 'input github ownername.',
25
- },
26
- {
27
- type: 'input',
28
- name: 'repoName',
29
- when() {
30
- return !currentInfo.repoName;
31
- },
32
- validate(input) {
33
- return input && input.length > 0;
34
- },
35
- message: 'input github repoName.',
36
- },
37
- {
38
- type: 'input',
39
- name: 'ref',
40
- when() {
41
- return !currentInfo.ref;
42
- },
43
- validate(input) {
44
- return input && input.length > 0;
42
+ {
43
+ type: 'input',
44
+ name: 'ref',
45
+ when() {
46
+ return !currentInfo.ref;
47
+ },
48
+ validate(input) {
49
+ return input && input.length > 0;
50
+ },
51
+ default: 'master',
52
+ message: 'input github branch or commit hash or tagname.',
45
53
  },
46
- 'default': 'master',
47
- message: 'input github branch or commit hash or tagname.',
48
- },
49
- {
50
- type: 'input',
51
- name: 'relativePath',
52
- when() {
53
- return !currentInfo.relativePath;
54
+ {
55
+ type: 'input',
56
+ name: 'relativePath',
57
+ when() {
58
+ return !currentInfo.relativePath;
59
+ },
60
+ validate(input) {
61
+ return input && input.length > 0;
62
+ },
63
+ default: '.',
64
+ message: 'input github relative path.',
54
65
  },
55
- validate(input) {
56
- return input && input.length > 0;
66
+ {
67
+ type: 'input',
68
+ name: 'dest',
69
+ when() {
70
+ return !currentInfo.dest;
71
+ },
72
+ validate(input) {
73
+ return input && input.length > 0;
74
+ },
75
+ default: '.',
76
+ message: 'input template output dest path.',
57
77
  },
58
- 'default': '.',
59
- message: 'input github relative path.',
60
- },
61
- {
62
- type: 'input',
63
- name: 'dest',
64
- when() {
65
- return !currentInfo.dest;
66
- },
67
- validate(input) {
68
- return input && input.length > 0;
69
- },
70
- 'default': '.',
71
- message: 'input template output dest path.',
72
- },
73
- ];
74
- const answer = yield exports.CreatePrompt(questions);
75
- return {
76
- owner: answer.owner || currentInfo.owner,
77
- dest: answer.dest || currentInfo.dest,
78
- repoName: answer.repoName || currentInfo.repoName,
79
- relativePath: answer.relativePath || currentInfo.relativePath,
80
- ref: answer.ref || currentInfo.ref,
81
- };
82
- });
83
- exports.PasswordPrompt = () => {
78
+ ];
79
+ const answer = yield (0, exports.CreatePrompt)(questions);
80
+ return {
81
+ owner: answer.owner || currentInfo.owner,
82
+ dest: answer.dest || currentInfo.dest,
83
+ repoName: answer.repoName || currentInfo.repoName,
84
+ relativePath: answer.relativePath || currentInfo.relativePath,
85
+ ref: answer.ref || currentInfo.ref,
86
+ };
87
+ });
88
+ }
89
+ function PasswordPrompt() {
84
90
  const question = {
85
91
  type: 'password',
86
92
  name: 'password',
@@ -89,5 +95,5 @@ exports.PasswordPrompt = () => {
89
95
  },
90
96
  message: 'input github account password.',
91
97
  };
92
- return exports.CreatePrompt([question]);
93
- };
98
+ return (0, exports.CreatePrompt)([question]);
99
+ }
package/lib/cmd/type.d.ts CHANGED
@@ -17,6 +17,7 @@ export interface CommandInfo {
17
17
  log?: boolean;
18
18
  logPrefix?: string;
19
19
  proxy?: string;
20
+ exactMatch?: boolean;
20
21
  }
21
22
  export interface DownloadPromptInfo {
22
23
  dest: string;
@@ -1,8 +1,8 @@
1
- import { PackageInfo, GithubLinkInfo } from './type';
2
- export declare const GetPackageInfo: () => PackageInfo;
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 const ParseGithubHttpsLink: (httpsLink: string) => GithubLinkInfo;
5
+ export declare function ParseGithubHttpsLink(httpsLink: string): GithubLinkInfo;
6
6
  export declare const TextEllipsis: (text: string, maxLen: number) => string;
7
- export declare const MakeDirs: (dirs: string) => void;
8
- export declare const AddExtraRandomQs: (origin: string) => string;
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.AddExtraRandomQs = exports.MakeDirs = exports.TextEllipsis = exports.ParseGithubHttpsLink = exports.isHttpsLink = exports.GITHUB_ADDRESS = exports.GetPackageInfo = void 0;
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 fs_1 = tslib_1.__importDefault(require("fs"));
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
- exports.GetPackageInfo = () => {
8
- const buffer = fs_1.default.readFileSync(path_1.default.resolve(__dirname, '../../package.json'));
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
- exports.isHttpsLink = (link) => link.trim().startsWith(exports.GITHUB_ADDRESS);
13
- exports.ParseGithubHttpsLink = (httpsLink) => {
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
- exports.TextEllipsis = (text, maxLen) => (text.length >= maxLen ? `${text.slice(0, maxLen)}...` : text);
67
- exports.MakeDirs = (dirs) => {
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 (fs_1.default.existsSync(dir)) {
75
+ if (node_fs_1.default.existsSync(dir)) {
70
76
  callback && callback();
71
77
  return;
72
78
  }
73
- mkdirs(path_1.default.dirname(dir), () => {
74
- fs_1.default.mkdirSync(dir);
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 (fs_1.default.existsSync(dirs))
84
+ if (node_fs_1.default.existsSync(dirs))
79
85
  return;
80
86
  mkdirs(dirs);
81
- };
82
- exports.AddExtraRandomQs = (origin) => {
83
- if (origin.indexOf('?') !== -1) {
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, RepoOptionType, DgitLifeCycle, DgitLoadGitTree } from './type';
2
- declare const dgit: (repoOption: RepoOptionType, dPath: string, dgitOptions?: DgitGlobalOption | undefined, hooks?: (DgitLifeCycle & DgitLoadGitTree) | undefined) => Promise<void>;
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,193 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- const fs_1 = tslib_1.__importDefault(require("fs"));
5
- const path_1 = tslib_1.__importDefault(require("path"));
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 repo_1 = tslib_1.__importDefault(require("./repo"));
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
- const dgit = (repoOption, dPath, dgitOptions, hooks) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
16
- const { username, password, token, githubLink, proxy = '', } = repoOption;
17
- let { owner, repoName, ref = 'master', relativePath = '.', } = repoOption;
18
- if (githubLink && utils_1.isHttpsLink(githubLink)) {
19
- const parseResult = utils_1.ParseGithubHttpsLink(githubLink);
20
- owner = parseResult.owner;
21
- repoName = parseResult.repoName;
22
- ref = parseResult.ref;
23
- relativePath = parseResult.relativePath;
24
- }
25
- if (!owner || !repoName) {
26
- throw new Error('invalid repo option.');
27
- }
28
- const logger = log_1.createLogger(dgitOptions);
29
- const { exclude = [], include = [] } = dgitOptions || {};
30
- let { parallelLimit = DEFAULT_PARALLEL_LIMIT } = dgitOptions || {};
31
- if (!parallelLimit || parallelLimit <= 0) {
32
- logger('parallelLimit value is invalid.');
33
- parallelLimit = DEFAULT_PARALLEL_LIMIT;
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
+ // https://deepwiki.com/search/python-node-treepythonjula-pyt_e06e7d62-6a9a-4d4b-9e74-0a1cf32f4946?mode=fast
17
+ function dgit(repoOption, dPath, dgitOptions, hooks) {
18
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
19
+ const { username, password, token, githubLink, proxy = '', } = repoOption;
20
+ let { owner, repoName, ref = 'master', relativePath = '.', } = repoOption;
21
+ if (githubLink && (0, utils_1.isHttpsLink)(githubLink)) {
22
+ const parseResult = (0, utils_1.ParseGithubHttpsLink)(githubLink);
23
+ owner = parseResult.owner;
24
+ repoName = parseResult.repoName;
25
+ ref = parseResult.ref;
26
+ relativePath = parseResult.relativePath;
27
+ }
28
+ if (!owner || !repoName) {
29
+ throw new Error('invalid repo option.');
30
+ }
31
+ const logger = (0, log_1.createLogger)(dgitOptions);
32
+ const { exclude = [], include = [], exactMatch = false } = dgitOptions || {};
33
+ let { parallelLimit = DEFAULT_PARALLEL_LIMIT } = dgitOptions || {};
34
+ if (!parallelLimit || parallelLimit <= 0) {
35
+ logger('parallelLimit value is invalid.');
36
+ parallelLimit = DEFAULT_PARALLEL_LIMIT;
76
37
  }
77
- const treeNodeList = result.tree;
78
- const includeTreeNodeList = treeNodeList.filter(node => {
79
- const nPath = path_1.default.resolve(__dirname, node.path);
80
- const rPath = path_1.default.resolve(__dirname, relativePath);
81
- if (!nPath.startsWith(rPath) || node.type !== 'blob') {
82
- return false;
38
+ parallelLimit > MAX_PARALLEL_LIMIT && (parallelLimit = MAX_PARALLEL_LIMIT);
39
+ const { onSuccess, onError, onProgress, onFinish, onRetry, onResolved, beforeLoadTree, afterLoadTree, } = hooks || {};
40
+ let onSuccessResolve = () => { };
41
+ let onErrorReject = () => { };
42
+ const prom = new Promise((resolve, reject) => {
43
+ onSuccessResolve = resolve;
44
+ onErrorReject = reject;
45
+ });
46
+ const { getRepoTreeUrl, getDownloadUrl } = (0, repo_1.default)(owner, repoName, ref, proxy);
47
+ const url = getRepoTreeUrl();
48
+ const headers = {
49
+ 'User-Agent': UserAgent,
50
+ 'Authorization': token ? `token ${token}` : undefined,
51
+ };
52
+ const auth = username && password
53
+ ? {
54
+ user: username,
55
+ pass: password,
56
+ sendImmediately: true,
83
57
  }
84
- if (exclude.some(v => nPath.startsWith(path_1.default.resolve(rPath, v))) &&
85
- include.every(v => !nPath.startsWith(path_1.default.resolve(rPath, v)))) {
86
- return false;
58
+ : undefined;
59
+ const options = {
60
+ url,
61
+ headers,
62
+ auth,
63
+ };
64
+ const destPath = node_path_1.default.isAbsolute(dPath) ? dPath : node_path_1.default.resolve(node_process_1.default.cwd(), dPath);
65
+ logger(' request repo tree options.');
66
+ logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
67
+ try {
68
+ logger(' loading remote repo tree...');
69
+ beforeLoadTree && beforeLoadTree();
70
+ const body = yield (0, request_1.requestGetPromise)(options, dgitOptions || {}, {
71
+ onRetry() {
72
+ logger(` request ${url} failed. Retrying...`);
73
+ onRetry && onRetry();
74
+ },
75
+ });
76
+ logger(' loading remote repo tree succeed.');
77
+ afterLoadTree && afterLoadTree();
78
+ const result = JSON.parse(body);
79
+ if (!result.tree || result.tree.length <= 0) {
80
+ throw new Error('404 repo not found!');
87
81
  }
88
- return true;
89
- });
90
- if (includeTreeNodeList.length <= 0) {
91
- throw new Error(`404 repo ${relativePath} not found!`);
92
- }
93
- const totalStatus = includeTreeNodeList.reduce((prev, cur) => {
94
- if (cur.type === 'blob') {
95
- prev.size += cur.size;
96
- prev.count++;
82
+ const treeNodeList = result.tree;
83
+ const includeTreeNodeList = treeNodeList.filter((node) => {
84
+ const nPath = node_path_1.default.resolve(__dirname, node.path);
85
+ const rPath = node_path_1.default.resolve(__dirname, relativePath);
86
+ let pathMatch;
87
+ if (exactMatch) {
88
+ // 精确匹配:路径完全相等或者路径后跟分隔符
89
+ pathMatch = nPath === rPath || nPath.startsWith(rPath + node_path_1.default.sep) || nPath.startsWith(`${rPath}/`);
90
+ }
91
+ else {
92
+ pathMatch = nPath.startsWith(rPath);
93
+ }
94
+ if (!pathMatch || node.type !== 'blob') {
95
+ return false;
96
+ }
97
+ if (exclude.some(v => nPath.startsWith(node_path_1.default.resolve(rPath, v)))
98
+ && include.every(v => !nPath.startsWith(node_path_1.default.resolve(rPath, v)))) {
99
+ return false;
100
+ }
101
+ return true;
102
+ });
103
+ if (includeTreeNodeList.length <= 0) {
104
+ throw new Error(`404 repo ${relativePath} not found!`);
97
105
  }
98
- return prev;
99
- }, { size: 0, count: 0 });
100
- let currentSize = 0;
101
- let currentCount = 0;
102
- onResolved &&
103
- onResolved({
106
+ const totalStatus = includeTreeNodeList.reduce((prev, cur) => {
107
+ if (cur.type === 'blob') {
108
+ prev.size += cur.size;
109
+ prev.count++;
110
+ }
111
+ return prev;
112
+ }, { size: 0, count: 0 });
113
+ let currentSize = 0;
114
+ let currentCount = 0;
115
+ onResolved
116
+ && onResolved({
117
+ currentSize,
118
+ currentCount,
119
+ totalSize: totalStatus.size,
120
+ totalCount: totalStatus.count,
121
+ });
122
+ logger(' include files resolved.');
123
+ logger('', JSON.stringify({
104
124
  currentSize,
105
125
  currentCount,
106
126
  totalSize: totalStatus.size,
107
127
  totalCount: totalStatus.count,
108
- });
109
- logger(' include files resolved.');
110
- logger('', JSON.stringify({
111
- currentSize,
112
- currentCount,
113
- totalSize: totalStatus.size,
114
- totalCount: totalStatus.count,
115
- }));
116
- async_1.default.eachLimit(includeTreeNodeList, parallelLimit, (node, callback) => {
117
- const downloadUrl = getDownloadUrl(node.path);
118
- const rPath = path_1.default.resolve(destPath, relativePath);
119
- const tPath = path_1.default.resolve(destPath, node.path);
120
- const root = path_1.default.resolve(destPath, '.');
121
- let targetPath;
122
- if (rPath === tPath) {
123
- targetPath = path_1.default.resolve(destPath, path_1.default.basename(tPath));
124
- }
125
- else {
126
- targetPath = tPath.replace(rPath, root);
127
- }
128
- logger('', node.path, relativePath, targetPath);
129
- if (!fs_1.default.existsSync(path_1.default.dirname(targetPath))) {
130
- utils_1.MakeDirs(path_1.default.dirname(targetPath));
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.
128
+ }));
129
+ async_1.default.eachLimit(includeTreeNodeList, parallelLimit, (node, callback) => {
130
+ const downloadUrl = getDownloadUrl(node.path);
131
+ const rPath = node_path_1.default.resolve(destPath, relativePath);
132
+ const tPath = node_path_1.default.resolve(destPath, node.path);
133
+ const root = node_path_1.default.resolve(destPath, '.');
134
+ let targetPath;
135
+ if (rPath === tPath) {
136
+ targetPath = node_path_1.default.resolve(destPath, node_path_1.default.basename(tPath));
137
+ }
138
+ else {
139
+ targetPath = tPath.replace(rPath, root);
140
+ }
141
+ logger('', node.path, relativePath, targetPath);
142
+ if (!node_fs_1.default.existsSync(node_path_1.default.dirname(targetPath))) {
143
+ (0, utils_1.MakeDirs)(node_path_1.default.dirname(targetPath));
144
+ }
145
+ const ws = node_fs_1.default.createWriteStream(targetPath);
146
+ logger(` downloading from ${downloadUrl}...`);
147
+ (0, request_1.requestOnStream)(downloadUrl, ws, dgitOptions || {}, {
148
+ onSuccess() {
149
+ currentCount++;
150
+ currentSize += node.size;
151
+ logger(` write file ${node.path} succeed.
139
152
  size: [${currentSize}/${totalStatus.size}],
140
153
  count: [${currentCount}/${totalStatus.count}]`);
141
- onProgress &&
142
- onProgress({
143
- totalCount: totalStatus.count,
144
- totalSize: totalStatus.size,
145
- currentSize,
146
- currentCount,
147
- }, node);
148
- callback();
149
- },
150
- onError(err) {
151
- logger('', err);
152
- callback(new Error(` request ${downloadUrl} failed.`));
153
- },
154
- onRetry() {
155
- logger(` request ${downloadUrl} failed. Retrying...`);
156
- onRetry && onRetry();
157
- },
154
+ onProgress
155
+ && onProgress({
156
+ totalCount: totalStatus.count,
157
+ totalSize: totalStatus.size,
158
+ currentSize,
159
+ currentCount,
160
+ }, node);
161
+ callback();
162
+ },
163
+ onError(err) {
164
+ logger('', err);
165
+ callback(new Error(` request ${downloadUrl} failed.`));
166
+ },
167
+ onRetry() {
168
+ logger(` request ${downloadUrl} failed. Retrying...`);
169
+ onRetry && onRetry();
170
+ },
171
+ });
172
+ }, (err) => {
173
+ if (err) {
174
+ onError && onError(err);
175
+ onFinish && onFinish();
176
+ onErrorReject(err);
177
+ }
178
+ else {
179
+ onSuccess && onSuccess();
180
+ onFinish && onFinish();
181
+ onSuccessResolve();
182
+ }
158
183
  });
159
- }, err => {
160
- if (err) {
161
- onError && onError(err);
162
- onFinish && onFinish();
163
- onErrorReject(err);
164
- }
165
- else {
166
- onSuccess && onSuccess();
167
- onFinish && onFinish();
168
- onSuccessResolve();
169
- }
170
- });
171
- }
172
- catch (error) {
173
- onError && onError(error);
174
- onFinish && onFinish();
175
- onErrorReject(error);
176
- }
177
- return prom;
178
- });
184
+ }
185
+ catch (error) {
186
+ onError && onError(error);
187
+ onFinish && onFinish();
188
+ onErrorReject(error);
189
+ }
190
+ return prom;
191
+ });
192
+ }
179
193
  exports.default = dgit;
package/lib/log.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { DgitGlobalOption } from './type';
2
- export declare const createLogger: (option?: DgitGlobalOption | undefined) => (...message: any[]) => void;
1
+ import type { DgitGlobalOption } from './type';
2
+ export declare function createLogger(option?: DgitGlobalOption): (...message: any[]) => void;