Skip to content

Commit 56aa0c5

Browse files
committed
feat: user view open in mac app
1 parent b02d0b4 commit 56aa0c5

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
export { version as versionNumber } from '../package.json'
2+
3+
export const PROFILE_URL = {
4+
web: 'https://web.okjike.com/u/',
5+
mobile: 'https://m.okjike.com/users/',
6+
mac: 'jike://page.jk/user/',
7+
}

src/modules/user/profile.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { format } from 'date-fns'
55
import { JikeClient } from 'jike-sdk/node'
66
import { filterUsers } from '../../utils/user'
77
import { displayImage } from '../../utils/terminal'
8+
import { PROFILE_URL } from '../../constants'
89
import type { ApiResponses } from 'jike-sdk/node'
910

1011
const { colors } = logger
@@ -111,19 +112,10 @@ export const queryProfile = async ({
111112

112113
texts.push(
113114
[],
114-
[
115-
' Web',
116-
colors.underline(`https://web.okjike.com/u/${result.user.username}`),
117-
],
118-
[
119-
'Mobile',
120-
colors.underline(`https://m.okjike.com/users/${result.user.username}`),
121-
],
115+
[' Web', colors.underline(PROFILE_URL.web + result.user.username)],
116+
['Mobile', colors.underline(PROFILE_URL.mobile + result.user.username)],
122117
process.platform === 'darwin'
123-
? [
124-
' macOS',
125-
colors.underline(`jike://page.jk/user/${result.user.username}`),
126-
]
118+
? [' macOS', colors.underline(PROFILE_URL.mac + result.user.username)]
127119
: []
128120
)
129121

src/modules/user/view.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,43 @@ import { logger } from '@poppinss/cliui'
22
import { createCommand } from 'commander'
33
import { JikeClient } from 'jike-sdk/node'
44
import open from 'open'
5+
import { PROFILE_URL } from '../../constants'
6+
import { errorAndExit } from '../../utils/log'
57
import { filterUsers } from '../../utils/user'
68

79
interface ViewOptions {
8-
username: string
9-
mobile: boolean
10+
username?: string
11+
platform?: keyof typeof PROFILE_URL
1012
}
1113

1214
export const view = createCommand('view')
15+
.aliases(['v', 'info', 'i'])
1316
.argument('[username]', 'the username of user')
1417
.description('open user profile in browser')
15-
.option('-m, --mobile', 'view page for mobile phone')
16-
.action((username?: string) => viewUser(username))
18+
.option(
19+
'-p, --platform <platform>',
20+
'supports web, mobile and mac, default is web',
21+
'web'
22+
)
23+
.action((username?: string) => {
24+
const opts = view.opts<ViewOptions>()
25+
viewUser({ ...opts, username })
26+
})
1727

18-
export const viewUser = async (username?: string) => {
19-
const { mobile } = view.opts<Omit<ViewOptions, 'username'>>()
28+
export const viewUser = async ({ username, platform }: ViewOptions) => {
2029
if (!username) {
2130
const [user] = filterUsers()
2231
const client = JikeClient.fromJSON(user)
2332
username = await client.getSelf().getUsername()
2433
}
2534

26-
const url = mobile
27-
? `https://m.okjike.com/users/${username}`
28-
: `https://web.okjike.com/u/${username}`
35+
platform ||= 'web'
36+
if (!(platform in PROFILE_URL)) {
37+
errorAndExit(new Error(`invlid platform: ${platform}`))
38+
}
2939

40+
const url = PROFILE_URL[platform] + username
3041
open(url)
42+
3143
logger.info(`${url} opened!`)
3244
}

0 commit comments

Comments
 (0)