|
| 1 | +import { join } from 'path' |
| 2 | +import { Position, Range, ViewColumn, WorkspaceEdit, window, workspace } from 'vscode' |
| 3 | + |
| 4 | +import { DocumentationPath } from '../constants' |
| 5 | +import { handleMessageItems } from '../helpers/handleMessageItems' |
| 6 | +import { isEmpty } from '../helpers/isEmpty' |
| 7 | +import { GlobalStateKey, getGlobalStateManager } from '../libs/GlobalStateManager' |
| 8 | +import { MessageItemType, type MessageButton } from '../types' |
| 9 | +import { getUserWorkspaceRootPath } from '../utils/getUserWorkspaceRootPath' |
| 10 | +import { showDocumentation } from '../utils/showDocumentation' |
| 11 | + |
| 12 | +const HIDE_MESSAGE_BUTTON: MessageButton = { |
| 13 | + action: async () => { |
| 14 | + await getGlobalStateManager().set(GlobalStateKey.ONBOARDING__HIDE_WELCOME_PAGE, true) |
| 15 | + }, |
| 16 | + label: 'Never show welcome page again', |
| 17 | + type: MessageItemType.BUTTON, |
| 18 | +} |
| 19 | + |
| 20 | +export async function welcome() { |
| 21 | + const isWelcomePageHidden = await getGlobalStateManager().get(GlobalStateKey.ONBOARDING__HIDE_WELCOME_PAGE) |
| 22 | + if (isWelcomePageHidden) { |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + await showDocumentation(DocumentationPath.WELCOME) |
| 27 | + |
| 28 | + const workspaceSettingsPath = join(getUserWorkspaceRootPath(), '.vscode', 'settings.json') |
| 29 | + const workspaceSettingsDocument = await workspace.openTextDocument(workspaceSettingsPath) |
| 30 | + const workspaceSettingsText = workspaceSettingsDocument.getText() |
| 31 | + |
| 32 | + if (!workspaceSettingsText.includes('openai-forge.customEvaluators')) { |
| 33 | + const workspaceSettings = !isEmpty(workspaceSettingsText) ? JSON.parse(workspaceSettingsText) : {} |
| 34 | + const suggestedWorkspaceSettings = JSON.stringify( |
| 35 | + { |
| 36 | + ...workspaceSettings, |
| 37 | + 'openai-forge.customEvaluators': [ |
| 38 | + { |
| 39 | + command: 'npm', |
| 40 | + commandArgs: ['run', 'build'], |
| 41 | + extensions: ['.js', '.jsx', '.ts', '.tsx'], |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + // eslint-disable-next-line no-null/no-null |
| 46 | + null, |
| 47 | + 2, |
| 48 | + ) |
| 49 | + const workspaceSettingsEdit = new WorkspaceEdit() |
| 50 | + workspaceSettingsEdit.replace( |
| 51 | + workspaceSettingsDocument.uri, |
| 52 | + new Range( |
| 53 | + new Position(0, 0), |
| 54 | + workspaceSettingsDocument.lineAt(workspaceSettingsDocument.lineCount - 1).range.end, |
| 55 | + ), |
| 56 | + suggestedWorkspaceSettings, |
| 57 | + ) |
| 58 | + |
| 59 | + await window.showTextDocument(workspaceSettingsDocument, ViewColumn.Two) |
| 60 | + await workspace.applyEdit(workspaceSettingsEdit) |
| 61 | + |
| 62 | + window |
| 63 | + .showInformationMessage("Once you've read the welcome page:", HIDE_MESSAGE_BUTTON.label) |
| 64 | + .then(handleMessageItems([HIDE_MESSAGE_BUTTON])) |
| 65 | + } |
| 66 | +} |
0 commit comments