使用VSCODE API观看项目依赖关系

发布于 2025-02-08 07:50:19 字数 172 浏览 2 评论 0原文

我正在编写VSCODE扩展程序,我需要一种方法来知道何时项目获得新的依赖性以触发某些操作。为此,我决定使用“ fs.watchfile”观看package.json文件。但是问题在于,FS仅在保存文件后才看到更改,并且需要一两秒钟。此外,如果用户手动添加新的依赖项。我想知道VSCODE是否具有一些内部API,可以比FS做得更好。

I am writing a VSCode extension and I need a way to know when a project gets a new dependency to trigger some action. For that, I decided to watch package.json file using 'fs.watchFile'. But the problem is that fs sees the change only after saving the file and it takes a second or two. Moreover, if a user adds a new dependency manually to package.json there is no change event until the user saves it. I wonder if VSCode has some internal API that will do it better than fs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2025-02-15 07:50:19

VSCODE具有vscode.workspace.createfilesystemwatcher的不错的实用程序。它比FS实现更好,因为当文件保存之前“肮脏”时也会触发它。可能的实施是:

const watcher = vscode.workspace.createFileSystemWatcher(
  packageJsonPath, // absolute path to package.json
  true, // ignore create events
  false, // don't ignore change events
  true, // ignore delete events
);
watcher.onDidChange(() => {
    // trigger some action
})

// when not needed
if (watcher) {
  watcher.dispose();
}

VSCode has a nice utility for that vscode.workspace.createFileSystemWatcher. It's better than fs implementation because it is also triggered when the file is 'dirty' before it is saved. Possible implementation is:

const watcher = vscode.workspace.createFileSystemWatcher(
  packageJsonPath, // absolute path to package.json
  true, // ignore create events
  false, // don't ignore change events
  true, // ignore delete events
);
watcher.onDidChange(() => {
    // trigger some action
})

// when not needed
if (watcher) {
  watcher.dispose();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文