diff --git a/src/recipes/vscode/index.js b/src/recipes/vscode/index.js index f4d5de91fb7..da7974a751f 100644 --- a/src/recipes/vscode/index.js +++ b/src/recipes/vscode/index.js @@ -1,5 +1,6 @@ const { join } = require('path') +const execa = require('execa') const inquirer = require('inquirer') const { NETLIFYDEVLOG, NETLIFYDEVWARN, chalk, error, log } = require('../../utils/command-helpers') @@ -27,6 +28,26 @@ const getEdgeFunctionsPath = ({ config, repositoryRoot }) => const getSettingsPath = (repositoryRoot) => join(repositoryRoot, '.vscode', 'settings.json') +const hasDenoVSCodeExt = async () => { + const { stdout: extensions } = await execa('code', ['--list-extensions'], { stderr: 'inherit' }) + return extensions.split('\n').includes('denoland.vscode-deno') +} + +const getDenoVSCodeExt = async () => { + await execa('code', ['--install-extension', 'denoland.vscode-deno'], { stdio: 'inherit' }) +} + +const getDenoExtPrompt = () => { + const message = 'The Deno VS Code extension is recommended. Would you like to install it now?' + + return inquirer.prompt({ + type: 'confirm', + name: 'confirm', + message, + default: true, + }) +} + const run = async ({ config, repositoryRoot }) => { const { DenoBridge } = await import('@netlify/edge-bundler') const deno = new DenoBridge({ @@ -45,14 +66,22 @@ const run = async ({ config, repositoryRoot }) => { } try { - await writeSettings({ settings, settingsPath }) - - log(`${NETLIFYDEVLOG} VS Code settings file ${fileExists ? 'updated' : 'created'}.`) + if (!(await hasDenoVSCodeExt())) { + const { confirm: denoExtConfirm } = await getDenoExtPrompt() + if (denoExtConfirm) getDenoVSCodeExt() + } + } catch { log( - `${NETLIFYDEVWARN} If you don't have the Deno VS Code extension, install it by visiting ${chalk.blue( + `${NETLIFYDEVWARN} Unable to install Deno VS Code extension. To install it manually, visit ${chalk.blue( 'https://ntl.fyi/deno-vscode', )}.`, ) + } + + try { + await writeSettings({ settings, settingsPath }) + + log(`${NETLIFYDEVLOG} VS Code settings file ${fileExists ? 'updated' : 'created'}.`) } catch { error('Could not write VS Code settings file.') }