Skip to content

Commit e97a5f0

Browse files
committed
PR feedback: eliminate pathToPackageJsonFolder parameter
1 parent c28c261 commit e97a5f0

File tree

10 files changed

+21
-19
lines changed

10 files changed

+21
-19
lines changed

apps/api-documenter/src/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { PackageJsonLookup } from '@microsoft/node-core-library';
88

99
import { ApiDocumenterCommandLine } from './cli/ApiDocumenterCommandLine';
1010

11-
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
11+
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
1212

1313
console.log(os.EOL + colors.bold(`api-documenter ${myPackageVersion} `
1414
+ colors.cyan(' - http://aka.ms/extractor') + os.EOL));

apps/api-extractor/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"run",
1616
"-l"
1717
],
18-
"sourceMaps": false
18+
"sourceMaps": true
1919
},
2020
{
2121
"type": "node",

apps/api-extractor/src/extractor/Extractor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Extractor {
125125
* The NPM package version for the currently executing instance of the "\@microsoft/api-extractor" library.
126126
*/
127127
public static get version(): string {
128-
return PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').version;
128+
return PackageJsonLookup.loadOwnPackageJson(__dirname).version;
129129
}
130130

131131
/**

apps/api-extractor/src/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { PackageJsonLookup } from '@microsoft/node-core-library';
88

99
import { ApiExtractorCommandLine } from './cli/ApiExtractorCommandLine';
1010

11-
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
11+
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
1212

1313
console.log(os.EOL + colors.bold(`api-extractor ${myPackageVersion} `
1414
+ colors.cyan(' - http://aka.ms/extractor') + os.EOL));

apps/rush-lib/src/api/Rush.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class Rush {
6060
* This is the same as the Rush tool version for that release.
6161
*/
6262
public static get version(): string {
63-
return PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').version;
63+
return PackageJsonLookup.loadOwnPackageJson(__dirname).version;
6464
}
6565

6666
private static _printStartupBanner(isManaged: boolean): void {

apps/rush/src/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { MinimalRushConfiguration } from './MinimalRushConfiguration';
4949
// Load the configuration
5050
const configuration: MinimalRushConfiguration | undefined = MinimalRushConfiguration.loadFromDefaultLocation();
5151

52-
const currentPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
52+
const currentPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
5353

5454
let rushVersionToLoad: string | undefined = undefined;
5555

common/reviews/api/node-core-library.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ enum NewlineKind {
344344
class PackageJsonLookup {
345345
constructor(parameters?: IPackageJsonLookupParameters);
346346
clearCache(): void;
347-
static loadOwnPackageJson(dirnameOfCaller: string, pathToPackageJsonFolder: string): IPackageJson;
347+
static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson;
348348
loadPackageJson(jsonFilename: string): IPackageJson;
349349
tryGetPackageFolderFor(fileOrFolderPath: string): string | undefined;
350350
tryGetPackageJsonFilePathFor(fileOrFolderPath: string): string | undefined;

libraries/node-core-library/src/PackageJsonLookup.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface IPackageJsonLookupParameters {
3131
* @public
3232
*/
3333
export class PackageJsonLookup {
34-
private static _loadOwnPackageJsonCache: Map<string, IPackageJson> = new Map<string, IPackageJson>();
34+
private static _loadOwnPackageJsonLookup: PackageJsonLookup = new PackageJsonLookup({ loadExtraFields: true });
3535

3636
private _loadExtraFields: boolean = false;
3737

@@ -51,28 +51,30 @@ export class PackageJsonLookup {
5151
* This function provides a concise and efficient way for an NPM package to report metadata about itself.
5252
* For example, a tool might want to report its version.
5353
*
54-
* The `loadOwnPackageJson()` caches the result, under the assumption that a tool's own package.json will never
55-
* change during the lifetime of the process.
54+
* The `loadOwnPackageJson()` probes upwards from the caller's folder, expecting to find a package.json file,
55+
* which is assumed to be the caller's package. The result is cached, under the assumption that a tool's
56+
* own package.json (and intermediary folders) will never change during the lifetime of the process.
5657
*
5758
* @example
5859
* ```ts
5960
* // Report the version of our NPM package
60-
* const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').version;
61+
* const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
6162
* console.log(`Cool Tool - Version ${myPackageVersion}`);
6263
* ```
6364
*
6465
* @param dirnameOfCaller - The NodeJS `__dirname` macro for the caller.
65-
* @param pathToPackageJson - A relative path to the caller's package.json file, omitting the "package.json" part.
6666
* @returns This function always returns a valid `IPackageJson` object. If any problems are encountered during
6767
* loading, an exception will be thrown instead.
6868
*/
69-
public static loadOwnPackageJson(dirnameOfCaller: string, pathToPackageJsonFolder: string): IPackageJson {
70-
const packageJsonPath: string = path.join(dirnameOfCaller, pathToPackageJsonFolder, FileConstants.PackageJson);
71-
let packageJson: IPackageJson | undefined = PackageJsonLookup._loadOwnPackageJsonCache.get(packageJsonPath);
69+
public static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson {
70+
const packageJson: IPackageJson | undefined = PackageJsonLookup._loadOwnPackageJsonLookup
71+
.tryLoadPackageJsonFor(dirnameOfCaller);
72+
7273
if (packageJson === undefined) {
73-
packageJson = JsonFile.load(packageJsonPath) as IPackageJson;
74-
PackageJsonLookup._loadOwnPackageJsonCache.set(packageJsonPath, packageJson);
74+
throw new Error(`PackageJsonLookup.loadOwnPackageJson() failed to find the caller's package.json.`
75+
+ ` The __dirname was: ${dirnameOfCaller}`);
7576
}
77+
7678
return packageJson;
7779
}
7880

libraries/node-core-library/src/test/PackageJsonLookup.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('PackageJsonLookup', () => {
1111
describe('basic tests', () => {
1212

1313
test('', () => {
14-
expect(PackageJsonLookup.loadOwnPackageJson(__dirname, '../..').name).toEqual('@microsoft/node-core-library');
14+
expect(PackageJsonLookup.loadOwnPackageJson(__dirname).name).toEqual('@microsoft/node-core-library');
1515
});
1616

1717
test('tryLoadPackageJsonFor() test', () => {

stack/rush-stack/src/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
import { RushStackCommandLine } from './cli/RushStackCommandLine';
1111

12-
const currentPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname, '..').version;
12+
const currentPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
1313

1414
console.log(os.EOL + colors.bold(`rush-stack ${currentPackageVersion} `
1515
+ colors.cyan(' - http://rushstack.io') + os.EOL));

0 commit comments

Comments
 (0)