Skip to content

Commit 0fa6b0b

Browse files
committed
dev: auto auto plugin reload + status in statusbar
1 parent de548ee commit 0fa6b0b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

buildTsPlugin.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ts-check
22
import buildTsPlugin from '@zardoy/vscode-utils/build/buildTypescriptPlugin.js'
33
import { build, analyzeMetafile } from 'esbuild'
4+
import fs from 'fs'
45

56
const enableWatch = process.argv.includes('--watch')
67
await build({
@@ -29,6 +30,22 @@ const result = await buildTsPlugin('typescript', undefined, undefined, {
2930
js: 'let ts, tsFull;',
3031
// js: 'const log = (...args) => console.log(...args.map(a => JSON.stringify(a)))',
3132
},
33+
plugins: [
34+
{
35+
name: 'watch-notifier',
36+
setup(build) {
37+
const writeStatus = (/** @type {number} */ signal) => {
38+
fs.writeFileSync('./out/build_plugin_result', signal.toString())
39+
}
40+
build.onStart(() => {
41+
writeStatus(0)
42+
})
43+
build.onEnd(({ errors }) => {
44+
writeStatus(errors.length ? 2 : 1)
45+
})
46+
},
47+
},
48+
],
3249
})
3350

3451
// @ts-ignore

src/autoPluginReload.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as vscode from 'vscode'
2+
import fs from 'fs'
3+
import { extensionCtx } from 'vscode-framework'
4+
5+
export default () => {
6+
const status = vscode.window.createStatusBarItem('plugin-auto-reload', vscode.StatusBarAlignment.Left, 1000)
7+
status.show()
8+
const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(extensionCtx.extensionUri, 'build_plugin_result'))
9+
const updateStatus = uri => {
10+
const newStatus = fs.readFileSync(uri.fsPath, 'utf8')
11+
if (newStatus === '1') {
12+
void vscode.commands.executeCommand('typescript.restartTsServer')
13+
status.text = 'Latest'
14+
}
15+
16+
if (newStatus === '0') {
17+
status.text = 'Rebuilding'
18+
}
19+
20+
if (newStatus === '2') {
21+
status.text = 'Build errored'
22+
}
23+
}
24+
25+
watcher.onDidChange(updateStatus)
26+
watcher.onDidCreate(updateStatus)
27+
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
9696

9797
figIntegration()
9898
vueVolarSupport()
99+
100+
if (process.env.PLATFORM === 'node' && process.env.NODE_ENV === 'development') {
101+
require('./autoPluginReload').default()
102+
}
99103
}
100104

101105
export const activate = async () => {

0 commit comments

Comments
 (0)