-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support discovering node version node 'node' npm module
Fixes #769
- Loading branch information
1 parent
2193e4c
commit 6daed2c
Showing
14 changed files
with
185 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { join } from 'path'; | ||
import { DebugType } from '../../common/contributionUtils'; | ||
import { IFsUtils } from '../../common/fsUtils'; | ||
import { once } from '../../common/objUtils'; | ||
import { nearestDirectoryContaining } from '../../common/urlUtils'; | ||
import { AnyLaunchConfiguration } from '../../configuration'; | ||
|
||
export interface IPackageJson { | ||
scripts?: { | ||
[name: string]: string; | ||
}; | ||
dependencies?: { | ||
[name: string]: string; | ||
}; | ||
devDependencies?: { | ||
[name: string]: string; | ||
}; | ||
} | ||
|
||
export interface IPackageJsonProvider { | ||
/** | ||
* Gets the path for the package.json associated with the current launched program. | ||
*/ | ||
getPath(): Promise<string | undefined>; | ||
|
||
/** | ||
* Gets the path for the package.json associated with the current launched program. | ||
*/ | ||
getContents(): Promise<IPackageJson | undefined>; | ||
} | ||
|
||
export const IPackageJsonProvider = Symbol('IPackageJsonProvider'); | ||
|
||
/** | ||
* A package.json provider that never returns path or contents. | ||
*/ | ||
export const noPackageJsonProvider = { | ||
getPath: () => Promise.resolve(undefined), | ||
getContents: () => Promise.resolve(undefined), | ||
}; | ||
|
||
@injectable() | ||
export class PackageJsonProvider implements IPackageJsonProvider { | ||
constructor( | ||
@inject(IFsUtils) private readonly fs: IFsUtils, | ||
@inject(AnyLaunchConfiguration) private readonly config: AnyLaunchConfiguration, | ||
) {} | ||
|
||
/** | ||
* Gets the package.json for the debugged program. | ||
*/ | ||
public readonly getPath = once(async () => { | ||
if (this.config.type !== DebugType.Node || this.config.request !== 'launch') { | ||
return; | ||
} | ||
|
||
const dir = await nearestDirectoryContaining(this.fs, this.config.cwd, 'package.json'); | ||
return dir ? join(dir, 'package.json') : undefined; | ||
}); | ||
|
||
/** | ||
* Gets the package.json contents for the debugged program. | ||
*/ | ||
public readonly getContents = once(async () => { | ||
const path = await this.getPath(); | ||
if (!path) { | ||
return; | ||
} | ||
|
||
try { | ||
const contents = await this.fs.readFile(path); | ||
return JSON.parse(contents.toString()) as IPackageJson; | ||
} catch { | ||
return; | ||
} | ||
}); | ||
} |
Oops, something went wrong.