@@ -4,16 +4,14 @@ import Table from 'tty-table';
44
55import { post , get , doDelete } from './api' ;
66import type { Platform } from './types' ;
7+ import { t } from './utils/i18n' ;
8+
9+ const validPlatforms = [ 'ios' , 'android' , 'harmony' ] ;
710
8- const validPlatforms = {
9- ios : 1 ,
10- android : 1 ,
11- harmony : 1 ,
12- } ;
1311
1412export 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 } ) ) ;
1715 }
1816 return platform ;
1917}
@@ -22,27 +20,23 @@ export function getSelectedApp(platform: Platform) {
2220 checkPlatform ( platform ) ;
2321
2422 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 } ) ) ;
2824 }
2925 const updateInfo = JSON . parse ( fs . readFileSync ( 'update.json' , 'utf8' ) ) ;
3026 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 } ) ) ;
3428 }
3529 return updateInfo [ platform ] ;
3630}
3731
38- export async function listApp ( platform : Platform ) {
32+ export async function listApp ( platform : Platform | '' = '' ) {
3933 const { data } = await get ( '/app/list' ) ;
4034 const list = platform ? data . filter ( ( v ) => v . platform === platform ) : data ;
4135
4236 const header = [
43- { value : '应用 id' } ,
44- { value : '应用名称' } ,
45- { value : '平台' } ,
37+ { value : t ( 'appId' ) } ,
38+ { value : t ( 'appName' ) } ,
39+ { value : t ( 'platform' ) } ,
4640 ] ;
4741 const rows = [ ] ;
4842 for ( const app of list ) {
@@ -51,19 +45,15 @@ export async function listApp(platform: Platform) {
5145
5246 console . log ( Table ( header , rows ) . render ( ) ) ;
5347
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 } ) } ` ) ;
5949 return list ;
6050}
6151
6252export async function chooseApp ( platform : Platform ) {
6353 const list = await listApp ( platform ) ;
6454
6555 while ( true ) {
66- const id = await question ( '输入应用 id:' ) ;
56+ const id = await question ( t ( 'enterAppIdQuestion' ) ) ;
6757 const app = list . find ( ( v ) => v . id === Number ( id ) ) ;
6858 if ( app ) {
6959 return app ;
@@ -77,13 +67,13 @@ export const commands = {
7767 } : {
7868 options : { name : string ; downloadUrl : string ; platform : Platform } ;
7969 } ) {
80- const name = options . name || ( await question ( '应用名称:' ) ) ;
70+ const name = options . name || ( await question ( t ( 'appNameQuestion' ) ) ) ;
8171 const { downloadUrl } = options ;
8272 const platform = checkPlatform (
83- options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
73+ options . platform || ( await question ( t ( 'platformQuestion' ) ) ) ,
8474 ) ;
8575 const { id } = await post ( '/app/create' , { name, platform, downloadUrl } ) ;
86- console . log ( `已成功创建应用(id: ${ id } )` ) ;
76+ console . log ( t ( 'createAppSuccess' , { id } ) ) ;
8777 await this . selectApp ( {
8878 args : [ id ] ,
8979 options : { platform } ,
@@ -93,18 +83,18 @@ export const commands = {
9383 const { platform } = options ;
9484 const id = args [ 0 ] || chooseApp ( platform ) ;
9585 if ( ! id ) {
96- console . log ( '已取消' ) ;
86+ console . log ( t ( 'cancelled' ) ) ;
9787 }
9888 await doDelete ( `/app/${ id } ` ) ;
99- console . log ( '操作成功' ) ;
89+ console . log ( t ( 'operationSuccess' ) ) ;
10090 } ,
10191 apps : async ( { options } : { options : { platform : Platform } } ) => {
10292 const { platform } = options ;
10393 listApp ( platform ) ;
10494 } ,
10595 selectApp : async ( { args, options } : { args : string [ ] ; options : { platform : Platform } } ) => {
10696 const platform = checkPlatform (
107- options . platform || ( await question ( '平台(ios/android/harmony):' ) ) ,
97+ options . platform || ( await question ( t ( 'platformQuestion' ) ) ) ,
10898 ) ;
10999 const id = args [ 0 ]
110100 ? Number . parseInt ( args [ 0 ] )
@@ -115,9 +105,7 @@ export const commands = {
115105 try {
116106 updateInfo = JSON . parse ( fs . readFileSync ( 'update.json' , 'utf8' ) ) ;
117107 } catch ( e ) {
118- console . error (
119- 'Failed to parse file `update.json`. Try to remove it manually.' ,
120- ) ;
108+ console . error ( t ( 'failedToParseUpdateJson' ) ) ;
121109 throw e ;
122110 }
123111 }
0 commit comments