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

[node-core-libary] Update PackageJsonLookup to only resolve to package.json files that contain a "name" field. #3559

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/node-core-library",
"comment": "Require at least a \"name\" field in package.json files",
iclanton marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch"
}
],
"packageName": "@rushstack/node-core-library"
}
26 changes: 21 additions & 5 deletions libraries/node-core-library/src/PackageJsonLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ export class PackageJsonLookup {
* if the `version` field is missing from the package.json file.
*/
public loadNodePackageJson(jsonFilename: string): INodePackageJson {
const packageJson: INodePackageJson | undefined = this._tryLoadNodePackageJson(jsonFilename);

if (!packageJson) {
throw new Error(`Error reading "${jsonFilename}":\n The required field "name" was not found`);
}

return packageJson;
}

// Try to load a package.json file as an INodePackageJson,
// returning undefined if the found file does not contain a `name` field.
iclanton marked this conversation as resolved.
Show resolved Hide resolved
private _tryLoadNodePackageJson(jsonFilename: string): INodePackageJson | undefined {
if (!FileSystem.exists(jsonFilename)) {
throw new Error(`Input file not found: ${jsonFilename}`);
iclanton marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -251,7 +263,7 @@ export class PackageJsonLookup {
// Make sure this is really a package.json file. CommonJS has fairly strict requirements,
// but NPM only requires "name" and "version"
if (!loadedPackageJson.name) {
throw new Error(`Error reading "${jsonFilename}":\n The required field "name" was not found`);
return undefined;
iclanton marked this conversation as resolved.
Show resolved Hide resolved
}

if (this._loadExtraFields) {
Expand Down Expand Up @@ -292,10 +304,14 @@ export class PackageJsonLookup {
return this._packageFolderCache.get(resolvedFileOrFolderPath);
}

// Is resolvedFileOrFolderPath itself a folder with a package.json file? If so, return it.
if (FileSystem.exists(path.join(resolvedFileOrFolderPath, FileConstants.PackageJson))) {
this._packageFolderCache.set(resolvedFileOrFolderPath, resolvedFileOrFolderPath);
return resolvedFileOrFolderPath;
// Is resolvedFileOrFolderPath itself a folder with a valid package.json file? If so, return it.
const packageJsonFilePath: string = path.join(resolvedFileOrFolderPath, FileConstants.PackageJson);
if (FileSystem.exists(packageJsonFilePath)) {
const packageJson: INodePackageJson | undefined = this._tryLoadNodePackageJson(packageJsonFilePath);
iclanton marked this conversation as resolved.
Show resolved Hide resolved
if (packageJson) {
this._packageFolderCache.set(resolvedFileOrFolderPath, resolvedFileOrFolderPath);
return resolvedFileOrFolderPath;
}
}

// Otherwise go up one level
Expand Down
17 changes: 17 additions & 0 deletions libraries/node-core-library/src/test/PackageJsonLookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,22 @@ describe(PackageJsonLookup.name, () => {

expect(foundFile).toEqual(path.join(foundFolder || '', FileConstants.PackageJson));
});

test(`${PackageJsonLookup.prototype.tryGetPackageFolderFor.name} test package with inner package.json with no name`, () => {
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
const sourceFilePath: string = path.join(
__dirname,
'./test-data/example-subdir-package-no-name/src/ExampleFile.txt'
);

// Example: C:\rushstack\libraries\node-core-library\src\test\example-subdir-package-no-name
const foundFolder: string | undefined = packageJsonLookup.tryGetPackageFolderFor(sourceFilePath);
expect(foundFolder).toBeDefined();
expect(foundFolder!.search(/[\\/]example-subdir-package-no-name$/i)).toBeGreaterThan(0);

const foundFile: string | undefined = packageJsonLookup.tryGetPackageJsonFilePathFor(sourceFilePath);

expect(foundFile).toEqual(path.join(foundFolder || '', FileConstants.PackageJson));
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "example-package",
"nonstandardField": 456
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

/**
* AEDoc for AliasClass
* @public
*/
export class AliasClass {
private readOnlyNumber: number;

/**
* AEDoc for aliasFunc()
* @internal
*/
public _aliasFunc(): void {
console.log('this is an internal API');
}

public aliasField: number;

public get shouldBeReadOnly(): number {
return this.readOnlyNumber;
}
}

class PrivateAliasClass {
public test(): void {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nonstandardField": 123
}