@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/src/cmd/prompt.ts CHANGED
@@ -1,98 +1,99 @@
1
- import inquirer, { Question } from 'inquirer';
2
- import { DownloadPromptInfo, PasswordPromptInfo } from './type';
1
+ import type { Question } from 'inquirer';
2
+ import type { DownloadPromptInfo, PasswordPromptInfo } from './type';
3
+ import inquirer from 'inquirer';
3
4
 
4
5
  export const CreatePrompt = (questions: Array<Question>): Promise<any> => inquirer.prompt(questions);
5
6
 
6
- export const DownloadPrompt = async (
7
- currentInfo: DownloadPromptInfo,
8
- ): Promise<DownloadPromptInfo> => {
9
- if (
10
- currentInfo.owner &&
11
- currentInfo.repoName &&
12
- currentInfo.ref &&
13
- currentInfo.relativePath &&
14
- currentInfo.dest
15
- ) return currentInfo;
7
+ export async function DownloadPrompt(currentInfo: DownloadPromptInfo): Promise<DownloadPromptInfo> {
8
+ if (
9
+ currentInfo.owner
10
+ && currentInfo.repoName
11
+ && currentInfo.ref
12
+ && currentInfo.relativePath
13
+ && currentInfo.dest
14
+ ) {
15
+ return currentInfo;
16
+ }
16
17
 
17
- const questions = [
18
- {
19
- type: 'input',
20
- name: 'owner',
21
- when () {
22
- return !currentInfo.owner;
23
- },
24
- validate (input: string) {
25
- return input && input.length > 0;
26
- },
27
- message: 'input github ownername.',
28
- },
29
- {
30
- type: 'input',
31
- name: 'repoName',
32
- when () {
33
- return !currentInfo.repoName;
34
- },
35
- validate (input: string) {
36
- return input && input.length > 0;
37
- },
38
- message: 'input github repoName.',
39
- },
40
- {
41
- type: 'input',
42
- name: 'ref',
43
- when () {
44
- return !currentInfo.ref;
45
- },
46
- validate (input: string) {
47
- return input && input.length > 0;
48
- },
49
- 'default': 'master',
50
- message : 'input github branch or commit hash or tagname.',
51
- },
52
- {
53
- type: 'input',
54
- name: 'relativePath',
55
- when () {
56
- return !currentInfo.relativePath;
57
- },
58
- validate (input: string) {
59
- return input && input.length > 0;
60
- },
61
- 'default': '.',
62
- message : 'input github relative path.',
63
- },
64
- {
65
- type: 'input',
66
- name: 'dest',
67
- when () {
68
- return !currentInfo.dest;
69
- },
70
- validate (input: string) {
71
- return input && input.length > 0;
72
- },
73
- 'default': '.',
74
- message : 'input template output dest path.',
75
- },
76
- ];
18
+ const questions = [
19
+ {
20
+ type: 'input',
21
+ name: 'owner',
22
+ when() {
23
+ return !currentInfo.owner;
24
+ },
25
+ validate(input: string) {
26
+ return input && input.length > 0;
27
+ },
28
+ message: 'input github ownername.',
29
+ },
30
+ {
31
+ type: 'input',
32
+ name: 'repoName',
33
+ when() {
34
+ return !currentInfo.repoName;
35
+ },
36
+ validate(input: string) {
37
+ return input && input.length > 0;
38
+ },
39
+ message: 'input github repoName.',
40
+ },
41
+ {
42
+ type: 'input',
43
+ name: 'ref',
44
+ when() {
45
+ return !currentInfo.ref;
46
+ },
47
+ validate(input: string) {
48
+ return input && input.length > 0;
49
+ },
50
+ default: 'master',
51
+ message: 'input github branch or commit hash or tagname.',
52
+ },
53
+ {
54
+ type: 'input',
55
+ name: 'relativePath',
56
+ when() {
57
+ return !currentInfo.relativePath;
58
+ },
59
+ validate(input: string) {
60
+ return input && input.length > 0;
61
+ },
62
+ default: '.',
63
+ message: 'input github relative path.',
64
+ },
65
+ {
66
+ type: 'input',
67
+ name: 'dest',
68
+ when() {
69
+ return !currentInfo.dest;
70
+ },
71
+ validate(input: string) {
72
+ return input && input.length > 0;
73
+ },
74
+ default: '.',
75
+ message: 'input template output dest path.',
76
+ },
77
+ ];
77
78
 
78
- const answer = await CreatePrompt(questions);
79
- return {
80
- owner : answer.owner || currentInfo.owner,
81
- dest : answer.dest || currentInfo.dest,
82
- repoName : answer.repoName || currentInfo.repoName,
83
- relativePath: answer.relativePath || currentInfo.relativePath,
84
- ref : answer.ref || currentInfo.ref,
85
- };
86
- };
79
+ const answer = await 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
+ }
87
88
 
88
- export const PasswordPrompt = (): Promise<PasswordPromptInfo> => {
89
- const question = {
90
- type: 'password',
91
- name: 'password',
92
- validate (input: string) {
93
- return input && input.length > 0;
94
- },
95
- message: 'input github account password.',
96
- };
97
- return CreatePrompt([ question ]);
98
- };
89
+ export function PasswordPrompt(): Promise<PasswordPromptInfo> {
90
+ const question = {
91
+ type: 'password',
92
+ name: 'password',
93
+ validate(input: string) {
94
+ return input && input.length > 0;
95
+ },
96
+ message: 'input github account password.',
97
+ };
98
+ return CreatePrompt([question]);
99
+ }
package/src/cmd/type.ts CHANGED
@@ -1,41 +1,41 @@
1
1
  export interface PackageInfo {
2
- version: string;
3
- name: string;
2
+ version: string;
3
+ name: string;
4
4
  }
5
5
 
6
6
  export interface CommandInfo {
7
- dest?: string;
8
- owner?: string;
9
- repoName?: string;
10
- ref?: string;
11
- relativePath?: string;
12
- parallelLimit?: string;
13
- username?: string;
14
- password?: string;
15
- token?: string;
16
- exclude?: string;
17
- include?: string;
18
- log?: boolean;
19
- logPrefix?: string;
20
- proxy?: string;
7
+ dest?: string;
8
+ owner?: string;
9
+ repoName?: string;
10
+ ref?: string;
11
+ relativePath?: string;
12
+ parallelLimit?: string;
13
+ username?: string;
14
+ password?: string;
15
+ token?: string;
16
+ exclude?: string;
17
+ include?: string;
18
+ log?: boolean;
19
+ logPrefix?: string;
20
+ proxy?: string;
21
21
  }
22
22
 
23
23
  export interface DownloadPromptInfo {
24
- dest: string;
25
- owner: string;
26
- repoName: string;
27
- ref: string;
28
- relativePath: string;
24
+ dest: string;
25
+ owner: string;
26
+ repoName: string;
27
+ ref: string;
28
+ relativePath: string;
29
29
  }
30
30
 
31
31
  export interface GithubLinkInfo {
32
- owner: string;
33
- repoName: string;
34
- ref: string;
35
- relativePath: string;
36
- type: string;
32
+ owner: string;
33
+ repoName: string;
34
+ ref: string;
35
+ relativePath: string;
36
+ type: string;
37
37
  }
38
38
 
39
39
  export interface PasswordPromptInfo {
40
- password: string;
40
+ password: string;
41
41
  }
package/src/cmd/utils.ts CHANGED
@@ -1,88 +1,93 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { PackageInfo, GithubLinkInfo } from './type';
1
+ import type { GithubLinkInfo, PackageInfo } from './type';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
4
 
5
- export const GetPackageInfo = (): PackageInfo => {
6
- const buffer = fs.readFileSync(path.resolve(__dirname, '../../package.json'));
7
- return JSON.parse(buffer.toString());
8
- };
5
+ export function GetPackageInfo(): PackageInfo {
6
+ const buffer = fs.readFileSync(path.resolve(__dirname, '../../package.json'));
7
+ return JSON.parse(buffer.toString());
8
+ }
9
9
 
10
10
  export const GITHUB_ADDRESS = 'https://github.com/';
11
11
  export const isHttpsLink = (link: string) => link.trim().startsWith(GITHUB_ADDRESS);
12
12
 
13
- export const ParseGithubHttpsLink = (httpsLink: string): GithubLinkInfo => {
14
- let nextLink = httpsLink.trim().slice(GITHUB_ADDRESS.length);
15
- let index = nextLink.indexOf('/');
16
- if (index === -1) throw new Error('invalid github address.');
17
- const owner = nextLink.slice(0, index);
18
- nextLink = nextLink.slice(owner.length + 1);
19
- index = nextLink.indexOf('/');
20
- let repoName: string;
21
- if (index === -1) {
22
- repoName = nextLink.slice(0);
23
- if (!repoName) throw new Error('invalid github address.');
24
- return {
25
- owner,
26
- repoName,
27
- ref : 'master',
28
- relativePath: '',
29
- type : 'tree',
30
- };
13
+ export function ParseGithubHttpsLink(httpsLink: string): GithubLinkInfo {
14
+ let nextLink = httpsLink.trim().slice(GITHUB_ADDRESS.length);
15
+ let index = nextLink.indexOf('/');
16
+ if (index === -1)
17
+ throw new Error('invalid github address.');
18
+ const owner = nextLink.slice(0, index);
19
+ nextLink = nextLink.slice(owner.length + 1);
20
+ index = nextLink.indexOf('/');
21
+ let repoName: string;
22
+ if (index === -1) {
23
+ repoName = nextLink.slice(0);
24
+ if (!repoName)
25
+ throw new Error('invalid github address.');
26
+ return {
27
+ owner,
28
+ repoName,
29
+ ref: 'master',
30
+ relativePath: '',
31
+ type: 'tree',
32
+ };
33
+ }
34
+ repoName = nextLink.slice(0, index);
35
+ nextLink = nextLink.slice(repoName.length + 1);
36
+ index = nextLink.indexOf('/');
37
+ let ref = 'master';
38
+ let relativePath = '';
39
+ let type = 'tree';
40
+ if (index === -1) {
41
+ if (repoName.endsWith('.git')) {
42
+ const lastIndex = -4;
43
+ repoName = repoName.slice(0, lastIndex);
31
44
  }
32
- repoName = nextLink.slice(0, index);
33
- nextLink = nextLink.slice(repoName.length + 1);
45
+ }
46
+ else {
47
+ type = nextLink.slice(0, index);
48
+ nextLink = nextLink.slice(type.length + 1);
34
49
  index = nextLink.indexOf('/');
35
- let ref = 'master';
36
- let relativePath = '';
37
- let type = 'tree';
38
50
  if (index === -1) {
39
- if (repoName.endsWith('.git')) {
40
- const lastIndex = -4;
41
- repoName = repoName.slice(0, lastIndex);
42
- }
43
- } else {
44
- type = nextLink.slice(0, index);
45
- nextLink = nextLink.slice(type.length + 1);
46
- index = nextLink.indexOf('/');
47
- if (index === -1) {
48
- ref = nextLink.slice(0) || 'master';
49
- } else {
50
- ref = nextLink.slice(0, index);
51
- relativePath = nextLink.slice(ref.length + 1);
52
- }
51
+ ref = nextLink.slice(0) || 'master';
52
+ }
53
+ else {
54
+ ref = nextLink.slice(0, index);
55
+ relativePath = nextLink.slice(ref.length + 1);
53
56
  }
57
+ }
54
58
 
55
- return {
56
- owner,
57
- repoName,
58
- ref,
59
- relativePath,
60
- type,
61
- };
62
- };
59
+ return {
60
+ owner,
61
+ repoName,
62
+ ref,
63
+ relativePath,
64
+ type,
65
+ };
66
+ }
63
67
 
64
- export const TextEllipsis = (text: string, maxLen: number): string => (text.length >= maxLen ? `${ text.slice(0, maxLen) }...` : text);
68
+ export const TextEllipsis = (text: string, maxLen: number): string => (text.length >= maxLen ? `${text.slice(0, maxLen)}...` : text);
65
69
 
66
- export const MakeDirs = (dirs: string): void => {
67
- const mkdirs = (dir: string, callback?: ()=> void) => {
68
- if (fs.existsSync(dir)) {
69
- callback && callback();
70
- return;
71
- }
70
+ export function MakeDirs(dirs: string): void {
71
+ const mkdirs = (dir: string, callback?: () => void) => {
72
+ if (fs.existsSync(dir)) {
73
+ callback && callback();
74
+ return;
75
+ }
72
76
 
73
- mkdirs(path.dirname(dir), () => {
74
- fs.mkdirSync(dir);
75
- callback && callback();
76
- });
77
- };
77
+ mkdirs(path.dirname(dir), () => {
78
+ fs.mkdirSync(dir);
79
+ callback && callback();
80
+ });
81
+ };
78
82
 
79
- if (fs.existsSync(dirs)) return;
80
- mkdirs(dirs);
81
- };
83
+ if (fs.existsSync(dirs))
84
+ return;
85
+ mkdirs(dirs);
86
+ }
82
87
 
83
- export const AddExtraRandomQs = (origin: string): string => {
84
- if (origin.indexOf('?') !== -1) {
85
- return `${ origin }&_t=${ Math.random() }`;
86
- }
87
- return `${ origin }?_t=${ Math.random() }`;
88
- };
88
+ export function AddExtraRandomQs(origin: string): string {
89
+ if (origin.includes('?')) {
90
+ return `${origin}&_t=${Math.random()}`;
91
+ }
92
+ return `${origin}?_t=${Math.random()}`;
93
+ }