Skip to content

Commit 9ffcbfb

Browse files
committed
refactor: Fixed config package.json logic
1 parent fe76ed1 commit 9ffcbfb

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

projects/patch/src/plugin/plugin.ts

+15-26
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,19 @@ namespace tsp {
2222
// region: Helpers
2323
/* ****************************************************** */
2424

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);
2730

2831
const seenPaths = new Set<string>();
2932
while (currentDir !== path.parse(currentDir).root) {
3033
if (seenPaths.has(currentDir)) return undefined;
3134
seenPaths.add(currentDir);
3235

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-
3836
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;
4338

4439
currentDir = path.resolve(currentDir, '..');
4540
}
@@ -78,26 +73,20 @@ namespace tsp {
7873
this.tsConfigPath = config.tsConfig && path.resolve(resolveBaseDir, config.tsConfig);
7974
const entryFilePath = require.resolve(configTransformValue, { paths: [ resolveBaseDir ] });
8075
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) {}
8576

8677
/* Get module PluginPackageConfig */
87-
if (packageFilePath) {
88-
let pluginPackageConfig: PluginPackageConfig | undefined;
78+
let pluginPackageConfig: PluginPackageConfig | undefined;
8979

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 };
9484

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;
10087
}
88+
89+
this.packageConfig = pluginPackageConfig;
10190
}
10291

10392
private validateConfig() {

0 commit comments

Comments
 (0)