| 46 | |
| 47 | @injectable() |
| 48 | export class PackageJsonProvider implements IPackageJsonProvider { |
| 49 | constructor( |
| 50 | @inject(IFsUtils) private readonly fs: IFsUtils, |
| 51 | @inject(AnyLaunchConfiguration) private readonly config: AnyLaunchConfiguration, |
| 52 | ) {} |
| 53 | |
| 54 | /** |
| 55 | * Gets the package.json for the debugged program. |
| 56 | */ |
| 57 | public readonly getPath = once(async () => { |
| 58 | if (this.config.type !== DebugType.Node || this.config.request !== 'launch') { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | const dir = await nearestDirectoryContaining(this.fs, this.config.cwd, 'package.json'); |
| 63 | return dir ? join(dir, 'package.json') : undefined; |
| 64 | }); |
| 65 | |
| 66 | /** |
| 67 | * Gets the package.json contents for the debugged program. |
| 68 | */ |
| 69 | public readonly getContents = once(async () => { |
| 70 | const path = await this.getPath(); |
| 71 | if (!path) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | try { |
| 76 | const contents = await this.fs.readFile(path); |
| 77 | return JSON.parse(contents.toString()) as IPackageJson; |
| 78 | } catch { |
| 79 | return; |
| 80 | } |
| 81 | }); |
| 82 | } |