@@ -4,16 +4,14 @@ import Table from 'tty-table';
4
4
5
5
import { post , get , doDelete } from './api' ;
6
6
import type { Platform } from './types' ;
7
+ import { t } from './utils/i18n' ;
8
+
9
+ const validPlatforms = [ 'ios' , 'android' , 'harmony' ] ;
7
10
8
- const validPlatforms = {
9
- ios : 1 ,
10
- android : 1 ,
11
- harmony : 1 ,
12
- } ;
13
11
14
12
export function checkPlatform ( platform : Platform ) {
15
- if ( ! validPlatforms [ platform ] ) {
16
- throw new Error ( `无法识别的平台 ' ${ platform } '` ) ;
13
+ if ( ! validPlatforms . includes ( platform ) ) {
14
+ throw new Error ( t ( 'unsupportedPlatform' , { platform } ) ) ;
17
15
}
18
16
return platform ;
19
17
}
@@ -22,27 +20,23 @@ export function getSelectedApp(platform: Platform) {
22
20
checkPlatform ( platform ) ;
23
21
24
22
if ( ! fs . existsSync ( 'update.json' ) ) {
25
- throw new Error (
26
- `App not selected. run 'pushy selectApp --platform ${ platform } ' first!` ,
27
- ) ;
23
+ throw new Error ( t ( 'appNotSelected' , { platform } ) ) ;
28
24
}
29
25
const updateInfo = JSON . parse ( fs . readFileSync ( 'update.json' , 'utf8' ) ) ;
30
26
if ( ! updateInfo [ platform ] ) {
31
- throw new Error (
32
- `App not selected. run 'pushy selectApp --platform ${ platform } ' first!` ,
33
- ) ;
27
+ throw new Error ( t ( 'appNotSelected' , { platform } ) ) ;
34
28
}
35
29
return updateInfo [ platform ] ;
36
30
}
37
31
38
- export async function listApp ( platform : Platform ) {
32
+ export async function listApp ( platform : Platform | '' = '' ) {
39
33
const { data } = await get ( '/app/list' ) ;
40
34
const list = platform ? data . filter ( ( v ) => v . platform === platform ) : data ;
41
35
42
36
const header = [
43
- { value : '应用 id' } ,
44
- { value : '应用名称' } ,
45
- { value : '平台' } ,
37
+ { value : t ( 'appId' ) } ,
38
+ { value : t ( 'appName' ) } ,
39
+ { value : t ( 'platform' ) } ,
46
40
] ;
47
41
const rows = [ ] ;
48
42
for ( const app of list ) {
@@ -51,19 +45,15 @@ export async function listApp(platform: Platform) {
51
45
52
46
console . log ( Table ( header , rows ) . render ( ) ) ;
53
47
54
- if ( platform ) {
55
- console . log ( `\共 ${ list . length } ${ platform } 个应用` ) ;
56
- } else {
57
- console . log ( `\共 ${ list . length } 个应用` ) ;
58
- }
48
+ console . log ( `\n${ t ( 'totalApps' , { count : list . length , platform } ) } ` ) ;
59
49
return list ;
60
50
}
61
51
62
52
export async function chooseApp ( platform : Platform ) {
63
53
const list = await listApp ( platform ) ;
64
54
65
55
while ( true ) {
66
- const id = await question ( '输入应用 id:' ) ;
56
+ const id = await question ( t ( 'enterAppIdQuestion' ) ) ;
67
57
const app = list . find ( ( v ) => v . id === Number ( id ) ) ;
68
58
if ( app ) {
69
59
return app ;
@@ -77,13 +67,13 @@ export const commands = {
77
67
} : {
78
68
options : { name : string ; downloadUrl : string ; platform : Platform } ;
79
69
} ) {
80
- const name = options . name || ( await question ( '应用名称:' ) ) ;
70
+ const name = options . name || ( await question ( t ( 'appNameQuestion' ) ) ) ;
81
71
const { downloadUrl } = options ;
82
72
const platform = checkPlatform (
83
- options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
73
+ options . platform || ( await question ( t ( 'platformQuestion' ) ) ) ,
84
74
) ;
85
75
const { id } = await post ( '/app/create' , { name, platform, downloadUrl } ) ;
86
- console . log ( `已成功创建应用(id: ${ id } )` ) ;
76
+ console . log ( t ( 'createAppSuccess' , { id } ) ) ;
87
77
await this . selectApp ( {
88
78
args : [ id ] ,
89
79
options : { platform } ,
@@ -93,18 +83,18 @@ export const commands = {
93
83
const { platform } = options ;
94
84
const id = args [ 0 ] || chooseApp ( platform ) ;
95
85
if ( ! id ) {
96
- console . log ( '已取消' ) ;
86
+ console . log ( t ( 'cancelled' ) ) ;
97
87
}
98
88
await doDelete ( `/app/${ id } ` ) ;
99
- console . log ( '操作成功' ) ;
89
+ console . log ( t ( 'operationSuccess' ) ) ;
100
90
} ,
101
91
apps : async ( { options } : { options : { platform : Platform } } ) => {
102
92
const { platform } = options ;
103
93
listApp ( platform ) ;
104
94
} ,
105
95
selectApp : async ( { args, options } : { args : string [ ] ; options : { platform : Platform } } ) => {
106
96
const platform = checkPlatform (
107
- options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
97
+ options . platform || ( await question ( t ( 'platformQuestion' ) ) ) ,
108
98
) ;
109
99
const id = args [ 0 ]
110
100
? Number . parseInt ( args [ 0 ] )
@@ -115,9 +105,7 @@ export const commands = {
115
105
try {
116
106
updateInfo = JSON . parse ( fs . readFileSync ( 'update.json' , 'utf8' ) ) ;
117
107
} catch ( e ) {
118
- console . error (
119
- 'Failed to parse file `update.json`. Try to remove it manually.' ,
120
- ) ;
108
+ console . error ( t ( 'failedToParseUpdateJson' ) ) ;
121
109
throw e ;
122
110
}
123
111
}
0 commit comments