@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/src/request.ts CHANGED
@@ -1,6 +1,7 @@
1
- import fs from 'fs';
2
- import request, { UrlOptions, CoreOptions } from 'request';
3
- import { DgitGlobalOption, DgitLifeCycle } from './type';
1
+ import type fs from 'node:fs';
2
+ import type { CoreOptions, UrlOptions } from 'request';
3
+ import type { DgitGlobalOption, DgitLifeCycle } from './type';
4
+ import request from 'request';
4
5
  import { AddExtraRandomQs } from './cmd/utils';
5
6
  import { createLogger } from './log';
6
7
 
@@ -9,102 +10,100 @@ type RequestOption = UrlOptions & CoreOptions;
9
10
  const REQUEST_RETRY_DELAY = 1500;
10
11
  const DEFAULT_MAX_RETRY_COUNT = 5;
11
12
 
12
- const requestGet = (
13
- options: RequestOption,
14
- maxRetryCount: number,
15
- hooks?: DgitLifeCycle,
16
- ): void => {
17
- const {
18
- onSuccess, onError, onFinish, onRetry,
19
- } = hooks || {};
20
-
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);
32
- return;
33
- }
34
-
35
- onSuccess && onSuccess(body);
13
+ function requestGet(options: RequestOption, maxRetryCount: number, hooks?: DgitLifeCycle): void {
14
+ const {
15
+ onSuccess,
16
+ onError,
17
+ onFinish,
18
+ onRetry,
19
+ } = hooks || {};
20
+
21
+ request.get(options, (err, _, body) => {
22
+ if (err) {
23
+ if (maxRetryCount < 1) {
24
+ onError && onError(err);
36
25
  onFinish && onFinish();
37
- });
38
- };
39
-
40
- export const requestGetPromise = (
41
- options: RequestOption,
42
- dgitOptions: DgitGlobalOption,
43
- hooks?: DgitLifeCycle,
44
- ): Promise<any> => new Promise((resolve, reject) => {
26
+ return;
27
+ }
28
+ setTimeout(() => {
29
+ onRetry && onRetry();
30
+ requestGet(options, maxRetryCount - 1, hooks);
31
+ }, REQUEST_RETRY_DELAY);
32
+ return;
33
+ }
34
+
35
+ onSuccess && onSuccess(body);
36
+ onFinish && onFinish();
37
+ });
38
+ }
39
+
40
+ export function requestGetPromise(options: RequestOption, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle): Promise<any> {
41
+ return new Promise((resolve, reject) => {
45
42
  const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
46
43
 
47
44
  const {
48
- onSuccess, onError, onFinish, onRetry,
45
+ onSuccess,
46
+ onError,
47
+ onFinish,
48
+ onRetry,
49
49
  } = hooks || {};
50
50
 
51
51
  const newHooks: DgitLifeCycle = {
52
- onSuccess (data: any) {
53
- resolve(data);
54
- onSuccess && onSuccess(data);
55
- },
56
- onError (err: any) {
57
- reject(err);
58
- onError && onError(err);
59
- },
60
- onFinish,
61
- onRetry,
52
+ onSuccess(data: any) {
53
+ resolve(data);
54
+ onSuccess && onSuccess(data);
55
+ },
56
+ onError(err: any) {
57
+ reject(err);
58
+ onError && onError(err);
59
+ },
60
+ onFinish,
61
+ onRetry,
62
62
  };
63
63
 
64
64
  requestGet(options, maxRetryCount, newHooks);
65
- });
66
-
67
- export const requestOnStream = (
68
- url: string,
69
- ws: fs.WriteStream,
70
- dgitOptions: DgitGlobalOption,
71
- hooks?: DgitLifeCycle,
72
- ) => {
73
- const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
74
-
75
- const logger = createLogger(dgitOptions);
76
-
77
- const {
78
- onSuccess, onError, onFinish, onRetry,
79
- } = hooks || {};
80
-
81
- const fn = (retryCount: number): void => {
82
- const downloadUrl = AddExtraRandomQs(url);
83
- logger(` dowloading from ${ downloadUrl }...`);
84
-
85
- request(encodeURI(downloadUrl))
86
- .on('error', err => {
87
- if (retryCount <= 0) {
88
- onError && onError(err);
89
- onFinish && onFinish();
90
- return;
91
- }
92
- setTimeout(() => {
93
- onRetry && onRetry();
94
- fn(retryCount - 1);
95
- }, REQUEST_RETRY_DELAY);
96
- })
97
- .pipe(ws);
98
- };
99
-
100
- ws.on('finish', () => {
101
- onSuccess && onSuccess();
102
- onFinish && onFinish();
103
- });
104
-
105
- ws.on('error', () => {
106
- logger(` ${ url }, write stream failed.`);
107
- });
108
-
109
- fn(maxRetryCount);
110
- };
65
+ });
66
+ }
67
+
68
+ export function requestOnStream(url: string, ws: fs.WriteStream, dgitOptions: DgitGlobalOption, hooks?: DgitLifeCycle) {
69
+ const { maxRetryCount = DEFAULT_MAX_RETRY_COUNT } = dgitOptions;
70
+
71
+ const logger = createLogger(dgitOptions);
72
+
73
+ const {
74
+ onSuccess,
75
+ onError,
76
+ onFinish,
77
+ onRetry,
78
+ } = hooks || {};
79
+
80
+ const fn = (retryCount: number): void => {
81
+ const downloadUrl = AddExtraRandomQs(url);
82
+ logger(` dowloading from ${downloadUrl}...`);
83
+
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);
97
+ };
98
+
99
+ ws.on('finish', () => {
100
+ onSuccess && onSuccess();
101
+ onFinish && onFinish();
102
+ });
103
+
104
+ ws.on('error', () => {
105
+ logger(` ${url}, write stream failed.`);
106
+ });
107
+
108
+ fn(maxRetryCount);
109
+ }
package/src/type.ts CHANGED
@@ -1,61 +1,62 @@
1
1
  export type GithubRelativePath = string;
2
2
 
3
3
  export interface DgitGlobalOption {
4
- maxRetryCount?: number;
5
- parallelLimit?: number;
6
- log?: boolean;
7
- logPrefix?: string;
8
- exclude?: GithubRelativePath[];
9
- include?: GithubRelativePath[];
4
+ maxRetryCount?: number;
5
+ parallelLimit?: number;
6
+ log?: boolean;
7
+ logPrefix?: string;
8
+ exclude?: GithubRelativePath[];
9
+ include?: GithubRelativePath[];
10
+ exactMatch?: boolean; // 精确匹配,比如只匹配mingw-w64-python,不去匹配mingw-w64-python-*
10
11
  }
11
12
 
12
13
  export interface PrivateOption {
13
- username?: string;
14
- password?: string;
15
- token?: string;
14
+ username?: string;
15
+ password?: string;
16
+ token?: string;
16
17
  }
17
18
 
18
19
  export interface RepoOptionType extends PrivateOption {
19
- owner?: string; // github owner
20
- repoName?: string; // repository name
21
- ref?: string; // branch, commit hash, tag.
22
- relativePath?: string; // download relative path
23
- githubLink?: string;
24
- proxy?: string;
20
+ owner?: string; // github owner
21
+ repoName?: string; // repository name
22
+ ref?: string; // branch, commit hash, tag.
23
+ relativePath?: string; // download relative path
24
+ githubLink?: string;
25
+ proxy?: string;
25
26
  }
26
27
 
27
28
  export interface RepoTreeNode {
28
- path: string;
29
- type: 'blob' | 'tree';
30
- size: number;
31
- url: string;
29
+ path: string;
30
+ type: 'blob' | 'tree';
31
+ size: number;
32
+ url: string;
32
33
  }
33
34
 
34
35
  export interface DgitLifeCycle {
35
- onSuccess?: (data?: any)=> void;
36
- onError?: (err?: any)=> void;
37
- onFinish?: ()=> void;
38
- onRetry?: ()=> void;
39
- onProgress?: (status: ProgressStatus, node: RepoTreeNode)=> void;
40
- onResolved?: (status: ProgressStatus)=> void;
36
+ onSuccess?: (data?: any) => void;
37
+ onError?: (err?: any) => void;
38
+ onFinish?: () => void;
39
+ onRetry?: () => void;
40
+ onProgress?: (status: ProgressStatus, node: RepoTreeNode) => void;
41
+ onResolved?: (status: ProgressStatus) => void;
41
42
  }
42
43
 
43
44
  export interface DgitLoadGitTree {
44
- beforeLoadTree?: ()=> void;
45
- afterLoadTree?: ()=> void;
45
+ beforeLoadTree?: () => void;
46
+ afterLoadTree?: () => void;
46
47
  }
47
48
 
48
49
  export interface ProgressStatus {
49
- currentSize: number;
50
- currentCount: number;
51
- totalCount: number;
52
- totalSize: number;
50
+ currentSize: number;
51
+ currentCount: number;
52
+ totalCount: number;
53
+ totalSize: number;
53
54
  }
54
55
 
55
56
  export interface GithubLinkInfo {
56
- owner: string;
57
- repoName: string;
58
- ref: string;
59
- relativePath: string;
60
- type: string;
57
+ owner: string;
58
+ repoName: string;
59
+ ref: string;
60
+ relativePath: string;
61
+ type: string;
61
62
  }
package/test/dgit.test.ts CHANGED
@@ -1,132 +1,135 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import dgit from '../src/dgit';
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { expect } from 'chai';
4
4
  import {
5
- after, describe, it, before,
5
+ after,
6
+ before,
7
+ describe,
8
+ it,
6
9
  } from 'mocha';
7
- import { expect } from 'chai';
10
+ import dgit from '../src/dgit';
8
11
 
9
- const Clean = (targets: Array<string>, callback: Function) => {
10
- targets.forEach(deletePath => {
11
- const stat = fs.statSync(deletePath);
12
- if (stat.isFile()) {
13
- return fs.unlinkSync(deletePath);
14
- }
15
- if (stat.isDirectory()) {
16
- const nextTargets = fs.readdirSync(deletePath).map(fileName => `${ deletePath }/${ fileName }`);
17
- Clean(nextTargets, () => {
18
- fs.rmdirSync(deletePath);
19
- });
20
- }
21
- });
22
- callback();
23
- };
12
+ function Clean(targets: Array<string>, callback: () => void) {
13
+ targets.forEach((deletePath) => {
14
+ const stat = fs.statSync(deletePath);
15
+ if (stat.isFile()) {
16
+ return fs.unlinkSync(deletePath);
17
+ }
18
+ if (stat.isDirectory()) {
19
+ const nextTargets = fs.readdirSync(deletePath).map(fileName => `${deletePath}/${fileName}`);
20
+ Clean(nextTargets, () => {
21
+ fs.rmdirSync(deletePath);
22
+ });
23
+ }
24
+ });
25
+ callback();
26
+ }
24
27
 
25
28
  describe('dgit功能测试', () => {
26
- const baseDir = 'testDir';
27
- const basePath = path.resolve(__dirname, '../', baseDir);
28
- const target = path.resolve(basePath, './PLAN.txt');
29
- const target2 = path.resolve(basePath, './.eslintignore');
30
- const target3 = path.resolve(basePath, './webpack.config.js');
31
- const target4 = path.resolve(basePath, './config/webpack.base.js');
32
- const target5 = path.resolve(basePath, './dgit.ts');
33
- const target6 = path.resolve(basePath, './cmd/main.ts');
34
- const target7 = path.resolve(basePath, './cmd/action.ts');
29
+ const baseDir = 'testDir';
30
+ const basePath = path.resolve(__dirname, '../', baseDir);
31
+ const target = path.resolve(basePath, './PLAN.txt');
32
+ const target2 = path.resolve(basePath, './.eslintignore');
33
+ const target3 = path.resolve(basePath, './webpack.config.js');
34
+ const target4 = path.resolve(basePath, './config/webpack.base.js');
35
+ const target5 = path.resolve(basePath, './dgit.ts');
36
+ const target6 = path.resolve(basePath, './cmd/main.ts');
37
+ const target7 = path.resolve(basePath, './cmd/action.ts');
35
38
 
36
- const deleteTarget = (): Promise<void> => new Promise(resolve => {
37
- if (fs.existsSync(basePath)) {
38
- return Clean([ basePath ], resolve);
39
- }
40
- resolve();
41
- });
39
+ const deleteTarget = (): Promise<void> => new Promise((resolve) => {
40
+ if (fs.existsSync(basePath)) {
41
+ return Clean([basePath], resolve);
42
+ }
43
+ resolve();
44
+ });
42
45
 
43
- before(deleteTarget);
44
- after(deleteTarget);
46
+ before(deleteTarget);
47
+ after(deleteTarget);
45
48
 
46
- it('dgit 能拉取远端git指定目录代码', async () => {
47
- await dgit(
48
- {
49
- owner : 'JohnApache',
50
- repoName : 'hasaki-cli',
51
- ref : 'master',
52
- relativePath: './PLAN.txt',
53
- },
54
- './testDir',
55
- { log: true },
56
- {
57
- onSuccess () {
58
- expect(fs.existsSync(target)).to.be.equal(true);
59
- },
60
- },
61
- );
62
- });
49
+ it('dgit 能拉取远端git指定目录代码', async () => {
50
+ await dgit(
51
+ {
52
+ owner: 'JohnApache',
53
+ repoName: 'hasaki-cli',
54
+ ref: 'master',
55
+ relativePath: './PLAN.txt',
56
+ },
57
+ './testDir',
58
+ { log: true },
59
+ {
60
+ onSuccess() {
61
+ expect(fs.existsSync(target)).to.be.equal(true);
62
+ },
63
+ },
64
+ );
65
+ });
63
66
 
64
- it('dgit 能直接使用githubLink方式拉取远端git指定目录代码', async () => {
65
- await dgit(
66
- { githubLink: 'https://github.com/JohnApache/hasaki-cli/blob/master/PLAN.txt' },
67
- './testDir',
68
- {
69
- log : true,
70
- parallelLimit: 1,
71
- },
72
- {
73
- onSuccess () {
74
- expect(fs.existsSync(target)).to.be.equal(true);
75
- },
76
- },
77
- );
78
- });
67
+ it('dgit 能直接使用githubLink方式拉取远端git指定目录代码', async () => {
68
+ await dgit(
69
+ { githubLink: 'https://github.com/JohnApache/hasaki-cli/blob/master/PLAN.txt' },
70
+ './testDir',
71
+ {
72
+ log: true,
73
+ parallelLimit: 1,
74
+ },
75
+ {
76
+ onSuccess() {
77
+ expect(fs.existsSync(target)).to.be.equal(true);
78
+ },
79
+ },
80
+ );
81
+ });
79
82
 
80
- it('dgit 能直接使用githubLink方式拉取远端git指定深层指定文件代码', async () => {
81
- await dgit(
82
- { githubLink: 'https://github.com/JohnApache/hasaki-cli/blob/master/assets/.eslintignore' },
83
- './testDir',
84
- {
85
- log : true,
86
- parallelLimit: 1,
87
- },
88
- {
89
- onSuccess () {
90
- expect(fs.existsSync(target2)).to.be.equal(true);
91
- },
92
- },
93
- );
94
- });
83
+ it('dgit 能直接使用githubLink方式拉取远端git指定深层指定文件代码', async () => {
84
+ await dgit(
85
+ { githubLink: 'https://github.com/JohnApache/hasaki-cli/blob/master/assets/.eslintignore' },
86
+ './testDir',
87
+ {
88
+ log: true,
89
+ parallelLimit: 1,
90
+ },
91
+ {
92
+ onSuccess() {
93
+ expect(fs.existsSync(target2)).to.be.equal(true);
94
+ },
95
+ },
96
+ );
97
+ });
95
98
 
96
- it('dgit 能直接使用githubLink方式拉取远端git指定深层指定目录代码', async () => {
97
- await dgit(
98
- { githubLink: 'https://github.com/JohnApache/hasaki-cli/tree/master/assets/webpack' },
99
- './testDir',
100
- {
101
- log : true,
102
- parallelLimit: 2,
103
- },
104
- {
105
- onSuccess () {
106
- expect(fs.existsSync(target3)).to.be.equal(true);
107
- expect(fs.existsSync(target4)).to.be.equal(true);
108
- },
109
- },
110
- );
111
- });
99
+ it('dgit 能直接使用githubLink方式拉取远端git指定深层指定目录代码', async () => {
100
+ await dgit(
101
+ { githubLink: 'https://github.com/JohnApache/hasaki-cli/tree/master/assets/webpack' },
102
+ './testDir',
103
+ {
104
+ log: true,
105
+ parallelLimit: 2,
106
+ },
107
+ {
108
+ onSuccess() {
109
+ expect(fs.existsSync(target3)).to.be.equal(true);
110
+ expect(fs.existsSync(target4)).to.be.equal(true);
111
+ },
112
+ },
113
+ );
114
+ });
112
115
 
113
- it('dgit 能接受exclude include 参数筛选目标目录文件', async () => {
114
- await dgit(
115
- { githubLink: 'https://github.com/JohnApache/dgit/tree/master/src' },
116
- './testDir',
117
- {
118
- log : true,
119
- parallelLimit: 2,
120
- exclude : [ 'cmd' ],
121
- include : [ 'cmd/main.ts' ],
122
- },
123
- {
124
- onSuccess () {
125
- expect(fs.existsSync(target5)).to.be.equal(true);
126
- expect(fs.existsSync(target6)).to.be.equal(true);
127
- expect(fs.existsSync(target7)).to.be.equal(false);
128
- },
129
- },
130
- );
131
- });
116
+ it('dgit 能接受exclude include 参数筛选目标目录文件', async () => {
117
+ await dgit(
118
+ { githubLink: 'https://github.com/JohnApache/dgit/tree/master/src' },
119
+ './testDir',
120
+ {
121
+ log: true,
122
+ parallelLimit: 2,
123
+ exclude: ['cmd'],
124
+ include: ['cmd/main.ts'],
125
+ },
126
+ {
127
+ onSuccess() {
128
+ expect(fs.existsSync(target5)).to.be.equal(true);
129
+ expect(fs.existsSync(target6)).to.be.equal(true);
130
+ expect(fs.existsSync(target7)).to.be.equal(false);
131
+ },
132
+ },
133
+ );
134
+ });
132
135
  });