Skip to content

Commit e0a2456

Browse files
author
Steven Weingärtner
committed
feat(logger): load package.json based on CWD to remove deprecated module code
1 parent 0782485 commit e0a2456

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

packages/logger/src/config.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/no-dynamic-require, global-require, no-param-reassign */
22
import fs from 'fs';
3-
import { join } from 'path';
3+
import { dirname, join, resolve } from 'path';
44
import merge from 'deepmerge';
55

66
const 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+
4055
function 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

5572
function loadModule(moduleName, path) {

packages/logger/src/config.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ jest.mock('/some/root/testpackage/package.json', () => ({
3737
},
3838
}), { virtual: true });
3939

40-
process.mainModule = {
41-
paths: [
42-
'/some/root/testpackage/node_modules',
43-
],
44-
};
45-
4640
describe('loadConfiguration', () => {
4741
beforeEach(() => {
4842
fs.existsSync.mockReturnValue(false);
43+
44+
jest.spyOn(process, 'cwd').mockReturnValue('/some/root/testpackage');
4945
});
5046

5147
describe('validation', () => {
@@ -147,6 +143,7 @@ describe('loadConfiguration', () => {
147143
describe('with package.json', () => {
148144
beforeEach(() => {
149145
fs.existsSync.mockReturnValue(true);
146+
fs.statSync.mockReturnValue({ isFile: jest.fn().mockReturnValue(true) });
150147
});
151148

152149
it('merges configuration with defaults', () => {

0 commit comments

Comments
 (0)