Skip to content

Commit

Permalink
Merge pull request #507 from direnv/fix/watched-paths
Browse files Browse the repository at this point in the history
direnv: Keep watches internal, expose finding their paths
  • Loading branch information
mkhl authored Jun 1, 2023
2 parents 8d084f9 + fc11ca9 commit 36f498b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]
### Fixed
- Avoid relying on direnv's internal representations

## [0.12.1] - 2023-04-24
### Fixed
Expand Down
10 changes: 7 additions & 3 deletions src/direnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export class CommandNotFoundError extends Error {

export type Data = Map<string, string>

export type Watch = Record<'Path', string>
type Watch = {
path?: string
['Path']?: string
}

export type Stdio = {
stdout: string
Expand Down Expand Up @@ -128,9 +131,10 @@ export function isInternal(key: string) {
return key.startsWith('DIRENV_')
}

export function watches(data?: Data): Watch[] {
export function watchedPaths(data?: Data): string[] {
if (data === undefined) return []
return decode(data.get('DIRENV_WATCHES')) ?? []
const watches: Watch[] = decode(data.get('DIRENV_WATCHES')) ?? []
return watches.map((it) => it.path ?? it.Path).filter((it): it is string => !!it)
}

function decode<T>(gzenv?: string): T | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Direnv implements vscode.Disposable {
private updateWatchers(data?: Data) {
this.watchers.dispose()
this.watchers = vscode.Disposable.from(
...direnv.watches(data).map((it) => this.createWatcher(it.Path)),
...direnv.watchedPaths(data).map((it) => this.createWatcher(it)),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/direnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('direnv', () => {
it('lists the .envrc file as watched', async () => {
await direnv.allow(file)
const data = await direnv.dump()
const paths = direnv.watches(data).map((it) => it.Path)
const paths = direnv.watchedPaths(data)
assert.ok(paths.includes(file))
})

Expand Down

0 comments on commit 36f498b

Please sign in to comment.