Skip to content

Commit

Permalink
feat(vscode): remove unocss.root and use auto-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Sep 3, 2023
1 parent 6a81c62 commit 88b9538
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"jiti": "^1.18.2",
"prettier": "^2.8.8",
"tsup": "^7.0.0",
"unconfig": "^0.3.9"
"unconfig": "^0.3.9",
"find-up": "^6.3.0"
}
}
27 changes: 19 additions & 8 deletions packages/vscode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { registerAutoComplete } from './autocomplete'
import { ContextLoader } from './contextLoader'
import { registerSelectionStyle } from './selectionStyle'
import { isFulfilled, isRejected } from './utils'
import { findUp } from 'find-up';

async function registerRoot(ext: ExtensionContext, status: StatusBarItem, cwd: string) {
const contextLoader = new ContextLoader(cwd)

await contextLoader.ready

const hasConfig = await contextLoader.loadContextInDirectory(cwd)
Expand Down Expand Up @@ -49,7 +49,6 @@ export async function activate(ext: ExtensionContext) {

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)),
)
Expand All @@ -72,13 +71,19 @@ export async function activate(ext: ExtensionContext) {
}
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 cacheMap = new Set()

const registerUnocss = async () => {
const url = window.activeTextEditor?.document.uri.fsPath
if (!url) return
if (/node_modules/.test(url)) return
const target = await findUp(['package.json'], { cwd: url })
if (!target) return
const cwd = target.slice(0, -13)
if (cacheMap.has(cwd)) return
cacheMap.add(cwd)
const contextLoader = await registerRoot(ext, status, cwd)

ext.subscriptions.push(
Expand All @@ -89,9 +94,15 @@ export async function activate(ext: ExtensionContext) {
}),
)
}


try {
await registerUnocss()
ext.subscriptions.push(window.onDidChangeActiveTextEditor(registerUnocss))
}
catch (e: any) {
log.appendLine(String(e.stack ?? e))
}
}

export function deactivate() {}
export function deactivate() { }

0 comments on commit 88b9538

Please sign in to comment.