11/* eslint-disable import/no-dynamic-require, global-require, no-param-reassign */
22import fs from 'fs' ;
3- import { join } from 'path' ;
3+ import { dirname , join , resolve } from 'path' ;
44import merge from 'deepmerge' ;
55
66const defaultConfig = {
@@ -25,9 +25,7 @@ const defaultConfig = {
2525 plugins : [ ] ,
2626} ;
2727
28- function getPackageConfigInPath ( path ) {
29- const packageJsonPath = join ( path , '..' , 'package.json' ) ;
30-
28+ function getConfigFromPackage ( packageJsonPath ) {
3129 if ( ! fs . existsSync ( packageJsonPath ) ) {
3230 return null ;
3331 }
@@ -37,19 +35,38 @@ function getPackageConfigInPath(path) {
3735 return { channel : pkg . name || defaultConfig . channel , ...pkg . nodeLogger } ;
3836}
3937
38+ function findPackageJson ( ) {
39+ let dir = resolve ( process . cwd ( ) ) ;
40+
41+ do {
42+ const pkgFile = join ( dir , 'package.json' ) ;
43+
44+ if ( ! fs . existsSync ( pkgFile ) || ! fs . statSync ( pkgFile ) . isFile ( ) ) {
45+ dir = join ( dir , '..' ) ;
46+ continue ;
47+ }
48+
49+ return pkgFile ;
50+ } while ( dir !== resolve ( dir , '..' ) ) ;
51+
52+ return null ;
53+ }
54+
4055function loadConfigFromPackage ( ) {
41- const { paths } = process . mainModule ;
56+ const pkgFile = findPackageJson ( ) ;
57+
58+ if ( ! pkgFile ) {
59+ return { path : null , config : { } } ;
60+ }
4261
43- const pkgConfig = paths
44- . map ( path => ( { path, config : getPackageConfigInPath ( path ) } ) )
45- . reverse ( )
46- . find ( config => ! ! config . config ) ;
62+ const config = getConfigFromPackage ( pkgFile ) ;
4763
48- if ( ! pkgConfig ) {
64+ if ( ! config ) {
4965 return { path : null , config : { } } ;
5066 }
5167
52- return pkgConfig ;
68+ const modulePath = join ( dirname ( pkgFile ) , 'node_modules' ) ;
69+ return { path : modulePath , config } ;
5370}
5471
5572function loadModule ( moduleName , path ) {
0 commit comments