Skip to content

Commit 7b30b58

Browse files
authored
Merge pull request microsoft#916 from nchlswhttkr/nchlswhttkr/git-hooks
[rush] Add support for git hooks
2 parents 545dfc1 + 8911a72 commit 7b30b58

3 files changed

Lines changed: 57 additions & 17 deletions

File tree

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

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import {
1919
MapExtensions,
2020
FileSystem,
2121
FileConstants,
22-
Sort
22+
Sort,
23+
FolderConstants,
24+
PosixModeBits
2325
} from '@microsoft/node-core-library';
2426

2527
import { ApprovedPackagesChecker } from '../logic/ApprovedPackagesChecker';
@@ -103,6 +105,7 @@ export interface IInstallManagerOptions {
103105
* If specified when using PNPM, the logs will be in /common/temp/pnpm.log
104106
*/
105107
collectLogFile: boolean;
108+
106109
/**
107110
* The variant to consider when performing installations and validating shrinkwrap updates.
108111
*/
@@ -180,10 +183,10 @@ export class InstallManager {
180183
private static _collectVersionsForDependencies(
181184
rushConfiguration: RushConfiguration,
182185
options: {
183-
versionsForDependencies: Map<string, Set<string>>;
184-
dependencies: ReadonlyArray<PackageJsonDependency>;
185-
cyclicDependencies: Set<string>;
186-
variant: string | undefined;
186+
versionsForDependencies: Map<string, Set<string>>;
187+
dependencies: ReadonlyArray<PackageJsonDependency>;
188+
cyclicDependencies: Set<string>;
189+
variant: string | undefined;
187190
}): void {
188191
const {
189192
variant,
@@ -264,6 +267,32 @@ export class InstallManager {
264267

265268
ApprovedPackagesChecker.rewriteConfigFiles(this._rushConfiguration);
266269

270+
// Git hooks are only installed if the repo opts in by including files in /common/git-hooks
271+
const hookSource: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
272+
const hookDestination: string = path.join(this._rushConfiguration.rushJsonFolder, FolderConstants.Git, 'hooks');
273+
274+
if (FileSystem.exists(hookSource)) {
275+
const hookFilenames: Array<string> = FileSystem.readFolder(hookSource);
276+
if (hookFilenames.length > 0) {
277+
console.log(os.EOL + colors.bold('Found files in the "common/git-hooks" folder.'));
278+
279+
// Clear the currently installed git hooks and install fresh copies
280+
FileSystem.ensureEmptyFolder(hookDestination);
281+
282+
// Only copy files that look like Git hook names
283+
const filteredHookFilenames: string[] = hookFilenames.filter(x => /^[a-z\-]+/.test(x));
284+
for (const filename of filteredHookFilenames) {
285+
FileSystem.copyFile({
286+
sourcePath: path.join(hookSource, filename),
287+
destinationPath: path.join(hookDestination, filename)
288+
});
289+
FileSystem.changePosixModeBits(path.join(hookDestination, filename), PosixModeBits.UserExecute);
290+
}
291+
292+
console.log('Successfully installed these Git hook scripts: ' + filteredHookFilenames.join(', ') + os.EOL);
293+
}
294+
}
295+
267296
// Ensure that the package manager is installed
268297
return this.ensureLocalPackageManager()
269298
.then(() => {
@@ -611,8 +640,8 @@ export class InstallManager {
611640
if (shrinkwrapFile) {
612641
if (!shrinkwrapFile.tryEnsureCompatibleDependency(packageName, packageVersion,
613642
rushProject.tempProjectName)) {
614-
shrinkwrapWarnings.push(`"${packageName}" (${packageVersion}) required by`
615-
+ ` "${rushProject.packageName}"`);
643+
shrinkwrapWarnings.push(`"${packageName}" (${packageVersion}) required by`
644+
+ ` "${rushProject.packageName}"`);
616645
shrinkwrapIsUpToDate = false;
617646
}
618647
}
@@ -872,7 +901,7 @@ export class InstallManager {
872901
}
873902

874903
// Run "npm install" in the common folder
875-
const installArgs: string[] = [ 'install' ];
904+
const installArgs: string[] = ['install'];
876905
this._pushConfigurationArgs(installArgs, options);
877906

878907
console.log(os.EOL + colors.bold(`Running "${this._rushConfiguration.packageManager} install" in`
@@ -981,7 +1010,7 @@ export class InstallManager {
9811010

9821011
private _queryIfReleaseIsPublished(registryUrl: string): Promise<boolean> {
9831012
let queryUrl: string = registryUrl;
984-
if (queryUrl[-1] !== '/') {
1013+
if (queryUrl[- 1] !== '/') {
9851014
queryUrl += '/';
9861015
}
9871016
// Note that the "@" symbol does not normally get URL-encoded
@@ -999,9 +1028,9 @@ export class InstallManager {
9991028
}
10001029

10011030
return fetch.default(queryUrl, {
1002-
headers: headers,
1003-
agent: agent
1004-
})
1031+
headers: headers,
1032+
agent: agent
1033+
})
10051034
.then((response: fetch.Response) => {
10061035
if (!response.ok) {
10071036
return Promise.reject(new Error('Failed to query'));
@@ -1026,9 +1055,9 @@ export class InstallManager {
10261055
// Make sure the tarball wasn't deleted from the CDN
10271056
headers.set('accept', '*/*');
10281057
return fetch.default(url, {
1029-
headers: headers,
1030-
agent: agent
1031-
})
1058+
headers: headers,
1059+
agent: agent
1060+
})
10321061
.then<boolean>((response2: fetch.Response) => {
10331062
if (!response2.ok) {
10341063
if (response2.status === 404) {
@@ -1122,7 +1151,7 @@ export class InstallManager {
11221151
private _syncFile(sourcePath: string, destinationPath: string): void {
11231152
if (FileSystem.exists(sourcePath)) {
11241153
console.log('Updating ' + destinationPath);
1125-
FileSystem.copyFile({sourcePath, destinationPath});
1154+
FileSystem.copyFile({ sourcePath, destinationPath });
11261155
} else {
11271156
if (FileSystem.exists(destinationPath)) {
11281157
console.log('Deleting ' + destinationPath);

‎apps/rush-lib/src/logic/PackageJsonUpdater.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class PackageJsonUpdater {
243243

244244
const oldDependencyType: DependencyType | undefined =
245245
oldDevDependency ? oldDevDependency.dependencyType :
246-
oldDependency ? oldDependency.dependencyType : undefined;
246+
oldDependency ? oldDependency.dependencyType : undefined;
247247

248248
dependencyType = dependencyType || oldDependencyType || DependencyType.Regular;
249249

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Added support for git hooks",
5+
"packageName": "@microsoft/rush",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "26531118+nchlswhttkr@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)