Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use require.resolve instead path.join to resolve dependencies path #63

Merged
merged 8 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions core/common-util/src/ModuleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class ModuleConfigUtil {
return ref;
}

private static readModuleFromNodeModules(baseDir: string) {
public static readModuleFromNodeModules(baseDir: string) {
const ref: ModuleReference[] = [];
let pkgContent: string;
try {
Expand All @@ -154,11 +154,16 @@ export class ModuleConfigUtil {
return [];
}
const pkg = JSON.parse(pkgContent);
if (!fs.existsSync(path.join(baseDir, '/node_modules'))) {
return ref;
}
for (const dependencyKey of Object.keys(pkg.dependencies || {})) {
const absolutePkgPath = path.join(baseDir, '/node_modules', dependencyKey);
let packageJsonPath: string;
try {
// https://nodejs.org/api/packages.html#package-entry-points
// ignore cases where the package entry is exports but package.json is not exported
packageJsonPath = require.resolve(`${dependencyKey}/package.json`, { paths: [ baseDir ] });
killagu marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {
continue;
}
const absolutePkgPath = path.dirname(packageJsonPath);
const realPkgPath = fs.realpathSync(absolutePkgPath);
try {
if (this.readModuleNameSync(realPkgPath)) {
Expand Down
34 changes: 34 additions & 0 deletions core/common-util/test/ModuleConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('test/ModuleConfig.test.ts', () => {
it('should work', () => {
const fixturesPath = path.join(__dirname, './fixtures/apps/app-with-no-module-json');
const ref = ModuleConfigUtil.readModuleReference(fixturesPath);
console.log(333, ref);
killagu marked this conversation as resolved.
Show resolved Hide resolved
assert.deepStrictEqual(ref, [
{ path: path.join(fixturesPath, 'app/module-a') },
{ path: path.join(fixturesPath, 'app/module-b') },
Expand Down Expand Up @@ -62,4 +63,37 @@ describe('test/ModuleConfig.test.ts', () => {
});
});
});

describe('read package dependencies', () => {

it('should success if package.json not exist', async () => {
const dir = path.resolve(__dirname, './fixtures/monorepo/foo');
const ret = ModuleConfigUtil.readModuleFromNodeModules(dir);
assert.deepStrictEqual(ret, []);
});

it('should success whether dependencies entry has exported package.json', async () => {
const dir = path.resolve(__dirname, './fixtures/monorepo/packages/d');
const ret = ModuleConfigUtil.readModuleFromNodeModules(dir);
assert.deepStrictEqual(ret, [{
path: path.resolve(__dirname, './fixtures/monorepo/packages/d/node_modules/e'),
}]);
});

it('should read dependencies from self node_modules', async () => {
const dir = path.resolve(__dirname, './fixtures/monorepo/packages/a');
const ret = ModuleConfigUtil.readModuleFromNodeModules(dir);
assert.deepStrictEqual(ret, [{
path: path.resolve(__dirname, './fixtures/monorepo/packages/a/node_modules/c'),
}]);
});

it('should read dependencies from parent node_modules', async () => {
const dir = path.resolve(__dirname, './fixtures/monorepo/packages/b');
const ret = ModuleConfigUtil.readModuleFromNodeModules(dir);
assert.deepStrictEqual(ret, [{
path: path.resolve(__dirname, './fixtures/monorepo/packages/a'),
}]);
});
});
});
Empty file.
1 change: 1 addition & 0 deletions core/common-util/test/fixtures/monorepo/node_modules/a

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions core/common-util/test/fixtures/monorepo/packages/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"eggModule": {
"name": "a"
},
"dependencies": {
"c": "*"
},
"name": "b",
"author": ""
}
10 changes: 10 additions & 0 deletions core/common-util/test/fixtures/monorepo/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"eggModule": {
"name": "b"
},
"dependencies": {
"a": "*"
},
"name": "b",
"author": ""
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions core/common-util/test/fixtures/monorepo/packages/d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"eggModule": {
"name": "d"
},
"dependencies": {
"e": "*",
"f": "*"
},
"name": "d",
"author": ""
}