Skip to content

Commit d77d016

Browse files
authored
chore: add script to generate contributes (#13)
1 parent 60ae4f1 commit d77d016

File tree

7 files changed

+167
-49
lines changed

7 files changed

+167
-49
lines changed

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"configuration": {
3535
"title": "tsperf.tracer",
3636
"properties": {
37-
"tsperf.tracer.typescript-path-mode": {
37+
"tsperf.tracer.typescriptPathMode": {
3838
"type": "string",
3939
"default": "vscode-builtin",
4040
"description": "Use TypeScript from",
@@ -56,7 +56,7 @@
5656
"default": "npx tsc --generateTrace ${traceDir}",
5757
"description": "command to generate tsc traces"
5858
},
59-
"tsperf.tracer.typescript-path": {
59+
"tsperf.tracer.typescriptPath": {
6060
"type": "string",
6161
"default": "",
6262
"description": "Path to TypeScript. Must be specified if 'Use TypeScript from' is 'Use tracer TypeScript path setting'"
@@ -88,11 +88,13 @@
8888
"commands": [
8989
{
9090
"command": "tsperf.tracer.gotoTracePosition",
91-
"title": "Goto position in trace"
91+
"title": "Goto position in trace",
92+
"category": "Tracer"
9293
},
9394
{
9495
"command": "tsperf.tracer.openInBrowser",
9596
"title": "Open trace view in browser",
97+
"category": "Tracer",
9698
"icon": {
9799
"dark": "resources/todo.svg",
98100
"light": "resources/todo.svg"
@@ -101,6 +103,7 @@
101103
{
102104
"command": "tsperf.tracer.runTrace",
103105
"title": "tsc trace",
106+
"category": "Tracer",
104107
"icon": {
105108
"dark": "resources/todo.svg",
106109
"light": "resources/todo.svg"
@@ -109,13 +112,15 @@
109112
{
110113
"command": "tsperf.tracer.sendTrace",
111114
"title": "Send Trace to Trace Viewer",
112-
"when": "!notebookEditorFocused && editorLangId == 'json'"
115+
"when": "!notebookEditorFocused && editorLangId == 'json'",
116+
"category": "Tracer"
113117
}
114118
]
115119
},
116120
"scripts": {
121+
"generate:contributes": "jiti ./scripts/generate-contributes.ts",
117122
"pretest": "nr build && nr lint",
118-
"build": "nr ui:build && rollup -c",
123+
"build": "nr generate:contributes && nr ui:build && rollup -c",
119124
"dev": "nr build --watch --sourcemap",
120125
"ui:dev": "nuxt dev ui",
121126
"ui:build": "nuxt build ui",
@@ -133,7 +138,7 @@
133138
"@rollup/plugin-node-resolve": "^15.2.3",
134139
"@types/d3": "^7.4.3",
135140
"@types/node": "^20.12.12",
136-
"@types/vscode": "^1.70.0",
141+
"@types/vscode": "1.70.0",
137142
"@vscode/vsce": "^2.26.1",
138143
"bumpp": "^9.4.1",
139144
"d3": "^7.9.0",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pnpm typecheck &&
12
pnpm ui:build --sourcemap &&
23
pnpm build --sourcemap &&
34
code ./playground --extensionDevelopmentPath=. --inspect-extensions 3300

scripts/generate-contributes.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { readFileSync, writeFileSync } from 'node:fs'
2+
import type { configKeys } from '../src/constants'
3+
import { extPrefix } from '../src/constants'
4+
5+
type WithPrefix<T extends readonly [string, ...string[]]> = { [k in keyof T]: `${typeof extPrefix}.${T[k]}` }
6+
type PropertyConfigKey = WithPrefix<typeof configKeys>[number]
7+
8+
interface Command {
9+
command: `${typeof extPrefix}.${string}`
10+
title: string
11+
category: `Tracer${string}`
12+
icon?: any
13+
when?: string
14+
}
15+
16+
const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'))
17+
18+
pkg.contributes = {
19+
configuration: {
20+
title: extPrefix,
21+
properties: {
22+
'tsperf.tracer.typescriptPathMode': {
23+
type: 'string',
24+
default: 'vscode-builtin',
25+
description: 'Use TypeScript from',
26+
enum: [
27+
'vscode-builtin',
28+
'tsdk',
29+
'workspace',
30+
'explicit',
31+
],
32+
enumDescriptions: [
33+
'VsCode\'s built in TypeScript',
34+
'VsCode TSDK setting',
35+
'node_modules in you project',
36+
'Use tracer TypeScript path setting',
37+
],
38+
},
39+
'tsperf.tracer.traceCmd': {
40+
type: 'string',
41+
// eslint-disable-next-line no-template-curly-in-string
42+
default: 'npx tsc --generateTrace ${traceDir}',
43+
description: 'command to generate tsc traces',
44+
},
45+
'tsperf.tracer.typescriptPath': {
46+
type: 'string',
47+
default: '',
48+
description: 'Path to TypeScript. Must be specified if \'Use TypeScript from\' is \'Use tracer TypeScript path setting\'',
49+
},
50+
'tsperf.tracer.benchmarkIterations': {
51+
type: 'number',
52+
default: 3,
53+
description: 'Higher values reduce variance but increase benchmarking time',
54+
},
55+
'tsperf.tracer.restartTsserverOnIteration': {
56+
type: 'boolean',
57+
default: false,
58+
description: 'Restart tsserver on each iteration to avoid caching influincing measurements',
59+
},
60+
'tsperf.tracer.allIdentifiers': {
61+
type: 'boolean',
62+
default: false,
63+
description: 'Benchmark all allIdentifiers or only the first of each statement',
64+
},
65+
} satisfies Record<PropertyConfigKey, any>,
66+
},
67+
commandPalette: [
68+
{
69+
command: 'tsperf.tracer.sendTrace',
70+
when: '!notebookEditorFocused && editorLangId == \'json\'',
71+
group: 'tracer',
72+
},
73+
],
74+
commands: [
75+
{
76+
command: 'tsperf.tracer.gotoTracePosition',
77+
title: 'Goto position in trace',
78+
category: 'Tracer',
79+
},
80+
{
81+
command: 'tsperf.tracer.openInBrowser',
82+
title: 'Open trace view in browser',
83+
category: 'Tracer',
84+
icon: {
85+
dark: 'resources/todo.svg',
86+
light: 'resources/todo.svg',
87+
},
88+
},
89+
{
90+
command: 'tsperf.tracer.runTrace',
91+
title: 'tsc trace',
92+
category: 'Tracer',
93+
icon: {
94+
dark: 'resources/todo.svg',
95+
light: 'resources/todo.svg',
96+
},
97+
},
98+
{
99+
command: 'tsperf.tracer.sendTrace',
100+
title: 'Send Trace to Trace Viewer',
101+
when: '!notebookEditorFocused && editorLangId == \'json\'',
102+
category: 'Tracer',
103+
},
104+
] satisfies Command[],
105+
}
106+
107+
const outStr = JSON.stringify(pkg, null, 2)
108+
109+
writeFileSync('./package.json', `${outStr}\n`)

src/configuration.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import * as vscode from 'vscode'
22
import { setTsPath } from './tsUtil'
3-
4-
const configKeys = [
5-
'typescript-path',
6-
'typescript-path-mode',
7-
'benchmarkIterations',
8-
'restartTsserverOnIteration',
9-
'allIdentifiers',
10-
'traceCmd',
11-
] as const
12-
13-
type ConfigKey = typeof configKeys[number]
14-
const configKey = 'tsperf.tracer'
3+
import type { ConfigKey } from './constants'
4+
import { configKeys, extPrefix } from './constants'
155

166
const currentConfig = {
17-
'typescript-path': '',
18-
'typescript-path-mode': 'vscode-builtin',
19-
'benchmarkIterations': 3,
20-
'restartTsserverOnIteration': false,
21-
'allIdentifiers': false,
7+
typescriptPath: '',
8+
typescriptPathMode: 'vscode-builtin',
9+
benchmarkIterations: 3,
10+
restartTsserverOnIteration: false,
11+
allIdentifiers: false,
2212
// eslint-disable-next-line no-template-curly-in-string
23-
'traceCmd': 'npx tsc --generateTrace ${traceDir}',
13+
traceCmd: 'npx tsc --generateTrace ${traceDir}',
2414
} satisfies Record<ConfigKey, any>
2515

2616
export function getCurrentConfig() {
@@ -40,35 +30,36 @@ function isBoolean(x: unknown): x is boolean {
4030
}
4131

4232
const configValidate = {
43-
'typescript-path': isString,
44-
'typescript-path-mode': isString,
45-
'benchmarkIterations': isNumber,
46-
'restartTsserverOnIteration': isBoolean,
47-
'allIdentifiers': isBoolean,
48-
'traceCmd': isString,
33+
typescriptPath: isString,
34+
typescriptPathMode: isString,
35+
benchmarkIterations: isNumber,
36+
restartTsserverOnIteration: isBoolean,
37+
allIdentifiers: isBoolean,
38+
traceCmd: isString,
4939
} satisfies Record<ConfigKey, any>
5040

5141
function noop() {
5242
}
5343

5444
const configHandlers = {
55-
'typescript-path': (_value: string) => { currentConfig['typescript-path-mode'] = '!ForceUpdate' },
56-
'typescript-path-mode': (value: string) => { setTsPath(value, currentConfig['typescript-path']) },
57-
'benchmarkIterations': noop,
58-
'restartTsserverOnIteration': noop,
59-
'allIdentifiers': noop,
60-
'traceCmd': noop,
45+
typescriptPath: (_value: string) => { currentConfig.typescriptPathMode = '!ForceUpdate' },
46+
typescriptPathMode: (value: string) => { setTsPath(value, currentConfig.typescriptPath) },
47+
benchmarkIterations: noop,
48+
restartTsserverOnIteration: noop,
49+
allIdentifiers: noop,
50+
traceCmd: noop,
6151
} satisfies Record<ConfigKey, any>
6252

63-
let configuration = vscode.workspace.getConfiguration(configKey)
53+
let configuration = vscode.workspace.getConfiguration(extPrefix)
6454

6555
const afterConfigHandlers: [keys: ConfigKey[], handler: (config: typeof currentConfig) => void][] = []
6656

67-
export function updateConfig() {
57+
export function updateConfig(opts?: { force?: ConfigKey[] }) {
6858
const changedKeys: ConfigKey[] = []
6959
for (const key of configKeys) {
70-
const newValue = configuration.get(key)
71-
if (newValue !== undefined && newValue !== currentConfig[key]) {
60+
let newValue = configuration.get(key)
61+
if (opts?.force?.includes(key) || (newValue !== undefined && newValue !== currentConfig[key])) {
62+
newValue ??= currentConfig[key]
7263
changedKeys.push(key)
7364
if (!configValidate[key](newValue)) {
7465
vscode.window.showErrorMessage(`wrong type received for configuration item ${key}: ${newValue}`)
@@ -86,8 +77,8 @@ export function updateConfig() {
8677
}
8778

8879
vscode.workspace.onDidChangeConfiguration((change) => {
89-
if (change.affectsConfiguration(configKey)) {
90-
configuration = vscode.workspace.getConfiguration(configKey)
80+
if (change.affectsConfiguration(extPrefix)) {
81+
configuration = vscode.workspace.getConfiguration(extPrefix)
9182
updateConfig()
9283
}
9384
})

src/constants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const extPrefix = 'tsperf.tracer'
2+
3+
export const configKeys = [
4+
'typescriptPath',
5+
'typescriptPathMode',
6+
'benchmarkIterations',
7+
'restartTsserverOnIteration',
8+
'allIdentifiers',
9+
'traceCmd',
10+
] as const
11+
12+
export type ConfigKey = typeof configKeys[number]

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ function getTs() {
2828
export async function activate(context: vscode.ExtensionContext) {
2929
log('============extension activated============')
3030

31-
updateConfig()
31+
updateConfig({ force: ['typescriptPathMode'] })
3232

3333
getTs()
3434

35-
afterConfigUpdate(['typescript-path', 'typescript-path-mode'], getTs)
35+
afterConfigUpdate(['typescriptPath', 'typescriptPathMode'], getTs)
3636

3737
const collection = vscode.languages.createDiagnosticCollection('tsperf')
3838

0 commit comments

Comments
 (0)