2
2
* Created by axetroy on 17-2-14.
3
3
*/
4
4
5
- const path = require ( ' path' ) ;
6
- const gitUrlParse = require ( ' git-url-parse' ) ;
7
- const fs = require ( ' fs-extra' ) ;
8
- const _ = require ( ' lodash' ) ;
9
- const inquirer = require ( ' inquirer' ) ;
5
+ const path = require ( " path" ) ;
6
+ const gitUrlParse = require ( " git-url-parse" ) ;
7
+ const fs = require ( " fs-extra" ) ;
8
+ const _ = require ( " lodash" ) ;
9
+ const inquirer = require ( " inquirer" ) ;
10
10
const prompt = inquirer . createPromptModule ( ) ;
11
- const which = require ( 'which' ) ;
12
- const uniqueString = require ( 'unique-string' ) ;
13
- const clipboardy = require ( 'clipboardy' ) ;
14
- import chalk from 'chalk' ;
15
- const __ = require ( 'i18n' ) . __ ;
16
-
17
-
18
- import { isExistPath , isLink , normalizePath , runShell } from '../utils' ;
19
- import config from '../config' ;
20
- import registry from '../registry' ;
21
- import globalConfig from '../global-config' ;
22
- import Gpmrc from '../gpmrc' ;
23
- import { info , warn } from '../logger' ;
24
-
25
- interface Argv$ {
26
- repo : string ;
27
- }
28
-
29
- interface Options$ {
30
- nolog ?: boolean ;
31
- unixify ?: boolean ;
32
- force ?: boolean ;
33
- name ?: string ;
34
- ignoreRc ?: boolean ;
35
- }
36
-
37
- async function add ( repo : string , options : Options$ ) {
11
+ const which = require ( "which" ) ;
12
+ const uniqueString = require ( "unique-string" ) ;
13
+ const clipboardy = require ( "clipboardy" ) ;
14
+ import chalk from "chalk" ;
15
+ const __ = require ( "i18n" ) . __ ;
16
+
17
+ import { isExistPath , isLink , normalizePath , runShell } from "../utils" ;
18
+ import config from "../config" ;
19
+ import registry from "../registry" ;
20
+ import globalConfig from "../global-config" ;
21
+ import Gpmrc from "../gpmrc" ;
22
+ import { info , warn } from "../logger" ;
23
+ import { IAddOption } from "../type" ;
24
+
25
+ export default async function add ( repo : string , options : IAddOption ) {
38
26
const gitInfo = gitUrlParse ( repo ) ;
39
27
40
28
if ( ! gitInfo || ! gitInfo . owner || ! gitInfo . name ) {
41
- throw new Error ( __ ( ' commands.add.log.invalid_url' , { repo } ) ) ;
29
+ throw new Error ( __ ( " commands.add.log.invalid_url" , { repo } ) ) ;
42
30
}
43
31
44
32
const randomTemp : string = path . join ( config . paths . temp , uniqueString ( ) ) ;
@@ -51,7 +39,7 @@ async function add(repo: string, options: Options$) {
51
39
const ownerDir : string = path . join ( sourceDir , gitInfo . owner ) ;
52
40
let repoDir : string = path . join (
53
41
ownerDir ,
54
- typeof options . name === ' string' ? options . name : gitInfo . name
42
+ typeof options . name === " string" ? options . name : gitInfo . name
55
43
) ;
56
44
57
45
let confirmCover : boolean = false ;
@@ -60,18 +48,18 @@ async function add(repo: string, options: Options$) {
60
48
confirmCover = true ;
61
49
} else {
62
50
confirmCover = ( await prompt ( {
63
- type : ' confirm' ,
64
- name : ' result' ,
51
+ type : " confirm" ,
52
+ name : " result" ,
65
53
message : chalk . white (
66
- __ ( ' commands.add.log.confirm_cover' , {
54
+ __ ( " commands.add.log.confirm_cover" , {
67
55
path : chalk . yellow . underline ( normalizePath ( repoDir , options ) )
68
56
} )
69
57
) ,
70
- [ ' default' ] : false
58
+ [ " default" ] : false
71
59
} ) ) . result ;
72
60
}
73
61
if ( ! confirmCover ) {
74
- ! options . nolog && info ( __ ( ' global.tips.good_bye' ) ) ;
62
+ ! options . nolog && info ( __ ( " global.tips.good_bye" ) ) ;
75
63
return process . exit ( 1 ) ;
76
64
}
77
65
}
@@ -82,11 +70,11 @@ async function add(repo: string, options: Options$) {
82
70
await fs . ensureDir ( randomTemp ) ;
83
71
84
72
try {
85
- const git = which . sync ( ' git' ) ;
73
+ const git = which . sync ( " git" ) ;
86
74
if ( ! git ) {
87
75
return Promise . reject (
88
76
new Error (
89
- __ ( ' commands.add.log.make_sure_install' , { bin : chalk . green ( ' Git' ) } )
77
+ __ ( " commands.add.log.make_sure_install" , { bin : chalk . green ( " Git" ) } )
90
78
)
91
79
) ;
92
80
}
@@ -96,7 +84,7 @@ async function add(repo: string, options: Options$) {
96
84
97
85
await runShell ( `git clone ${ gitInfo . href } ` , {
98
86
cwd : randomTemp ,
99
- stdio : ' inherit'
87
+ stdio : " inherit"
100
88
} ) ;
101
89
102
90
// if it's a link, then unlink first
@@ -125,7 +113,7 @@ async function add(repo: string, options: Options$) {
125
113
await fs . move ( repoDir , newRepoDir ) ;
126
114
repoDir = newRepoDir ;
127
115
}
128
- await gpmrc . runHook ( ' add' , { cwd : repoDir } ) . catch ( err => {
116
+ await gpmrc . runHook ( " add" , { cwd : repoDir } ) . catch ( err => {
129
117
console . error ( err ) ;
130
118
return Promise . resolve ( ) ;
131
119
} ) ;
@@ -139,23 +127,19 @@ async function add(repo: string, options: Options$) {
139
127
if ( ! options . nolog ) {
140
128
let finallyPath = normalizePath ( repoDir , options ) ;
141
129
info (
142
- __ ( ' commands.add.log.info_add_success' , {
130
+ __ ( " commands.add.log.info_add_success" , {
143
131
path : chalk . green . underline ( finallyPath )
144
132
} )
145
133
) ;
146
134
try {
147
135
clipboardy . writeSync ( finallyPath ) ;
148
136
info (
149
- __ ( ' commands.add.log.info_copy_clipboard' , {
150
- key : chalk . green ( ' <CTRL+V>' )
137
+ __ ( " commands.add.log.info_copy_clipboard" , {
138
+ key : chalk . green ( " <CTRL+V>" )
151
139
} )
152
140
) ;
153
141
} catch ( err ) {
154
- warn ( __ ( ' commands.add.log.warn_copy_clipboard' ) ) ;
142
+ warn ( __ ( " commands.add.log.warn_copy_clipboard" ) ) ;
155
143
}
156
144
}
157
145
}
158
-
159
- export default async function ( argv : Argv$ , options : Options$ ) {
160
- return await add ( argv . repo , options ) ;
161
- }
0 commit comments