Skip to content

Commit 0d30870

Browse files
author
Nicholas Pape
authored
[rush] Cleanup some API's (microsoft#769)
* Expose a few API's that need to be public * Changefile * Merged mrctdrvs to trin * Missing changes * Add new ChangeManager class * Remove newline * add some comments * PR feedback * Add changefiles
1 parent b09f550 commit 0d30870

31 files changed

Lines changed: 214 additions & 96 deletions

File tree

apps/api-documenter/src/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import * as os from 'os';
55
import * as colors from 'colors';
66
import * as path from 'path';
77

8+
import { FileConstants } from '@microsoft/node-core-library';
9+
810
import { ApiDocumenterCommandLine } from './cli/ApiDocumenterCommandLine';
911

1012
const myPackageJsonFilename: string = path.resolve(path.join(
11-
__dirname, '..', 'package.json')
13+
__dirname, '..', FileConstants.PackageJson)
1214
);
1315
const myPackageJson: { version: string } = require(myPackageJsonFilename);
1416

apps/api-extractor/src/DocItemLoader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as os from 'os';
55
import * as path from 'path';
66
import {
77
JsonFile,
8-
FileSystem
8+
FileSystem,
9+
FileConstants
910
} from '@microsoft/node-core-library';
1011

1112
import {
@@ -40,7 +41,7 @@ export class DocItemLoader implements IReferenceResolver {
4041
* that we are compiling.
4142
*/
4243
constructor(projectFolder: string) {
43-
if (!FileSystem.exists(path.join(projectFolder, 'package.json'))) {
44+
if (!FileSystem.exists(path.join(projectFolder, FileConstants.PackageJson))) {
4445
throw new Error(`An NPM project was not found in the specified folder: ${projectFolder}`);
4546
}
4647

apps/api-extractor/src/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import * as os from 'os';
55
import * as colors from 'colors';
66
import * as path from 'path';
77

8+
import { FileConstants } from '@microsoft/node-core-library';
9+
810
import { ApiExtractorCommandLine } from './cli/ApiExtractorCommandLine';
911

1012
const myPackageJsonFilename: string = path.resolve(path.join(
11-
__dirname, '..', 'package.json')
13+
__dirname, '..', FileConstants.PackageJson)
1214
);
1315
const myPackageJson: { version: string } = require(myPackageJsonFilename);
1416

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,22 @@ export class ChangeFile {
6262
}
6363

6464
/**
65-
* Writes the change file to disk in sync mode
65+
* Writes the change file to disk in sync mode.
66+
* Returns the filepath.
67+
* @returns the path to the file that was written (based on generatePath())
6668
*/
67-
public writeSync(): void {
69+
public writeSync(): string {
6870
const filePath: string = this.generatePath();
6971
JsonFile.save(this._changeFileData, filePath, {
7072
ensureFolderExists: true
7173
});
74+
return filePath;
7275
}
7376

7477
/**
75-
* Generates a file path for storing the change file to disk
78+
* Generates a file path for storing the change file to disk.
79+
* Note that this value may change if called twice in a row,
80+
* as it is partially based on the current date/time.
7681
*/
7782
public generatePath(): string {
7883
let branch: string | undefined = undefined;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { RushConfiguration } from './RushConfiguration';
2+
import { RushConfigurationProject } from './RushConfigurationProject';
3+
import { ChangeFile } from './ChangeFile';
4+
import { IChangeFile } from './ChangeManagement';
5+
6+
/**
7+
* A class that helps with programatically interacting with Rush's change files.
8+
* @public
9+
*/
10+
export class ChangeManager {
11+
/**
12+
* Creates a change file that has a 'none' type.
13+
* @param rushConfiguration - The rush configuration we are working with
14+
* @param projectName - The name of the project for which to create a change file
15+
* @param emailAddress - The email address which should be associated with this change
16+
* @returns the path to the file that was created, or undefined if no file was written
17+
*/
18+
public static createEmptyChangeFiles(
19+
rushConfiguration: RushConfiguration,
20+
projectName: string,
21+
emailAddress: string): string | undefined {
22+
const projectInfo: RushConfigurationProject | undefined = rushConfiguration.getProjectByName(projectName);
23+
if (projectInfo && projectInfo.shouldPublish) {
24+
25+
const changefile: IChangeFile = { // tslint:disable-line:no-any
26+
'changes': [{
27+
comment: '',
28+
packageName: projectName,
29+
type: 'none'
30+
}],
31+
'packageName': projectName,
32+
'email': emailAddress
33+
};
34+
35+
return new ChangeFile(changefile, rushConfiguration).writeSync();
36+
}
37+
return undefined;
38+
}
39+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import { EOL } from 'os';
55
import * as path from 'path';
66
import * as colors from 'colors';
7-
import { IPackageJson } from '@microsoft/node-core-library';
7+
import {
8+
IPackageJson,
9+
FileConstants
10+
} from '@microsoft/node-core-library';
811

912
import { RushCommandLineParser } from '../cli/RushCommandLineParser';
1013
import { RushConstants } from '../logic/RushConstants';
@@ -65,7 +68,7 @@ export class Rush {
6568
*/
6669
public static get version(): string {
6770
if (!Rush._version) {
68-
const myPackageJsonFilename: string = path.resolve(path.join(__dirname, '..', '..', 'package.json'));
71+
const myPackageJsonFilename: string = path.resolve(path.join(__dirname, '..', '..', FileConstants.PackageJson));
6972
const myPackageJson: IPackageJson = require(myPackageJsonFilename);
7073
Rush._version = myPackageJson.version;
7174
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
JsonFile,
77
IPackageJson,
88
PackageName,
9-
FileSystem
9+
FileSystem,
10+
FileConstants
1011
} from '@microsoft/node-core-library';
1112

1213
import { RushConfiguration } from '../api/RushConfiguration';
@@ -88,7 +89,7 @@ export class RushConfigurationProject {
8889
this._reviewCategory = projectJson.reviewCategory;
8990
}
9091

91-
const packageJsonFilename: string = path.join(this._projectFolder, 'package.json');
92+
const packageJsonFilename: string = path.join(this._projectFolder, FileConstants.PackageJson);
9293
this._packageJson = JsonFile.load(packageJsonFilename);
9394

9495
if (this._packageJson.name !== this._packageName) {

apps/rush-lib/src/cli/actions/VersionAction.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// See LICENSE in the project root for license information.
33

44
import * as semver from 'semver';
5-
import { IPackageJson } from '@microsoft/node-core-library';
5+
import {
6+
IPackageJson,
7+
FileConstants
8+
} from '@microsoft/node-core-library';
69
import {
710
CommandLineFlagParameter,
811
CommandLineStringParameter
@@ -220,7 +223,7 @@ export class VersionAction extends BaseRushAction {
220223

221224
// Commit the package.json and change files updates.
222225
const packageJsonUpdated: boolean = uncommittedChanges.some((changePath) => {
223-
return changePath.indexOf('package.json') > 0;
226+
return changePath.indexOf(FileConstants.PackageJson) > 0;
224227
});
225228

226229
if (packageJsonUpdated) {

apps/rush-lib/src/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@ export {
3838
} from './api/EventHooks';
3939

4040
export {
41-
ChangeFile
42-
} from './api/ChangeFile';
43-
44-
export {
45-
ChangeType,
46-
IChangeInfo
47-
} from './api/ChangeManagement';
41+
ChangeManager
42+
} from './api/ChangeManager';
4843

4944
export {
5045
LastInstallFlag as _LastInstallFlag

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
Text,
1818
IPackageJson,
1919
MapExtensions,
20-
FileSystem
20+
FileSystem,
21+
FileConstants
2122
} from '@microsoft/node-core-library';
2223

2324
import { ApprovedPackagesChecker } from '../logic/ApprovedPackagesChecker';
@@ -534,7 +535,7 @@ export class InstallManager {
534535
unscopedTempProjectName);
535536

536537
// Example: "C:\MyRepo\common\temp\projects\my-project-2\package.json"
537-
const tempPackageJsonFilename: string = path.join(tempProjectFolder, RushConstants.packageJsonFilename);
538+
const tempPackageJsonFilename: string = path.join(tempProjectFolder, FileConstants.PackageJson);
538539

539540
// we only want to overwrite the package if the existing tarball's package.json is different from tempPackageJson
540541
let shouldOverwrite: boolean = true;
@@ -578,7 +579,7 @@ export class InstallManager {
578579
noPax: true,
579580
sync: true,
580581
prefix: npmPackageFolder
581-
} as CreateOptions, ['package.json']);
582+
} as CreateOptions, [FileConstants.PackageJson]);
582583

583584
console.log(`Updating ${tarballFile}`);
584585
} catch (error) {
@@ -591,7 +592,7 @@ export class InstallManager {
591592

592593
// Example: "C:\MyRepo\common\temp\package.json"
593594
const commonPackageJsonFilename: string = path.join(this._rushConfiguration.commonTempFolder,
594-
RushConstants.packageJsonFilename);
595+
FileConstants.PackageJson);
595596

596597
if (shrinkwrapFile) {
597598
// If we have a (possibly incomplete) shrinkwrap file, save it as the temporary file.

0 commit comments

Comments
 (0)