/
index.ts
97 lines (79 loc) · 2.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import path from 'path'
import type { ExtensionContext, StatusBarItem } from 'vscode'
import { StatusBarAlignment, commands, window, workspace } from 'vscode'
import { version } from '../package.json'
import { log } from './log'
import { registerAnnotations } from './annotation'
import { registerAutoComplete } from './autocomplete'
import { ContextLoader } from './contextLoader'
import { registerSelectionStyle } from './selectionStyle'
import { isFulfilled, isRejected } from './utils'
async function registerRoot(ext: ExtensionContext, status: StatusBarItem, cwd: string) {
const contextLoader = new ContextLoader(cwd)
await contextLoader.ready
const hasConfig = await contextLoader.loadContextInDirectory(cwd)
// TODO: improve this to re-enable after configuration created
if (!hasConfig)
throw new Error(`➖ No config found in ${cwd}`)
registerAutoComplete(cwd, contextLoader, ext)
registerAnnotations(cwd, contextLoader, status, ext)
registerSelectionStyle(cwd, contextLoader)
return contextLoader
}
export async function activate(ext: ExtensionContext) {
log.appendLine(`⚪️ UnoCSS for VS Code v${version}\n`)
const projectPath = workspace.workspaceFolders?.[0].uri.fsPath
if (!projectPath) {
log.appendLine('➖ No active workspace found, UnoCSS is disabled')
return
}
const config = workspace.getConfiguration('unocss')
const disabled = config.get<boolean>('disable', false)
if (disabled) {
log.appendLine('➖ Disabled by configuration')
return
}
const status = window.createStatusBarItem(StatusBarAlignment.Right, 200)
status.text = 'UnoCSS'
const root = config.get<string | string[]>('root')
if (Array.isArray(root) && root.length) {
const cwds = root.map(dir => path.resolve(projectPath, dir))
const contextLoadersResult = await Promise.allSettled(
cwds.map(cwd => registerRoot(ext, status, cwd)),
)
ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('🔁 Reloading...')
await Promise.all(
contextLoadersResult
.filter(isFulfilled)
.map(result => result.value.reload),
)
log.appendLine('✅ Reloaded.')
}),
)
for (const result of contextLoadersResult.filter(isRejected)) {
const e = result.reason
log.appendLine(String(e.stack ?? e))
}
return
}
// now if the root is an array, then it is an empty array
const cwd = (root && !Array.isArray(root))
? path.resolve(projectPath, root)
: projectPath
try {
const contextLoader = await registerRoot(ext, status, cwd)
ext.subscriptions.push(
commands.registerCommand('unocss.reload', async () => {
log.appendLine('🔁 Reloading...')
await contextLoader.reload()
log.appendLine('✅ Reloaded.')
}),
)
}
catch (e: any) {
log.appendLine(String(e.stack ?? e))
}
}
export function deactivate() {}