@bratel/dgit 0.0.15 → 0.0.16

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/dgit.js CHANGED
@@ -45,29 +45,26 @@ function dgit(repoOption, dPath, dgitOptions, hooks) {
45
45
  });
46
46
  const { getRepoTreeUrl, getDownloadUrl } = (0, repo_1.default)(owner, repoName, ref, proxy);
47
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,
57
- }
58
- : undefined;
59
- const options = {
48
+ const config = {
60
49
  url,
61
- headers,
62
- auth,
50
+ headers: {
51
+ 'User-Agent': UserAgent,
52
+ 'Authorization': token ? `token ${token}` : undefined,
53
+ },
54
+ auth: username && password
55
+ ? {
56
+ username,
57
+ password,
58
+ }
59
+ : undefined,
63
60
  };
64
61
  const destPath = node_path_1.default.isAbsolute(dPath) ? dPath : node_path_1.default.resolve(node_process_1.default.cwd(), dPath);
65
62
  logger(' request repo tree options.');
66
- logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
63
+ logger(JSON.stringify(config, null, JSON_STRINGIFY_PADDING));
67
64
  try {
68
65
  logger(' loading remote repo tree...');
69
66
  beforeLoadTree && beforeLoadTree();
70
- const body = yield (0, request_1.requestGetPromise)(options, dgitOptions || {}, {
67
+ const body = yield (0, request_1.requestGetPromise)(config, dgitOptions || {}, {
71
68
  onRetry() {
72
69
  logger(` request ${url} failed. Retrying...`);
73
70
  onRetry && onRetry();
@@ -75,7 +72,7 @@ function dgit(repoOption, dPath, dgitOptions, hooks) {
75
72
  });
76
73
  logger(' loading remote repo tree succeed.');
77
74
  afterLoadTree && afterLoadTree();
78
- const result = JSON.parse(body);
75
+ const result = body;
79
76
  if (!result.tree || result.tree.length <= 0) {
80
77
  throw new Error('404 repo not found!');
81
78
  }
package/lib/request.d.ts CHANGED
@@ -1,7 +1,5 @@
1
+ import type { AxiosRequestConfig } from 'axios';
1
2
  import type fs from 'node:fs';
2
- import type { CoreOptions, UrlOptions } from 'request';
3
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>;
4
+ export declare function requestGetPromise(config: AxiosRequestConfig, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any>;
6
5
  export declare function requestOnStream(url: string, ws: fs.WriteStream, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): void;
7
- export {};
package/lib/request.js CHANGED
@@ -3,15 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requestGetPromise = requestGetPromise;
4
4
  exports.requestOnStream = requestOnStream;
5
5
  const tslib_1 = require("tslib");
6
- const request_1 = tslib_1.__importDefault(require("request"));
6
+ const axios_1 = tslib_1.__importDefault(require("axios"));
7
7
  const utils_1 = require("./cmd/utils");
8
8
  const log_1 = require("./log");
9
9
  const REQUEST_RETRY_DELAY = 1500;
10
10
  const DEFAULT_MAX_RETRY_COUNT = 5;
11
- function requestGet(options, maxRetryCount, hooks) {
12
- const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
13
- request_1.default.get(options, (err, _, body) => {
14
- if (err) {
11
+ function requestGet(config, maxRetryCount, hooks) {
12
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
13
+ const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
14
+ try {
15
+ const response = yield (0, axios_1.default)(config);
16
+ onSuccess && onSuccess(response.data);
17
+ onFinish && onFinish();
18
+ }
19
+ catch (err) {
15
20
  if (maxRetryCount < 1) {
16
21
  onError && onError(err);
17
22
  onFinish && onFinish();
@@ -19,15 +24,12 @@ function requestGet(options, maxRetryCount, hooks) {
19
24
  }
20
25
  setTimeout(() => {
21
26
  onRetry && onRetry();
22
- requestGet(options, maxRetryCount - 1, hooks);
27
+ requestGet(config, maxRetryCount - 1, hooks);
23
28
  }, REQUEST_RETRY_DELAY);
24
- return;
25
29
  }
26
- onSuccess && onSuccess(body);
27
- onFinish && onFinish();
28
30
  });
29
31
  }
30
- function requestGetPromise(options, dgitOptions, hooks) {
32
+ function requestGetPromise(config, dgitOptions, hooks) {
31
33
  return new Promise((resolve, reject) => {
32
34
  const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
33
35
  const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
@@ -43,18 +45,25 @@ function requestGetPromise(options, dgitOptions, hooks) {
43
45
  onFinish,
44
46
  onRetry,
45
47
  };
46
- requestGet(options, maxRetryCount, newHooks);
48
+ requestGet(config, maxRetryCount, newHooks);
47
49
  });
48
50
  }
49
51
  function requestOnStream(url, ws, dgitOptions, hooks) {
50
52
  const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
51
53
  const logger = (0, log_1.createLogger)(dgitOptions);
52
54
  const { onSuccess, onError, onFinish, onRetry, } = hooks || {};
53
- const fn = (retryCount) => {
55
+ const fn = (retryCount) => tslib_1.__awaiter(this, void 0, void 0, function* () {
54
56
  const downloadUrl = (0, utils_1.AddExtraRandomQs)(url);
55
57
  logger(` dowloading from ${downloadUrl}...`);
56
- (0, request_1.default)(encodeURI(downloadUrl))
57
- .on('error', (err) => {
58
+ try {
59
+ const response = yield (0, axios_1.default)({
60
+ method: 'GET',
61
+ url: encodeURI(downloadUrl),
62
+ responseType: 'stream',
63
+ });
64
+ response.data.pipe(ws);
65
+ }
66
+ catch (err) {
58
67
  if (retryCount <= 0) {
59
68
  onError && onError(err);
60
69
  onFinish && onFinish();
@@ -64,9 +73,8 @@ function requestOnStream(url, ws, dgitOptions, hooks) {
64
73
  onRetry && onRetry();
65
74
  fn(retryCount - 1);
66
75
  }, REQUEST_RETRY_DELAY);
67
- })
68
- .pipe(ws);
69
- };
76
+ }
77
+ });
70
78
  ws.on('finish', () => {
71
79
  onSuccess && onSuccess();
72
80
  onFinish && onFinish();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bratel/dgit",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "@dking/hasaki-cli init application",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -24,7 +24,7 @@
24
24
  "build:ts": "tsc --build",
25
25
  "lint": "eslint",
26
26
  "lint:fix": "eslint src --ext .jsx --ext .js --ext .tsx --ext .ts --cache --fix",
27
- "test:mocha": "nyc --reporter=text mocha --require ts-node/register test/**/*.{ts,tsx} -t 60000",
27
+ "test:mocha": "npm run build:ts && nyc --reporter=text mocha --require ts-node/register test/**/*.{ts,tsx} -t 60000",
28
28
  "test:mocha:reporter": "nyc --reporter=lcov --reporter=text mocha --require ts-node/register test/**/*.{ts,tsx} -t 60000 --reporter=mochawesome",
29
29
  "watch:ts": "tsc --watch",
30
30
  "prepublishOnly": "npm run test:mocha && npm run lint && npm run build:ts"
@@ -35,14 +35,13 @@
35
35
  "@types/inquirer": "^9.0.9",
36
36
  "@types/ora": "^3.2.0",
37
37
  "@types/progress": "^2.0.7",
38
- "@types/request": "^2.48.13",
39
38
  "async": "^3.2.6",
39
+ "axios": "^1.13.2",
40
40
  "chalk": "^5.6.2",
41
41
  "commander": "^14.0.2",
42
42
  "inquirer": "^13.1.0",
43
43
  "ora": "^9.0.0",
44
- "progress": "^2.0.3",
45
- "request": "^2.88.2"
44
+ "progress": "^2.0.3"
46
45
  },
47
46
  "devDependencies": {
48
47
  "@antfu/eslint-config": "^6.7.3",
package/src/dgit.ts CHANGED
@@ -86,34 +86,29 @@ async function dgit(repoOption: RepoOptionType, dPath: string, dgitOptions?: Dgi
86
86
  const { getRepoTreeUrl, getDownloadUrl } = repo(owner, repoName, ref, proxy);
87
87
  const url = getRepoTreeUrl();
88
88
 
89
- const headers = {
90
- 'User-Agent': UserAgent,
91
- 'Authorization': token ? `token ${token}` : undefined,
92
- };
93
-
94
- const auth = username && password
95
- ? {
96
- user: username,
97
- pass: password,
98
- sendImmediately: true,
99
- }
100
- : undefined;
101
-
102
- const options = {
89
+ const config = {
103
90
  url,
104
- headers,
105
- auth,
91
+ headers: {
92
+ 'User-Agent': UserAgent,
93
+ 'Authorization': token ? `token ${token}` : undefined,
94
+ },
95
+ auth: username && password
96
+ ? {
97
+ username,
98
+ password,
99
+ }
100
+ : undefined,
106
101
  };
107
102
 
108
103
  const destPath = path.isAbsolute(dPath) ? dPath : path.resolve(process.cwd(), dPath);
109
104
 
110
105
  logger(' request repo tree options.');
111
- logger(JSON.stringify(options, null, JSON_STRINGIFY_PADDING));
106
+ logger(JSON.stringify(config, null, JSON_STRINGIFY_PADDING));
112
107
 
113
108
  try {
114
109
  logger(' loading remote repo tree...');
115
110
  beforeLoadTree && beforeLoadTree();
116
- const body = await requestGetPromise(options, dgitOptions || {}, {
111
+ const body = await requestGetPromise(config, dgitOptions || {}, {
117
112
  onRetry() {
118
113
  logger(` request ${url} failed. Retrying...`);
119
114
  onRetry && onRetry();
@@ -122,7 +117,7 @@ async function dgit(repoOption: RepoOptionType, dPath: string, dgitOptions?: Dgi
122
117
 
123
118
  logger(' loading remote repo tree succeed.');
124
119
  afterLoadTree && afterLoadTree();
125
- const result = JSON.parse(body);
120
+ const result = body;
126
121
 
127
122
  if (!result.tree || result.tree.length <= 0) {
128
123
  throw new Error('404 repo not found!');
package/src/request.ts CHANGED
@@ -1,16 +1,14 @@
1
+ import type { AxiosRequestConfig } from 'axios';
1
2
  import type fs from 'node:fs';
2
- import type { CoreOptions, UrlOptions } from 'request';
3
3
  import type { DgitGlobalOption, DgitLifeCycle } from './type';
4
- import request from 'request';
4
+ import axios from 'axios';
5
5
  import { AddExtraRandomQs } from './cmd/utils';
6
6
  import { createLogger } from './log';
7
7
 
8
- type RequestOption = UrlOptions & CoreOptions;
9
-
10
8
  const REQUEST_RETRY_DELAY = 1500;
11
9
  const DEFAULT_MAX_RETRY_COUNT = 5;
12
10
 
13
- function requestGet(options: RequestOption, maxRetryCount: number, hooks?: DgitLifeCycle): void {
11
+ async function requestGet(config: AxiosRequestConfig, maxRetryCount: number, hooks?: DgitLifeCycle): Promise<void> {
14
12
  const {
15
13
  onSuccess,
16
14
  onError,
@@ -18,26 +16,25 @@ function requestGet(options: RequestOption, maxRetryCount: number, hooks?: DgitL
18
16
  onRetry,
19
17
  } = hooks || {};
20
18
 
21
- request.get(options, (err, _, body) => {
22
- if (err) {
23
- if (maxRetryCount < 1) {
24
- onError && onError(err);
25
- onFinish && onFinish();
26
- return;
27
- }
28
- setTimeout(() => {
29
- onRetry && onRetry();
30
- requestGet(options, maxRetryCount - 1, hooks);
31
- }, REQUEST_RETRY_DELAY);
19
+ try {
20
+ const response = await axios(config);
21
+ onSuccess && onSuccess(response.data);
22
+ onFinish && onFinish();
23
+ }
24
+ catch (err: any) {
25
+ if (maxRetryCount < 1) {
26
+ onError && onError(err);
27
+ onFinish && onFinish();
32
28
  return;
33
29
  }
34
-
35
- onSuccess && onSuccess(body);
36
- onFinish && onFinish();
37
- });
30
+ setTimeout(() => {
31
+ onRetry && onRetry();
32
+ requestGet(config, maxRetryCount - 1, hooks);
33
+ }, REQUEST_RETRY_DELAY);
34
+ }
38
35
  }
39
36
 
40
- export function requestGetPromise(options: RequestOption, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any> {
37
+ export function requestGetPromise(config: AxiosRequestConfig, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any> {
41
38
  return new Promise((resolve, reject) => {
42
39
  const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
43
40
 
@@ -61,7 +58,7 @@ export function requestGetPromise(options: RequestOption, dgitOptions: DgitGloba
61
58
  onRetry,
62
59
  };
63
60
 
64
- requestGet(options, maxRetryCount, newHooks);
61
+ requestGet(config, maxRetryCount, newHooks);
65
62
  });
66
63
  }
67
64
 
@@ -77,23 +74,32 @@ export function requestOnStream(url: string, ws: fs.WriteStream, dgitOptions: Dg
77
74
  onRetry,
78
75
  } = hooks || {};
79
76
 
80
- const fn = (retryCount: number): void => {
77
+ const fn = async (retryCount: number): Promise<void> => {
81
78
  const downloadUrl = AddExtraRandomQs(url);
82
79
  logger(` dowloading from ${downloadUrl}...`);
83
80
 
84
- request(encodeURI(downloadUrl))
85
- .on('error', (err) => {
86
- if (retryCount <= 0) {
87
- onError && onError(err);
88
- onFinish && onFinish();
89
- return;
90
- }
91
- setTimeout(() => {
92
- onRetry && onRetry();
93
- fn(retryCount - 1);
94
- }, REQUEST_RETRY_DELAY);
95
- })
96
- .pipe(ws);
81
+ try {
82
+ const response = await axios(
83
+ {
84
+ method: 'GET',
85
+ url: encodeURI(downloadUrl),
86
+ responseType: 'stream',
87
+ },
88
+ );
89
+ response.data.pipe(ws);
90
+ }
91
+ catch (err: any) {
92
+ if (retryCount <= 0) {
93
+ onError && onError(err);
94
+ onFinish && onFinish();
95
+ return;
96
+ }
97
+
98
+ setTimeout(() => {
99
+ onRetry && onRetry();
100
+ fn(retryCount - 1);
101
+ }, REQUEST_RETRY_DELAY);
102
+ }
97
103
  };
98
104
 
99
105
  ws.on('finish', () => {