Skip to content

Commit d8587e5

Browse files
committed
refactor: convert namespaces to modules
1 parent 6fa86f9 commit d8587e5

20 files changed

+195
-183
lines changed

src/auth/auth-manager.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,38 @@ authProvider.onDidChangeSessions(async e => {
2727
await BlogExportProvider.optionalInstance?.refreshRecords({ force: false, clearCache: true })
2828
})
2929

30-
// eslint-disable-next-line @typescript-eslint/no-namespace
31-
export namespace AuthManager {
32-
export async function isAuthed() {
30+
export class AuthManager {
31+
static async isAuthed() {
3332
const sessionJsonList = await LocalState.getSecret(ExtConst.EXT_SESSION_STORAGE_KEY)
3433
const sessionList = JSON.parse(sessionJsonList ?? '[]') as AuthSession[]
3534
return sessionList.length > 0
3635
}
3736

38-
export function ensureSession(opt?: AuthGetSessionOpt) {
37+
static ensureSession(opt?: AuthGetSessionOpt) {
3938
try {
4039
return authentication.getSession(authProvider.providerId, [], opt)
4140
} catch (e) {
4241
throw Error(`创建/获取 Session 失败: ${e as string}`)
4342
}
4443
}
4544

46-
export async function webLogin() {
45+
static async webLogin() {
4746
authProvider.useBrowser()
48-
await login()
47+
await AuthManager.login()
4948
}
5049

51-
export async function patLogin() {
50+
static async patLogin() {
5251
authProvider.usePat()
53-
await login()
52+
await AuthManager.login()
5453
}
5554

56-
export async function login() {
57-
const session = await ensureSession({ createIfNone: false, forceNewSession: true })
55+
static async login() {
56+
const session = await AuthManager.ensureSession({ createIfNone: false, forceNewSession: true })
5857
if (session !== undefined)
5958
await LocalState.setSecret(ExtConst.EXT_SESSION_STORAGE_KEY, JSON.stringify([session]))
6059
}
6160

62-
export async function logout() {
61+
static async logout() {
6362
if (!(await AuthManager.isAuthed())) return
6463

6564
try {
@@ -74,19 +73,19 @@ export namespace AuthManager {
7473
}
7574
}
7675

77-
export async function acquireToken() {
78-
const session = await ensureSession({ createIfNone: false })
76+
static async acquireToken() {
77+
const session = await AuthManager.ensureSession({ createIfNone: false })
7978

8079
if (session === undefined) Alert.throwWithWarn('未授权')
8180
if (isAuthSessionExpired(session)) {
8281
void Alert.warn('授权已过期,请重新登录')
83-
await logout()
82+
await AuthManager.logout()
8483
}
8584

8685
return session?.accessToken
8786
}
8887

89-
export async function updateAuthStatus() {
88+
static async updateAuthStatus() {
9089
const isAuthed = await AuthManager.isAuthed()
9190
await setCtx('isAuthed', isAuthed)
9291
await setCtx('isUnauthorized', !isAuthed)

src/auth/oauth.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ function getAuthedOauthReq() {
77
return new OauthReq(ExtConst.CLIENT_ID, ExtConst.CLIENT_SEC)
88
}
99

10-
// eslint-disable-next-line @typescript-eslint/no-namespace
11-
export namespace Oauth {
12-
export function getToken(verifyCode: string, authCode: string) {
10+
export class Oauth {
11+
static getToken(verifyCode: string, authCode: string) {
1312
const req = getAuthedOauthReq()
1413
try {
1514
return req.getToken(authCode, verifyCode, globalCtx.extUrl)
@@ -19,7 +18,7 @@ export namespace Oauth {
1918
}
2019
}
2120

22-
export function revokeToken(token: string) {
21+
static revokeToken(token: string) {
2322
try {
2423
const req = getAuthedOauthReq()
2524
return req.revokeToken(token)

src/cmd/blog-export/open-local.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ export async function openLocalExport(opts: Partial<typeof defaultOptions> = def
1818
canSelectFolders: false,
1919
canSelectMany: false,
2020
filters: {
21-
// eslint-disable-next-line @typescript-eslint/naming-convention
2221
Sqlite: ['db', 'zip'],
23-
// eslint-disable-next-line @typescript-eslint/naming-convention
2422
// ZipSqlite: ['zip'],
2523
},
2624
})) ?? []
@@ -44,7 +42,7 @@ export async function openLocalExport(opts: Partial<typeof defaultOptions> = def
4442
// }
4543

4644
if (isConfirmedToUnzip) {
47-
// eslint-disable-next-line @typescript-eslint/naming-convention
45+
4846
const AdmZip = (await import('adm-zip')).default
4947
const zip = new AdmZip(filePath)
5048
zip.extractEntryTo(`${fileName}.db`, dirname, false, true)

src/cmd/browser.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,47 @@ import { execCmd } from '@/infra/cmd'
33
import { UserService } from '@/service/user.service'
44
import { Alert } from '@/infra/alert'
55

6-
export namespace Browser.Open {
7-
export function open(url: string) {
8-
return execCmd('vscode.open', Uri.parse(url))
9-
}
10-
}
6+
export class Browser {
7+
static Open = class {
8+
static open(url: string) {
9+
return execCmd('vscode.open', Uri.parse(url))
10+
}
1111

12-
export namespace Browser.Open.Cnb {
13-
export const home = () => open('https://www.cnblogs.com')
14-
export const news = () => open('https://news.cnblogs.com')
15-
export const ing = () => open('https://ing.cnblogs.com')
16-
export const q = () => open('https://q.cnblogs.com')
17-
}
12+
static Cnb = class {
13+
static home = () => Browser.Open.open('https://www.cnblogs.com')
14+
static news = () => Browser.Open.open('https://news.cnblogs.com')
15+
static ing = () => Browser.Open.open('https://ing.cnblogs.com')
16+
static q = () => Browser.Open.open('https://q.cnblogs.com')
17+
}
1818

19-
export namespace Browser.Open.User {
20-
export const accountSetting = () => open('https://account.cnblogs.com/settings/account')
21-
export const buyVip = () => open('https://cnblogs.vip/')
19+
User = class {
20+
static accountSetting = () => Browser.Open.open('https://account.cnblogs.com/settings/account')
21+
static buyVip = () => Browser.Open.open('https://cnblogs.vip/')
2222

23-
export async function blog() {
24-
const blogApp = (await UserService.getUserInfo())?.blogApp
23+
static async blog() {
24+
const blogApp = (await UserService.getUserInfo())?.blogApp
2525

26-
if (blogApp == null) return void Alert.warn('未开通博客')
26+
if (blogApp == null) return void Alert.warn('未开通博客')
2727

28-
void open(`https://www.cnblogs.com/${blogApp}`)
29-
}
28+
void Browser.Open.open(`https://www.cnblogs.com/${blogApp}`)
29+
}
3030

31-
export const blogConsole = () => open('https://write.cnblogs.com')
31+
static blogConsole = () => Browser.Open.open('https://write.cnblogs.com')
3232

33-
export async function home() {
34-
const accountId = (await UserService.getUserInfo())?.accountId
35-
if (accountId !== undefined) {
36-
const url = `https://home.cnblogs.com/u/${accountId}`
37-
return open(url)
33+
static async home() {
34+
const accountId = (await UserService.getUserInfo())?.accountId
35+
if (accountId !== undefined) {
36+
const url = `https://home.cnblogs.com/u/${accountId}`
37+
return Browser.Open.open(url)
38+
}
39+
}
3840
}
3941
}
4042
}
43+
44+
45+
46+
// eslint-disable-next-line @typescript-eslint/no-namespace
47+
export namespace Browser.Open.User {
48+
49+
}

src/cmd/ing/ing-page-list.ts

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,62 @@ import { getIngListWebviewProvider } from '@/service/ing/ing-list-webview-provid
22
import { QuickPickItem, window } from 'vscode'
33
import { IngType, IngTypesMetadata } from '@/model/ing'
44

5-
export namespace Ing.ListView {
6-
export const refresh = () => getIngListWebviewProvider().reload()
7-
8-
export function goNext() {
9-
const provider = getIngListWebviewProvider()
10-
const { pageIndex } = provider
11-
return provider.reload({ pageIndex: pageIndex + 1 })
12-
}
13-
14-
export function goPrev() {
15-
const provider = getIngListWebviewProvider()
16-
const { pageIndex } = provider
17-
if (pageIndex > 1) return provider.reload({ pageIndex: pageIndex - 1 })
18-
return Promise.resolve()
19-
}
20-
21-
export function goFirst(): Promise<void> {
22-
return getIngListWebviewProvider().reload({ pageIndex: 1 })
23-
}
24-
25-
export function switchType() {
26-
const options: (QuickPickItem & { ingType: IngType })[] = IngTypesMetadata.map(
27-
([ingType, { displayName, description }]) => ({
28-
label: displayName,
29-
ingType: ingType,
30-
description: description,
31-
picked: ingType === getIngListWebviewProvider().ingType,
32-
})
33-
)
34-
const quickPick = window.createQuickPick<(typeof options)[0]>()
35-
36-
quickPick.title = '选择闪存类型'
37-
quickPick.items = options
38-
quickPick.canSelectMany = false
39-
quickPick.activeItems = options.filter(x => x.picked)
40-
quickPick.selectedItems = quickPick.activeItems
41-
quickPick.ignoreFocusOut = false
42-
43-
const disposables = [quickPick]
44-
45-
quickPick.onDidChangeSelection(
46-
([selectedItem]) => {
47-
quickPick.hide()
48-
return getIngListWebviewProvider().reload({
49-
pageIndex: 1,
50-
ingType: selectedItem.ingType,
5+
export class Ing {
6+
static ListView = class {
7+
static refresh = () => getIngListWebviewProvider().reload()
8+
9+
static goNext() {
10+
const provider = getIngListWebviewProvider()
11+
const { pageIndex } = provider
12+
return provider.reload({ pageIndex: pageIndex + 1 })
13+
}
14+
15+
static goPrev() {
16+
const provider = getIngListWebviewProvider()
17+
const { pageIndex } = provider
18+
if (pageIndex > 1) return provider.reload({ pageIndex: pageIndex - 1 })
19+
return Promise.resolve()
20+
}
21+
22+
static goFirst(): Promise<void> {
23+
return getIngListWebviewProvider().reload({ pageIndex: 1 })
24+
}
25+
26+
static switchType() {
27+
const options: (QuickPickItem & { ingType: IngType })[] = IngTypesMetadata.map(
28+
([ingType, { displayName, description }]) => ({
29+
label: displayName,
30+
ingType: ingType,
31+
description: description,
32+
picked: ingType === getIngListWebviewProvider().ingType,
5133
})
52-
},
53-
undefined,
54-
disposables
55-
)
56-
quickPick.onDidHide(() => disposables.forEach(d => d.dispose()), undefined, disposables)
57-
quickPick.show()
58-
59-
return Promise.resolve()
34+
)
35+
const quickPick = window.createQuickPick<(typeof options)[0]>()
36+
37+
quickPick.title = '选择闪存类型'
38+
quickPick.items = options
39+
quickPick.canSelectMany = false
40+
quickPick.activeItems = options.filter(x => x.picked)
41+
quickPick.selectedItems = quickPick.activeItems
42+
quickPick.ignoreFocusOut = false
43+
44+
const disposables = [quickPick]
45+
46+
quickPick.onDidChangeSelection(
47+
([selectedItem]) => {
48+
quickPick.hide()
49+
return getIngListWebviewProvider().reload({
50+
pageIndex: 1,
51+
ingType: selectedItem.ingType,
52+
})
53+
},
54+
undefined,
55+
disposables
56+
)
57+
quickPick.onDidHide(() => disposables.forEach(d => d.dispose()), undefined, disposables)
58+
quickPick.show()
59+
60+
return Promise.resolve()
61+
}
6062
}
6163
}

src/cmd/upload-img/upload-img-from-path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function uploadImgFromPath(path: string) {
77
const readStream = fs.createReadStream(path)
88
return imageService.upload(readStream)
99
} catch (e) {
10-
console.log(`上传图片失败: ${<string>e}`)
11-
void Alert.err(`上传图片失败: ${<string>e}`)
10+
console.log(`上传图片失败: ${e as string}`)
11+
void Alert.err(`上传图片失败: ${e as string}`)
1212
}
1313
}

src/cmd/workspace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Alert } from '@/infra/alert'
33
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
44
import { TextDocument, WorkspaceEdit, window, Range } from 'vscode'
55

6-
export namespace Workspace {
7-
export function resetTextDoc(doc: TextDocument, text: string) {
6+
export class Workspace {
7+
static resetTextDoc(doc: TextDocument, text: string) {
88
const firstLine = doc.lineAt(0)
99
const lastLine = doc.lineAt(doc.lineCount - 1)
1010
const range = new Range(firstLine.range.start, lastLine.range.end)
@@ -16,7 +16,7 @@ export namespace Workspace {
1616
return we
1717
}
1818

19-
export async function codeOpen() {
19+
static async codeOpen() {
2020
const uri = WorkspaceCfg.getWorkspaceUri()
2121
const options = ['在当前窗口中打开', '在新窗口中打开']
2222
const msg = `即将打开 ${uri.fsPath}`
@@ -28,11 +28,11 @@ export namespace Workspace {
2828
await execCmd('vscode.openFolder', uri, shouldOpenInNewWindow)
2929
}
3030

31-
export function osOpen() {
31+
static osOpen() {
3232
void execCmd('revealFileInOS', WorkspaceCfg.getWorkspaceUri())
3333
}
3434

35-
export async function set() {
35+
static async set() {
3636
const uris = await window.showOpenDialog({
3737
title: '选择工作空间',
3838
canSelectFolders: true,

src/ctx/cfg/icon-theme.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { workspace } from 'vscode'
22

3-
export namespace IconThemeCfg {
4-
export function getIconTheme(): string {
3+
export class IconThemeCfg {
4+
static getIconTheme(): string {
55
return workspace.getConfiguration('workbench').get('iconTheme') ?? 'unknown'
66
}
77
}

0 commit comments

Comments
 (0)