Skip to content

Commit eee48c3

Browse files
committed
feat(new-command): replace global typescript with local version, so you can update TS globally for bug fixes and so on
1 parent 1c6e950 commit eee48c3

File tree

4 files changed

+90
-62
lines changed

4 files changed

+90
-62
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@
5555
"command": "copyFullType",
5656
"title": "Copy Full Type"
5757
},
58-
{
59-
"command": "pasteCodeWithImports",
60-
"title": "Paste Code with Imports"
61-
},
6258
{
6359
"command": "disableAllOptionalFeatures",
6460
"title": "Disable All Optional Features"
61+
},
62+
{
63+
"command": "replaceGlobalTypescriptWithLocalVersion",
64+
"title": "Replace Global Typescript with Local Version"
6565
}
6666
],
6767
"keybindings": [

src/extension.ts

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import vueVolarSupport from './vueVolarSupport'
1717
import moreCompletions from './moreCompletions'
1818
import { mergeSettingsFromScopes } from './mergeSettings'
1919
import codeActionProvider from './codeActionProvider'
20+
import nonTsCommands from './nonTsCommands'
2021

2122
let isActivated = false
2223
// let erroredStatusBarItem: vscode.StatusBarItem | undefined
@@ -91,7 +92,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
9192
}
9293

9394
export const activate = async () => {
94-
registerDisableOptionalFeaturesCommand()
95+
nonTsCommands()
9596
migrateSettings()
9697

9798
const possiblyActivateTsPlugin = async () => {
@@ -140,55 +141,3 @@ export const activate = async () => {
140141
})
141142
}
142143
}
143-
144-
const registerDisableOptionalFeaturesCommand = () => {
145-
registerExtensionCommand('disableAllOptionalFeatures', async () => {
146-
const config = vscode.workspace.getConfiguration(process.env.IDS_PREFIX, null)
147-
const toDisable: Array<[keyof Settings, any]> = []
148-
for (const optionalExperience of optionalExperiences) {
149-
const desiredKey = Array.isArray(optionalExperience) ? optionalExperience[0] : optionalExperience
150-
const desiredValue = Array.isArray(optionalExperience) ? optionalExperience[1] : false
151-
if (config.get(desiredKey) !== desiredValue) toDisable.push([desiredKey, desiredValue])
152-
}
153-
154-
const action = await vscode.window.showInformationMessage(
155-
`${toDisable.length} features are going to be disabled`,
156-
{ detail: '', modal: true },
157-
'Write to settings NOW',
158-
'Copy settings',
159-
)
160-
if (!action) return
161-
switch (action) {
162-
case 'Write to settings NOW': {
163-
for (const [key, value] of toDisable) {
164-
void config.update(key, value, vscode.ConfigurationTarget.Global)
165-
}
166-
167-
break
168-
}
169-
170-
case 'Copy settings': {
171-
await vscode.env.clipboard.writeText(JSON.stringify(Object.fromEntries(toDisable), undefined, 4))
172-
break
173-
}
174-
}
175-
})
176-
}
177-
178-
/** Experiences that are enabled out of the box */
179-
const optionalExperiences: Array<keyof ConditionalPick<Settings, boolean> | [keyof Settings, any]> = [
180-
'enableMethodSnippets',
181-
'removeUselessFunctionProps.enable',
182-
'patchToString.enable',
183-
['suggestions.keywordsInsertText', 'none'],
184-
'highlightNonFunctionMethods.enable',
185-
'markTsCodeActions.enable',
186-
['markTsCodeFixes.character', ''],
187-
'removeCodeFixes.enable',
188-
'removeDefinitionFromReferences',
189-
'removeImportsFromReferences',
190-
'miscDefinitionImprovement',
191-
'improveJsxCompletions',
192-
'objectLiteralCompletions.moreVariants',
193-
'codeActions.extractTypeInferName',
194-
]

src/nonTsCommands.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
]

src/specialCommands.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ export default () => {
289289
await vscode.env.clipboard.writeText(text)
290290
})
291291

292-
registerExtensionCommand('pasteCodeWithImports', async () => {
293-
const clipboard = await vscode.env.clipboard.readText()
294-
const lines = clipboard.split('\n')
295-
const lastImportLineIndex = lines.findIndex(line => line !== 'import')
296-
})
292+
// registerExtensionCommand('pasteCodeWithImports', async () => {
293+
// const clipboard = await vscode.env.clipboard.readText()
294+
// const lines = clipboard.split('\n')
295+
// const lastImportLineIndex = lines.findIndex(line => line !== 'import')
296+
// })
297297
}

0 commit comments

Comments
 (0)