@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/cmd/main.ts CHANGED
@@ -1,70 +1,73 @@
1
- import program from 'commander';
1
+ import type { PackageInfo } from './type';
2
+ import process from 'node:process';
2
3
  import chalk from 'chalk';
3
4
 
4
- import { PackageInfo } from './type';
5
- import { GetPackageInfo } from './utils';
5
+ import { Command } from 'commander';
6
6
  import DownloadAction from './action';
7
+ import { GetPackageInfo } from './utils';
7
8
 
8
9
  const EXIT_CODE = 1;
10
+ const program = new Command();
9
11
 
10
- const Exit = (): void => {
11
- process.exit(EXIT_CODE);
12
- };
12
+ function Exit(): void {
13
+ process.exit(EXIT_CODE);
14
+ }
13
15
 
14
- const UnknownCommand = (cmdName: string): void => {
15
- console.log(`${ chalk.red('Unknown command') } ${ chalk.yellow(cmdName) }.`);
16
- };
16
+ function UnknownCommand(cmdName: string): void {
17
+ console.log(`${chalk.red('Unknown command')} ${chalk.yellow(cmdName)}.`);
18
+ }
17
19
 
18
20
  const packageInfo: PackageInfo = GetPackageInfo();
19
21
 
20
22
  program.version(packageInfo.version);
21
23
 
22
24
  program
23
- .command('download [githubLink]')
24
- .option('--owner <ownerName>', 'git repo author.')
25
- .option('--repo-name <repoName>', 'git repo name.')
26
- .option('--ref <refName>', 'git repo branch, commit hash or tagname.')
27
- .option(
28
- '--relative-path <relativePath>',
29
- 'specified repo relative path to download.',
30
- )
31
- .option('-d, --dest <destPath>', 'specified dest path.')
32
- .option(
33
- '-l, --parallel-limit, <number>',
34
- 'specified download max parallel limit.',
35
- )
36
- .option('-u, --username, <username>', 'specified git account username.')
37
- .option('-p --password, <password>', 'specified git account password.')
38
- .option(
39
- '-t --token, <token>',
40
- 'specified git account personal access token.',
41
- )
42
- .option(
43
- '-e --exclude, <relativePath,...,relativePath>',
44
- 'indicates which file paths need to be excluded in the current directory.',
45
- )
46
- .option(
47
- '-i --include, <relativePath,...,relativePath>',
48
- 'indicates which files need to be included in the exclusion file list.',
49
- )
50
- .option('--log', 'output dgit internal log details.')
51
- .option('--log-prefix, <log>', 'dgit internal log prefix.')
52
- .option('--proxy, <proxyHttp>', 'dgit proxy download url.')
53
- .alias('d')
54
- .description('download the file with the specified path of the remote repo.')
55
- .action(DownloadAction);
25
+ .command('download [githubLink]')
26
+ .option('--owner <ownerName>', 'git repo author.')
27
+ .option('--repo-name <repoName>', 'git repo name.')
28
+ .option('--ref <refName>', 'git repo branch, commit hash or tagname.')
29
+ .option(
30
+ '--relative-path <relativePath>',
31
+ 'specified repo relative path to download.',
32
+ )
33
+ .option('-d, --dest <destPath>', 'specified dest path.')
34
+ .option(
35
+ '-l, --parallel-limit, <number>',
36
+ 'specified download max parallel limit.',
37
+ )
38
+ .option('-u, --username, <username>', 'specified git account username.')
39
+ .option('-p --password, <password>', 'specified git account password.')
40
+ .option(
41
+ '-t --token, <token>',
42
+ 'specified git account personal access token.',
43
+ )
44
+ .option(
45
+ '-e --exclude, <relativePath,...,relativePath>',
46
+ 'indicates which file paths need to be excluded in the current directory.',
47
+ )
48
+ .option(
49
+ '-i --include, <relativePath,...,relativePath>',
50
+ 'indicates which files need to be included in the exclusion file list.',
51
+ )
52
+ .option('--exact-match', 'enable exact path matching instead of prefix matching.')
53
+ .option('--log', 'output dgit internal log details.')
54
+ .option('--log-prefix, <log>', 'dgit internal log prefix.')
55
+ .option('--proxy, <proxyHttp>', 'dgit proxy download url.')
56
+ .alias('d')
57
+ .description('download the file with the specified path of the remote repo.')
58
+ .action(DownloadAction);
56
59
 
57
60
  program.on('command:*', (cmdObj = []) => {
58
- const [ cmd ] = cmdObj;
59
- if (cmd) {
60
- program.outputHelp();
61
- UnknownCommand(cmd);
62
- Exit();
63
- }
61
+ const [cmd] = cmdObj;
62
+ if (cmd) {
63
+ program.outputHelp();
64
+ UnknownCommand(cmd);
65
+ Exit();
66
+ }
64
67
  });
65
68
 
66
69
  if (process.argv.slice(2).length <= 0) {
67
- program.help();
70
+ program.help();
68
71
  }
69
72
 
70
73
  program.parse(process.argv);
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,42 @@
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
+ exactMatch?: boolean;
21
22
  }
22
23
 
23
24
  export interface DownloadPromptInfo {
24
- dest: string;
25
- owner: string;
26
- repoName: string;
27
- ref: string;
28
- relativePath: string;
25
+ dest: string;
26
+ owner: string;
27
+ repoName: string;
28
+ ref: string;
29
+ relativePath: string;
29
30
  }
30
31
 
31
32
  export interface GithubLinkInfo {
32
- owner: string;
33
- repoName: string;
34
- ref: string;
35
- relativePath: string;
36
- type: string;
33
+ owner: string;
34
+ repoName: string;
35
+ ref: string;
36
+ relativePath: string;
37
+ type: string;
37
38
  }
38
39
 
39
40
  export interface PasswordPromptInfo {
40
- password: string;
41
+ password: string;
41
42
  }
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
+ }