Skip to content

Commit bf4cbab

Browse files
committed
Start server once we know any workspace folder needs it
1 parent 7afe226 commit bf4cbab

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

packages/vscode-tailwindcss/src/analyze.ts

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,53 @@ export async function anyWorkspaceFoldersNeedServer({ folders, token }: SearchOp
2929
if (typeof configFilePath === 'object' && Object.values(configFilePath).length > 0) return true
3030
}
3131

32-
let configs: Array<() => Thenable<Uri[]>> = []
33-
let stylesheets: Array<() => Thenable<Uri[]>> = []
32+
// If any search returns that it needs a workspace then the server needs to be started
33+
// and the remainder of the searches will be cancelled
34+
let searches = folders.map((folder) =>
35+
workspaceFoldersNeedServer({ folder, token }).then((found) => {
36+
if (found) return true
3437

35-
for (let folder of folders) {
36-
let exclusions = getExcludePatterns(folder).flatMap((pattern) => braces.expand(pattern))
37-
let exclude = `{${exclusions.join(',').replace(/{/g, '%7B').replace(/}/g, '%7D')}}`
38-
39-
configs.push(() =>
40-
workspace.findFiles(
41-
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
42-
exclude,
43-
undefined,
44-
token,
45-
),
46-
)
47-
48-
stylesheets.push(() =>
49-
workspace.findFiles(new RelativePattern(folder, `**/${CSS_GLOB}`), exclude, undefined, token),
50-
)
38+
// We use `throw` so we can use Promise.any(…)
39+
throw new Error(DUMMY_ERROR_MESSAGE)
40+
}),
41+
)
42+
43+
const DUMMY_ERROR_MESSAGE = 'Workspace folder not needed'
44+
45+
try {
46+
return await Promise.any(searches)
47+
} catch (err) {
48+
for (let anErr of (err as AggregateError).errors ?? []) {
49+
if (typeof anErr === 'object' && err.message === DUMMY_ERROR_MESSAGE) {
50+
continue
51+
}
52+
53+
console.error(anErr)
54+
}
55+
56+
return false
5157
}
58+
}
59+
60+
export interface FolderSearchOptions {
61+
folder: WorkspaceFolder
62+
token: CancellationToken
63+
}
64+
65+
async function workspaceFoldersNeedServer({ folder, token }: FolderSearchOptions) {
66+
let exclusions = getExcludePatterns(folder).flatMap((pattern) => braces.expand(pattern))
67+
let exclude = `{${exclusions.join(',').replace(/{/g, '%7B').replace(/}/g, '%7D')}}`
5268

5369
// If we find a config file then we need the server
54-
let configUrls = await Promise.all(configs.map((fn) => fn()))
55-
for (let group of configUrls) {
56-
if (group.length > 0) {
57-
return true
58-
}
70+
let configs = await workspace.findFiles(
71+
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
72+
exclude,
73+
undefined,
74+
token,
75+
)
76+
77+
if (configs.length > 0) {
78+
return true
5979
}
6080

6181
// If we find a possibly-related stylesheet then we need the server
@@ -65,12 +85,16 @@ export async function anyWorkspaceFoldersNeedServer({ folders, token }: SearchOp
6585
// This is also, unfortunately, prone to starting the server unncessarily
6686
// in projects that don't use TailwindCSS so we do this one-by-one instead
6787
// of all at once to keep disk I/O low.
68-
let stylesheetUrls = await Promise.all(stylesheets.map((fn) => fn()))
69-
for (let group of stylesheetUrls) {
70-
for (let file of group) {
71-
if (await fileMayBeTailwindRelated(file)) {
72-
return true
73-
}
88+
let stylesheets = await workspace.findFiles(
89+
new RelativePattern(folder, `**/${CSS_GLOB}`),
90+
exclude,
91+
undefined,
92+
token,
93+
)
94+
95+
for (let file of stylesheets) {
96+
if (await fileMayBeTailwindRelated(file)) {
97+
return true
7498
}
7599
}
76100
}

0 commit comments

Comments
 (0)