-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) only show SvelteKit files context menu in kit project (#1771)
- Loading branch information
1 parent
c7fe395
commit c366e2f
Showing
2 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,83 @@ | ||
import { ExtensionContext } from 'vscode'; | ||
import { TextDecoder } from 'util'; | ||
import { ExtensionContext, commands, workspace } from 'vscode'; | ||
import { addGenerateKitRouteFilesCommand } from './generateFiles'; | ||
|
||
type ShowSvelteKitFilesContextMenuConfig = 'auto' | 'always' | 'never'; | ||
|
||
export function setupSvelteKit(context: ExtensionContext) { | ||
let contextMenuEnabled = false; | ||
context.subscriptions.push( | ||
workspace.onDidChangeConfiguration(() => { | ||
enableContextMenu(); | ||
}) | ||
); | ||
|
||
addGenerateKitRouteFilesCommand(context); | ||
enableContextMenu(); | ||
|
||
async function enableContextMenu() { | ||
const config = getConfig(); | ||
if (config === 'never') { | ||
if (contextMenuEnabled) { | ||
setEnableContext(false); | ||
} | ||
return; | ||
} | ||
|
||
if (config === 'always') { | ||
// Force on. The condition is defined in the extension manifest | ||
return; | ||
} | ||
|
||
const enabled = await detect(20); | ||
if (enabled !== contextMenuEnabled) { | ||
setEnableContext(enabled); | ||
contextMenuEnabled = enabled; | ||
} | ||
} | ||
} | ||
|
||
function getConfig() { | ||
return ( | ||
workspace | ||
.getConfiguration('svelte.ui.svelteKitFilesContextMenu') | ||
.get<ShowSvelteKitFilesContextMenuConfig>('enable') ?? 'auto' | ||
); | ||
} | ||
|
||
async function detect(nrRetries: number): Promise<boolean> { | ||
const packageJsonList = await workspace.findFiles('**/package.json', '**/node_modules/**'); | ||
|
||
if (packageJsonList.length === 0 && nrRetries > 0) { | ||
// We assume that the user has not setup their project yet, so try again after a while | ||
await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
return detect(nrRetries - 1); | ||
} | ||
|
||
for (const fileUri of packageJsonList) { | ||
try { | ||
const text = new TextDecoder().decode(await workspace.fs.readFile(fileUri)); | ||
const pkg = JSON.parse(text); | ||
const hasKit = Object.keys(pkg.devDependencies ?? {}) | ||
.concat(Object.keys(pkg.dependencies ?? {})) | ||
.includes('@sveltejs/kit'); | ||
|
||
if (hasKit) { | ||
return true; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function setEnableContext(enable: boolean) { | ||
// https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context | ||
commands.executeCommand( | ||
'setContext', | ||
'svelte.uiContext.svelteKitFilesContextMenu.enable', | ||
enable | ||
); | ||
} |