Skip to content

Commit

Permalink
feat(vscode): ignore config load failed root (#2662)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 24, 2023
1 parent 113ba40 commit 071c5e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/vscode/src/index.ts
Expand Up @@ -7,6 +7,7 @@ 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)
Expand Down Expand Up @@ -49,18 +50,24 @@ export async function activate(ext: ExtensionContext) {
if (Array.isArray(root) && root.length) {
const cwds = root.map(dir => path.resolve(projectPath, dir))

try {
const contextLoaders = await Promise.all(cwds.map(cwd => registerRoot(ext, status, cwd)))
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(contextLoaders.map(ctxLoader => ctxLoader.reload))
log.appendLine('✅ Reloaded.')
}),
)
}
catch (e: any) {
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
Expand Down
8 changes: 8 additions & 0 deletions packages/vscode/src/utils.ts
Expand Up @@ -122,3 +122,11 @@ export function isSubdir(parent: string, child: string) {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}

export function isFulfilled<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T> {
return result.status === 'fulfilled'
}

export function isRejected(result: PromiseSettledResult<unknown>): result is PromiseRejectedResult {
return result.status === 'rejected'
}

0 comments on commit 071c5e2

Please sign in to comment.