@@ -22,24 +22,19 @@ namespace tsp {
22
22
// region: Helpers
23
23
/* ****************************************************** */
24
24
25
- function getModulePackagePath ( transformerPath : string , packageFilePath : string ) : string | undefined {
26
- let currentDir = path . dirname ( transformerPath ) ;
25
+ // Could likely fail if the plugin is in a symlinked directory or the package's main file is in a
26
+ // directory above the package.json – however, I believe that the walking up method used here is the common
27
+ // approach, so we'll consider these acceptable edge cases for now.
28
+ function getPackagePath ( entryFilePath : string ) : string | undefined {
29
+ let currentDir = path . dirname ( entryFilePath ) ;
27
30
28
31
const seenPaths = new Set < string > ( ) ;
29
32
while ( currentDir !== path . parse ( currentDir ) . root ) {
30
33
if ( seenPaths . has ( currentDir ) ) return undefined ;
31
34
seenPaths . add ( currentDir ) ;
32
35
33
- // Could likely fail if the transformer is in a symlinked directory or the package's main file is in a
34
- // directory above the package.json – however, I believe that the walking up method used here is the common
35
- // approach, so we'll consider these acceptable edge cases for now.
36
- if ( path . relative ( currentDir , packageFilePath ) . startsWith ( '..' ) ) return undefined ;
37
-
38
36
const potentialPkgPath = path . join ( currentDir , 'package.json' ) ;
39
- // If the project's package matches the transformer's package, return it
40
- if ( fs . existsSync ( potentialPkgPath ) ) {
41
- return potentialPkgPath === packageFilePath ? packageFilePath : undefined ;
42
- }
37
+ if ( fs . existsSync ( potentialPkgPath ) ) return potentialPkgPath ;
43
38
44
39
currentDir = path . resolve ( currentDir , '..' ) ;
45
40
}
@@ -78,26 +73,20 @@ namespace tsp {
78
73
this . tsConfigPath = config . tsConfig && path . resolve ( resolveBaseDir , config . tsConfig ) ;
79
74
const entryFilePath = require . resolve ( configTransformValue , { paths : [ resolveBaseDir ] } ) ;
80
75
this . entryFilePath = entryFilePath ;
81
- let packageFilePath : string | undefined ;
82
- try {
83
- packageFilePath = require . resolve ( path . join ( path . dirname ( entryFilePath ) , 'package.json' ) , { paths : [ resolveBaseDir ] } ) ;
84
- } catch ( e ) { }
85
76
86
77
/* Get module PluginPackageConfig */
87
- if ( packageFilePath ) {
88
- let pluginPackageConfig : PluginPackageConfig | undefined ;
78
+ let pluginPackageConfig : PluginPackageConfig | undefined ;
89
79
90
- const modulePackagePath = getModulePackagePath ( entryFilePath , packageFilePath ) ;
91
- if ( modulePackagePath ) {
92
- const modulePkgJsonContent = fs . readFileSync ( modulePackagePath , 'utf8' ) ;
93
- const modulePkgJson = JSON . parse ( modulePkgJsonContent ) as { tsp ?: PluginPackageConfig } ;
80
+ const modulePackagePath = getPackagePath ( entryFilePath ) ;
81
+ if ( modulePackagePath ) {
82
+ const modulePkgJsonContent = fs . readFileSync ( modulePackagePath , 'utf8' ) ;
83
+ const modulePkgJson = JSON . parse ( modulePkgJsonContent ) as { tsp ?: PluginPackageConfig } ;
94
84
95
- pluginPackageConfig = modulePkgJson . tsp ;
96
- if ( pluginPackageConfig === null || typeof pluginPackageConfig !== 'object' ) pluginPackageConfig = undefined ;
97
- }
98
-
99
- this . packageConfig = pluginPackageConfig ;
85
+ pluginPackageConfig = modulePkgJson . tsp ;
86
+ if ( pluginPackageConfig === null || typeof pluginPackageConfig !== 'object' ) pluginPackageConfig = undefined ;
100
87
}
88
+
89
+ this . packageConfig = pluginPackageConfig ;
101
90
}
102
91
103
92
private validateConfig ( ) {
0 commit comments