Skip to content

Commit 9cf5d08

Browse files
committed
Further optimization
1 parent a91fc88 commit 9cf5d08

File tree

12 files changed

+83
-12
lines changed

12 files changed

+83
-12
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
// eslint-disable-next-line
5+
const importLazy = require('import-lazy')(require);
6+
47
import * as path from 'path';
5-
import * as _ from 'lodash';
8+
const _ = importLazy('lodash');
69
import { FileSystem, JsonFile, JsonObject } from '@rushstack/node-core-library';
710

811
import { PackageManagerName } from './packageManager/PackageManager';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Rush {
106106
* This is the same as the Rush tool version for that release.
107107
*/
108108
public static get version(): string {
109-
return PackageJsonLookup.loadOwnPackageJson(__dirname).version;
109+
return PackageJsonLookup.getOwnPackageJsonVersion(__dirname);
110110
}
111111

112112
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
// eslint-disable-next-line
5+
const importLazy = require('import-lazy')(require);
6+
47
console.log('VersionPolicy.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
5-
import { cloneDeep } from 'lodash';
8+
const _ = importLazy('lodash');
69
console.log('VersionPolicy.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
710
import * as semver from 'semver';
811
console.log('VersionPolicy.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
@@ -328,7 +331,7 @@ export class LockStepVersionPolicy extends VersionPolicy {
328331
}
329332

330333
private _updatePackageVersion(project: IPackageJson, newVersion: semver.SemVer): IPackageJson {
331-
const updatedProject: IPackageJson = cloneDeep(project);
334+
const updatedProject: IPackageJson = _.cloneDeep(project);
332335
updatedProject.version = newVersion.format();
333336
return updatedProject;
334337
}
@@ -387,7 +390,7 @@ export class IndividualVersionPolicy extends VersionPolicy {
387390
if (this.lockedMajor) {
388391
const version: semver.SemVer = new semver.SemVer(project.version);
389392
if (version.major < this.lockedMajor) {
390-
const updatedProject: IPackageJson = cloneDeep(project);
393+
const updatedProject: IPackageJson = _.cloneDeep(project);
391394
updatedProject.version = `${this._lockedMajor}.0.0`;
392395
return updatedProject;
393396
} else if (version.major > this.lockedMajor) {

apps/rush-lib/src/logic/ShrinkwrapFileFactory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
console.log('ShrinkwrapFileFactory.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
45
import { PackageManagerName } from '../api/packageManager/PackageManager';
6+
console.log('ShrinkwrapFileFactory.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
57
import { BaseShrinkwrapFile } from './base/BaseShrinkwrapFile';
8+
console.log('ShrinkwrapFileFactory.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
69
import { NpmShrinkwrapFile } from './npm/NpmShrinkwrapFile';
10+
console.log('ShrinkwrapFileFactory.ts : 4: ' + (new Date().getTime() % 20000) / 1000.0);
711
import { PnpmShrinkwrapFile } from './pnpm/PnpmShrinkwrapFile';
12+
console.log('ShrinkwrapFileFactory.ts : 5: ' + (new Date().getTime() % 20000) / 1000.0);
813
import { YarnShrinkwrapFile } from './yarn/YarnShrinkwrapFile';
14+
console.log('ShrinkwrapFileFactory.ts : 6: ' + (new Date().getTime() % 20000) / 1000.0);
915
import { PackageManagerOptionsConfigurationBase, PnpmOptionsConfiguration } from '../api/RushConfiguration';
16+
console.log('ShrinkwrapFileFactory.ts : 7: ' + (new Date().getTime() % 20000) / 1000.0);
1017

1118
export class ShrinkwrapFileFactory {
1219
public static getShrinkwrapFile(

apps/rush-lib/src/logic/Telemetry.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
// eslint-disable-next-line
5+
const importLazy = require('import-lazy')(require);
6+
47
import * as fs from 'fs';
58
import * as path from 'path';
6-
import { cloneDeep } from 'lodash';
9+
const _ = importLazy('lodash');
710

811
import { RushConfiguration } from '../api/RushConfiguration';
912
import { Rush } from '../api/Rush';
@@ -40,7 +43,7 @@ export class Telemetry {
4043
if (!this._enabled) {
4144
return;
4245
}
43-
const data: ITelemetryData = cloneDeep(telemetryData);
46+
const data: ITelemetryData = _.cloneDeep(telemetryData);
4447
data.timestamp = data.timestamp || new Date().getTime();
4548
data.platform = data.platform || process.platform;
4649
data.rushVersion = data.rushVersion || Rush.version;

apps/rush-lib/src/logic/VersionManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
// eslint-disable-next-line
5+
const importLazy = require('import-lazy')(require);
6+
47
import * as path from 'path';
58
import * as semver from 'semver';
6-
import { cloneDeep } from 'lodash';
9+
const _ = importLazy('lodash');
710
import { IPackageJson, JsonFile, FileConstants } from '@rushstack/node-core-library';
811

912
import { VersionPolicy, BumpType, LockStepVersionPolicy } from '../api/VersionPolicy';
@@ -185,7 +188,7 @@ export class VersionManager {
185188
let clonedProject: IPackageJson | undefined = this._updatedProjects.get(rushProject.packageName);
186189
let projectVersionChanged: boolean = true;
187190
if (!clonedProject) {
188-
clonedProject = cloneDeep(rushProject.packageJson);
191+
clonedProject = _.cloneDeep(rushProject.packageJson);
189192
projectVersionChanged = false;
190193
}
191194
this._updateProjectAllDependencies(rushProject, clonedProject!, projectVersionChanged);

apps/rush-lib/src/logic/npm/NpmShrinkwrapFile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
import * as os from 'os';
55

6+
console.log('NpmShrinkwrapFile.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
67
import { JsonFile, FileSystem, InternalError } from '@rushstack/node-core-library';
8+
console.log('NpmShrinkwrapFile.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
79

810
import { BaseShrinkwrapFile } from '../base/BaseShrinkwrapFile';
11+
console.log('NpmShrinkwrapFile.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
912
import { DependencySpecifier } from '../DependencySpecifier';
13+
console.log('NpmShrinkwrapFile.ts : 4: ' + (new Date().getTime() % 20000) / 1000.0);
1014

1115
interface INpmShrinkwrapDependencyJson {
1216
version: string;

apps/rush-lib/src/logic/yarn/YarnShrinkwrapFile.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
// eslint-disable-next-line
5+
const importLazy = require('import-lazy')(require);
6+
7+
console.log('YarnShrinkwrapFile.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
48
import * as os from 'os';
5-
import * as lockfile from '@yarnpkg/lockfile';
9+
console.log('YarnShrinkwrapFile.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
10+
// eslint-disable-next-line
11+
const lockfile = importLazy('@yarnpkg/lockfile');
12+
console.log('YarnShrinkwrapFile.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
613
import { BaseShrinkwrapFile } from '../base/BaseShrinkwrapFile';
14+
console.log('YarnShrinkwrapFile.ts : 4: ' + (new Date().getTime() % 20000) / 1000.0);
715
import { FileSystem, IParsedPackageNameOrError, InternalError } from '@rushstack/node-core-library';
16+
console.log('YarnShrinkwrapFile.ts : 5: ' + (new Date().getTime() % 20000) / 1000.0);
817
import { RushConstants } from '../RushConstants';
18+
console.log('YarnShrinkwrapFile.ts : 6: ' + (new Date().getTime() % 20000) / 1000.0);
919
import { DependencySpecifier } from '../DependencySpecifier';
20+
console.log('YarnShrinkwrapFile.ts : 7: ' + (new Date().getTime() % 20000) / 1000.0);
1021
import { PackageNameParsers } from '../../api/PackageNameParsers';
22+
console.log('YarnShrinkwrapFile.ts : 8: ' + (new Date().getTime() % 20000) / 1000.0);
23+
24+
interface IYarnLockfileParseResult {
25+
// eslint-disable-next-line
26+
object: any;
27+
}
1128

1229
/**
1330
* Used with YarnShrinkwrapFile._encodePackageNameAndSemVer() and _decodePackageNameAndSemVer().
@@ -146,7 +163,7 @@ export class YarnShrinkwrapFile extends BaseShrinkwrapFile {
146163

147164
public static loadFromFile(shrinkwrapFilename: string): YarnShrinkwrapFile | undefined {
148165
let shrinkwrapString: string;
149-
let shrinkwrapJson: lockfile.ParseResult;
166+
let shrinkwrapJson: IYarnLockfileParseResult;
150167
try {
151168
if (!FileSystem.exists(shrinkwrapFilename)) {
152169
return undefined; // file does not exist

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ export const enum NewlineKind {
496496
export class PackageJsonLookup {
497497
constructor(parameters?: IPackageJsonLookupParameters);
498498
clearCache(): void;
499+
// (undocumented)
500+
static getOwnPackageJsonVersion(dirnameOfCaller: string): string;
499501
loadNodePackageJson(jsonFilename: string): INodePackageJson;
500502
static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson;
501503
loadPackageJson(jsonFilename: string): IPackageJson;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
console.log('JsonFile.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
45
import * as os from 'os';
6+
console.log('JsonFile.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
57
import * as jju from 'jju';
8+
console.log('JsonFile.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
69

710
import { JsonSchema, IJsonSchemaErrorInfo, IJsonSchemaValidateOptions } from './JsonSchema';
11+
console.log('JsonFile.ts : 4: ' + (new Date().getTime() % 20000) / 1000.0);
812
import { Text, NewlineKind } from './Text';
13+
console.log('JsonFile.ts : 5: ' + (new Date().getTime() % 20000) / 1000.0);
914
import { FileSystem } from './FileSystem';
15+
console.log('JsonFile.ts : 6: ' + (new Date().getTime() % 20000) / 1000.0);
1016

1117
/**
1218
* Represents a JSON-serializable object whose type has not been determined yet.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
console.log('JsonSchema.ts : 1: ' + (new Date().getTime() % 20000) / 1000.0);
45
import * as os from 'os';
6+
console.log('JsonSchema.ts : 2: ' + (new Date().getTime() % 20000) / 1000.0);
57
import * as path from 'path';
6-
import Validator = require('z-schema');
8+
console.log('JsonSchema.ts : 3: ' + (new Date().getTime() % 20000) / 1000.0);
9+
import Validator = require('z-schema/dist/ZSchema-browser-min');
10+
console.log('JsonSchema.ts : 4: ' + (new Date().getTime() % 20000) / 1000.0);
711

812
import { JsonFile, JsonObject } from './JsonFile';
13+
console.log('JsonSchema.ts : 5: ' + (new Date().getTime() % 20000) / 1000.0);
914
import { FileSystem } from './FileSystem';
15+
console.log('JsonSchema.ts : 6: ' + (new Date().getTime() % 20000) / 1000.0);
1016

1117
interface ISchemaWithId {
1218
id: string | undefined;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,25 @@ export class PackageJsonLookup {
8282
* loading, an exception will be thrown instead.
8383
*/
8484
public static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson {
85+
console.log('PackageJsonLookup.loadOwnPackageJson() : 1: ' + (new Date().getTime() % 20000) / 1000.0);
8586
const packageJson:
8687
| IPackageJson
8788
| undefined = PackageJsonLookup._loadOwnPackageJsonLookup.tryLoadPackageJsonFor(dirnameOfCaller);
8889

90+
console.log('PackageJsonLookup.loadOwnPackageJson() : 2: ' + (new Date().getTime() % 20000) / 1000.0);
8991
if (packageJson === undefined) {
9092
throw new Error(
9193
`PackageJsonLookup.loadOwnPackageJson() failed to find the caller's package.json.` +
9294
` The __dirname was: ${dirnameOfCaller}`
9395
);
9496
}
9597

98+
console.log('PackageJsonLookup.loadOwnPackageJson() : 3: ' + (new Date().getTime() % 20000) / 1000.0);
9699
if (packageJson.version !== undefined) {
97100
return packageJson as IPackageJson;
98101
}
99102

103+
console.log('PackageJsonLookup.loadOwnPackageJson() : 4: ' + (new Date().getTime() % 20000) / 1000.0);
100104
const errorPath: string =
101105
PackageJsonLookup._loadOwnPackageJsonLookup.tryGetPackageJsonFilePathFor(dirnameOfCaller) ||
102106
'package.json';
@@ -106,6 +110,19 @@ export class PackageJsonLookup {
106110
);
107111
}
108112

113+
public static getOwnPackageJsonVersion(dirnameOfCaller: string): string {
114+
let parent: string = path.dirname(dirnameOfCaller);
115+
do {
116+
try {
117+
return require(path.resolve(dirnameOfCaller, 'package.json')).version;
118+
} catch {
119+
dirnameOfCaller = parent;
120+
parent = path.dirname(dirnameOfCaller);
121+
}
122+
} while (parent !== dirnameOfCaller);
123+
throw new Error("Couldn't get path");
124+
}
125+
109126
/**
110127
* Clears the internal file cache.
111128
* @remarks

0 commit comments

Comments
 (0)