|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { ConditionalPick } from 'type-fest' |
| 3 | +import { registerExtensionCommand, Settings } from 'vscode-framework' |
| 4 | +import { getCurrentWorkspaceRoot } from '@zardoy/vscode-utils/build/fs' |
| 5 | +import { Utils } from 'vscode-uri' |
| 6 | +import { showQuickPick } from '@zardoy/vscode-utils/build/quickPick' |
| 7 | + |
| 8 | +// these commands doesn't require TS to be available |
| 9 | + |
| 10 | +export default () => { |
| 11 | + registerExtensionCommand('disableAllOptionalFeatures', async () => { |
| 12 | + const config = vscode.workspace.getConfiguration(process.env.IDS_PREFIX, null) |
| 13 | + const toDisable: Array<[keyof Settings, any]> = [] |
| 14 | + for (const optionalExperience of optionalExperiences) { |
| 15 | + const desiredKey = Array.isArray(optionalExperience) ? optionalExperience[0] : optionalExperience |
| 16 | + const desiredValue = Array.isArray(optionalExperience) ? optionalExperience[1] : false |
| 17 | + if (config.get(desiredKey) !== desiredValue) toDisable.push([desiredKey, desiredValue]) |
| 18 | + } |
| 19 | + |
| 20 | + const action = await vscode.window.showInformationMessage( |
| 21 | + `${toDisable.length} features are going to be disabled`, |
| 22 | + { detail: '', modal: true }, |
| 23 | + 'Write to settings NOW', |
| 24 | + 'Copy settings', |
| 25 | + ) |
| 26 | + if (!action) return |
| 27 | + switch (action) { |
| 28 | + case 'Write to settings NOW': { |
| 29 | + for (const [key, value] of toDisable) { |
| 30 | + void config.update(key, value, vscode.ConfigurationTarget.Global) |
| 31 | + } |
| 32 | + |
| 33 | + break |
| 34 | + } |
| 35 | + |
| 36 | + case 'Copy settings': { |
| 37 | + await vscode.env.clipboard.writeText(JSON.stringify(Object.fromEntries(toDisable), undefined, 4)) |
| 38 | + break |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + registerExtensionCommand('replaceGlobalTypescriptWithLocalVersion', async () => { |
| 44 | + const root = getCurrentWorkspaceRoot() |
| 45 | + const localTypeScript = Utils.joinPath(root.uri, 'node_modules/typescript') |
| 46 | + const globalTypeScript = Utils.joinPath(vscode.Uri.file(vscode.env.appRoot), 'extensions/node_modules/typescript') |
| 47 | + const { version: localVersion } = await vscode.workspace.fs |
| 48 | + .readFile(Utils.joinPath(localTypeScript, 'package.json')) |
| 49 | + .then(result => JSON.parse(result.toString())) |
| 50 | + const { version: globalVersion } = await vscode.workspace.fs |
| 51 | + .readFile(Utils.joinPath(globalTypeScript, 'package.json')) |
| 52 | + .then(result => JSON.parse(result.toString())) |
| 53 | + const result = await showQuickPick([`Replace global TS ${globalVersion} with local ${localVersion}`].map(x => ({ value: x, label: x }))) |
| 54 | + if (!result) return |
| 55 | + const paths = ['package.json', 'lib'] |
| 56 | + for (const path of paths) { |
| 57 | + // eslint-disable-next-line no-await-in-loop |
| 58 | + await vscode.workspace.fs.copy(Utils.joinPath(localTypeScript, path), Utils.joinPath(globalTypeScript, path), { overwrite: true }) |
| 59 | + } |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +/** Experiences that are enabled out of the box */ |
| 64 | +const optionalExperiences: Array<keyof ConditionalPick<Settings, boolean> | [keyof Settings, any]> = [ |
| 65 | + 'enableMethodSnippets', |
| 66 | + 'removeUselessFunctionProps.enable', |
| 67 | + 'patchToString.enable', |
| 68 | + ['suggestions.keywordsInsertText', 'none'], |
| 69 | + 'highlightNonFunctionMethods.enable', |
| 70 | + 'markTsCodeActions.enable', |
| 71 | + ['markTsCodeFixes.character', ''], |
| 72 | + 'removeCodeFixes.enable', |
| 73 | + 'removeDefinitionFromReferences', |
| 74 | + 'removeImportsFromReferences', |
| 75 | + 'miscDefinitionImprovement', |
| 76 | + 'improveJsxCompletions', |
| 77 | + 'objectLiteralCompletions.moreVariants', |
| 78 | + 'codeActions.extractTypeInferName', |
| 79 | +] |
0 commit comments