44'use strict' ;
55
66import * as path from 'path' ;
7- import { inject , injectable } from 'inversify' ;
7+ import * as fs from 'fs-extra' ;
8+ import { injectable } from 'inversify' ;
89import { CancellationToken , DebugConfiguration , WorkspaceFolder } from 'vscode' ;
910import { IDynamicDebugConfigurationService } from '../types' ;
10- import { IFileSystem } from '../../../common/platform/types' ;
11- import { IPathUtils } from '../../../common/types' ;
1211import { DebuggerTypeName } from '../../constants' ;
1312import { asyncFilter } from '../../../common/utils/arrayUtils' ;
1413
1514const workspaceFolderToken = '${workspaceFolder}' ;
1615
1716@injectable ( )
1817export class DynamicPythonDebugConfigurationService implements IDynamicDebugConfigurationService {
19- constructor ( @inject ( IFileSystem ) private fs : IFileSystem , @inject ( IPathUtils ) private pathUtils : IPathUtils ) { }
20-
18+ // eslint-disable-next-line class-methods-use-this
2119 public async provideDebugConfigurations (
2220 folder : WorkspaceFolder ,
2321 _token ?: CancellationToken ,
@@ -32,20 +30,20 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
3230 justMyCode : true ,
3331 } ) ;
3432
35- const djangoManagePath = await this . getDjangoPath ( folder ) ;
33+ const djangoManagePath = await DynamicPythonDebugConfigurationService . getDjangoPath ( folder ) ;
3634 if ( djangoManagePath ) {
3735 providers . push ( {
3836 name : 'Python: Django' ,
3937 type : DebuggerTypeName ,
4038 request : 'launch' ,
41- program : `${ workspaceFolderToken } ${ this . pathUtils . separator } ${ djangoManagePath } ` ,
39+ program : `${ workspaceFolderToken } ${ path . sep } ${ djangoManagePath } ` ,
4240 args : [ 'runserver' ] ,
4341 django : true ,
4442 justMyCode : true ,
4543 } ) ;
4644 }
4745
48- const flaskPath = await this . getFlaskPath ( folder ) ;
46+ const flaskPath = await DynamicPythonDebugConfigurationService . getFlaskPath ( folder ) ;
4947 if ( flaskPath ) {
5048 providers . push ( {
5149 name : 'Python: Flask' ,
@@ -62,12 +60,9 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
6260 } ) ;
6361 }
6462
65- let fastApiPath = await this . getFastApiPath ( folder ) ;
63+ let fastApiPath = await DynamicPythonDebugConfigurationService . getFastApiPath ( folder ) ;
6664 if ( fastApiPath ) {
67- fastApiPath = path
68- . relative ( folder . uri . fsPath , fastApiPath )
69- . replaceAll ( this . pathUtils . separator , '.' )
70- . replace ( '.py' , '' ) ;
65+ fastApiPath = path . relative ( folder . uri . fsPath , fastApiPath ) . replaceAll ( path . sep , '.' ) . replace ( '.py' , '' ) ;
7166 providers . push ( {
7267 name : 'Python: FastAPI' ,
7368 type : DebuggerTypeName ,
@@ -82,19 +77,19 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
8277 return providers ;
8378 }
8479
85- private async getDjangoPath ( folder : WorkspaceFolder ) {
80+ private static async getDjangoPath ( folder : WorkspaceFolder ) {
8681 const regExpression = / e x e c u t e _ f r o m _ c o m m a n d _ l i n e \( / ;
87- const possiblePaths = await this . getPossiblePaths (
82+ const possiblePaths = await DynamicPythonDebugConfigurationService . getPossiblePaths (
8883 folder ,
8984 [ 'manage.py' , '*/manage.py' , 'app.py' , '*/app.py' ] ,
9085 regExpression ,
9186 ) ;
9287 return possiblePaths . length ? path . relative ( folder . uri . fsPath , possiblePaths [ 0 ] ) : null ;
9388 }
9489
95- private async getFastApiPath ( folder : WorkspaceFolder ) {
90+ private static async getFastApiPath ( folder : WorkspaceFolder ) {
9691 const regExpression = / a p p \s * = \s * F a s t A P I \( / ;
97- const fastApiPaths = await this . getPossiblePaths (
92+ const fastApiPaths = await DynamicPythonDebugConfigurationService . getPossiblePaths (
9893 folder ,
9994 [ 'main.py' , 'app.py' , '*/main.py' , '*/app.py' , '*/*/main.py' , '*/*/app.py' ] ,
10095 regExpression ,
@@ -103,9 +98,9 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
10398 return fastApiPaths . length ? fastApiPaths [ 0 ] : null ;
10499 }
105100
106- private async getFlaskPath ( folder : WorkspaceFolder ) {
101+ private static async getFlaskPath ( folder : WorkspaceFolder ) {
107102 const regExpression = / a p p (?: l i c a t i o n ) ? \s * = \s * (?: f l a s k \. ) ? F l a s k \( | d e f \s + (?: c r e a t e | m a k e ) _ a p p \( / ;
108- const flaskPaths = await this . getPossiblePaths (
103+ const flaskPaths = await DynamicPythonDebugConfigurationService . getPossiblePaths (
109104 folder ,
110105 [ '__init__.py' , 'app.py' , 'wsgi.py' , '*/__init__.py' , '*/app.py' , '*/wsgi.py' ] ,
111106 regExpression ,
@@ -114,16 +109,23 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
114109 return flaskPaths . length ? flaskPaths [ 0 ] : null ;
115110 }
116111
117- private async getPossiblePaths ( folder : WorkspaceFolder , globPatterns : string [ ] , regex : RegExp ) : Promise < string [ ] > {
112+ private static async getPossiblePaths (
113+ folder : WorkspaceFolder ,
114+ globPatterns : string [ ] ,
115+ regex : RegExp ,
116+ ) : Promise < string [ ] > {
118117 const foundPathsPromises = ( await Promise . allSettled (
119118 globPatterns . map (
120- async ( pattern ) : Promise < string [ ] > => this . fs . search ( path . join ( folder . uri . fsPath , pattern ) ) ,
119+ async ( pattern ) : Promise < string [ ] > =>
120+ ( await fs . pathExists ( path . join ( folder . uri . fsPath , pattern ) ) )
121+ ? [ path . join ( folder . uri . fsPath , pattern ) ]
122+ : [ ] ,
121123 ) ,
122124 ) ) as { status : string ; value : [ ] } [ ] ;
123125 const possiblePaths : string [ ] = [ ] ;
124126 foundPathsPromises . forEach ( ( result ) => possiblePaths . push ( ...result . value ) ) ;
125127 const finalPaths = await asyncFilter ( possiblePaths , async ( possiblePath ) =>
126- regex . exec ( ( await this . fs . readFile ( possiblePath ) ) . toString ( ) ) ,
128+ regex . exec ( ( await fs . readFile ( possiblePath ) ) . toString ( ) ) ,
127129 ) ;
128130
129131 return finalPaths ;
0 commit comments