Skip to content

Commit 09f1bb9

Browse files
committed
Added support for git hooks
1 parent c7237d9 commit 09f1bb9

6 files changed

Lines changed: 66 additions & 21 deletions

File tree

‎apps/rush-lib/src/cli/actions/BaseInstallAction.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export abstract class BaseInstallAction extends BaseRushAction {
3030
protected _noLinkParameter: CommandLineFlagParameter;
3131
protected _networkConcurrencyParameter: CommandLineIntegerParameter;
3232
protected _debugPackageManagerParameter: CommandLineFlagParameter;
33+
protected _withHooksParameter: CommandLineFlagParameter;
3334

3435
protected onDefineParameters(): void {
3536
this._purgeParameter = this.defineFlagParameter({
@@ -60,6 +61,11 @@ export abstract class BaseInstallAction extends BaseRushAction {
6061
+ ' the output of Rush to a file when using this command.'
6162
});
6263
this._variant = this.defineStringParameter(Variants.VARIANT_PARAMETER);
64+
this._withHooksParameter = this.defineFlagParameter({
65+
parameterLongName: '--with-hooks',
66+
description: 'If specified, will copy over git hooks from "common/git-hooks" folder into the'
67+
+ ' git config directory. Each file should be an applicably named, such as "pre-commit.sh".'
68+
});
6369
}
6470

6571
protected abstract buildInstallOptions(): IInstallManagerOptions;

‎apps/rush-lib/src/cli/actions/InstallAction.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export class InstallAction extends BaseInstallAction {
3434
recheckShrinkwrap: false,
3535
networkConcurrency: this._networkConcurrencyParameter.value,
3636
collectLogFile: this._debugPackageManagerParameter.value!,
37-
variant: this._variant.value
37+
variant: this._variant.value,
38+
withHooks: this._withHooksParameter.value
3839
};
3940
}
4041
}

‎apps/rush-lib/src/cli/actions/UpdateAction.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export class UpdateAction extends BaseInstallAction {
6262
recheckShrinkwrap: this._recheckParameter.value!,
6363
networkConcurrency: this._networkConcurrencyParameter.value,
6464
collectLogFile: this._debugPackageManagerParameter.value!,
65-
variant: this._variant.value
65+
variant: this._variant.value,
66+
withHooks: this._withHooksParameter.value
6667
};
6768
}
6869
}

‎apps/rush-lib/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Optional arguments:
259259
exports[`CommandLineHelp prints the help for each action: install 1`] = `
260260
"usage: rush install [-h] [-p] [--bypass-policy] [--no-link]
261261
[--network-concurrency COUNT] [--debug-package-manager]
262-
[--variant VARIANT]
262+
[--variant VARIANT] [--with-hooks]
263263
264264
265265
The \\"rush install\\" command installs package dependencies for all your
@@ -296,6 +296,10 @@ Optional arguments:
296296
--variant VARIANT Run command using a variant installation
297297
configuration. This parameter may alternatively
298298
specified via the RUSH_VARIANT environment variable.
299+
--with-hooks If specified, will copy over git hooks from
300+
\\"common/git-hooks\\" folder into the git config
301+
directory. Each file should be an applicably named,
302+
such as \\"pre-commit.sh\\".
299303
"
300304
`;
301305
@@ -477,7 +481,7 @@ Optional arguments:
477481
exports[`CommandLineHelp prints the help for each action: update 1`] = `
478482
"usage: rush update [-h] [-p] [--bypass-policy] [--no-link]
479483
[--network-concurrency COUNT] [--debug-package-manager]
480-
[--variant VARIANT] [--full] [--recheck]
484+
[--variant VARIANT] [--with-hooks] [--full] [--recheck]
481485
482486
483487
The \\"rush update\\" command installs the dependencies described in your package.
@@ -513,6 +517,10 @@ Optional arguments:
513517
--variant VARIANT Run command using a variant installation
514518
configuration. This parameter may alternatively
515519
specified via the RUSH_VARIANT environment variable.
520+
--with-hooks If specified, will copy over git hooks from
521+
\\"common/git-hooks\\" folder into the git config
522+
directory. Each file should be an applicably named,
523+
such as \\"pre-commit.sh\\".
516524
--full Normally \\"rush update\\" tries to preserve your
517525
existing installed versions and only makes the
518526
minimum updates needed to satisfy the package.json

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

Lines changed: 43 additions & 15 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';
@@ -102,10 +104,16 @@ export interface IInstallManagerOptions {
102104
* If specified when using PNPM, the logs will be in /common/temp/pnpm.log
103105
*/
104106
collectLogFile: boolean;
107+
105108
/**
106109
* The variant to consider when performing installations and validating shrinkwrap updates.
107110
*/
108111
variant?: string | undefined;
112+
113+
/**
114+
* Whether to copy over git hooks into the git config when installing.
115+
*/
116+
withHooks: boolean;
109117
}
110118

111119
/**
@@ -178,10 +186,10 @@ export class InstallManager {
178186
private static _collectVersionsForDependencies(
179187
rushConfiguration: RushConfiguration,
180188
options: {
181-
versionsForDependencies: Map<string, Set<string>>;
182-
dependencies: ReadonlyArray<PackageJsonDependency>;
183-
cyclicDependencies: Set<string>;
184-
variant: string | undefined;
189+
versionsForDependencies: Map<string, Set<string>>;
190+
dependencies: ReadonlyArray<PackageJsonDependency>;
191+
cyclicDependencies: Set<string>;
192+
variant: string | undefined;
185193
}): void {
186194
const {
187195
variant,
@@ -256,6 +264,26 @@ export class InstallManager {
256264

257265
ApprovedPackagesChecker.rewriteConfigFiles(this._rushConfiguration);
258266

267+
if (options.withHooks) {
268+
const source: string = path.join(this._rushConfiguration.commonFolder, 'git-hooks');
269+
const destination: string = path.join(this._rushConfiguration.rushJsonFolder, FolderConstants.Git, 'hooks');
270+
271+
if (FileSystem.exists(source) && FileSystem.exists(destination)) {
272+
FileSystem.readFolder(source).forEach(filename => {
273+
274+
const match: RegExpMatchArray | null = filename.match(/[a-z\-]+/);
275+
if (match !== null) {
276+
FileSystem.copyFile({
277+
sourcePath: path.join(source, filename),
278+
destinationPath: path.join(destination, match[0])
279+
});
280+
FileSystem.changePosixModeBits(path.join(destination, match[0]), PosixModeBits.UserExecute);
281+
}
282+
283+
});
284+
}
285+
}
286+
259287
// Ensure that the package manager is installed
260288
return this.ensureLocalPackageManager()
261289
.then(() => {
@@ -603,8 +631,8 @@ export class InstallManager {
603631
if (shrinkwrapFile) {
604632
if (!shrinkwrapFile.tryEnsureCompatibleDependency(packageName, packageVersion,
605633
rushProject.tempProjectName)) {
606-
shrinkwrapWarnings.push(`"${packageName}" (${packageVersion}) required by`
607-
+ ` "${rushProject.packageName}"`);
634+
shrinkwrapWarnings.push(`"${packageName}" (${packageVersion}) required by`
635+
+ ` "${rushProject.packageName}"`);
608636
shrinkwrapIsUpToDate = false;
609637
}
610638
}
@@ -864,7 +892,7 @@ export class InstallManager {
864892
}
865893

866894
// Run "npm install" in the common folder
867-
const installArgs: string[] = [ 'install' ];
895+
const installArgs: string[] = ['install'];
868896
this._pushConfigurationArgs(installArgs, options);
869897

870898
console.log(os.EOL + colors.bold(`Running "${this._rushConfiguration.packageManager} install" in`
@@ -991,9 +1019,9 @@ export class InstallManager {
9911019
}
9921020

9931021
return fetch.default(queryUrl, {
994-
headers: headers,
995-
agent: agent
996-
})
1022+
headers: headers,
1023+
agent: agent
1024+
})
9971025
.then((response: fetch.Response) => {
9981026
if (!response.ok) {
9991027
return Promise.reject(new Error('Failed to query'));
@@ -1018,9 +1046,9 @@ export class InstallManager {
10181046
// Make sure the tarball wasn't deleted from the CDN
10191047
headers.set('accept', '*/*');
10201048
return fetch.default(url, {
1021-
headers: headers,
1022-
agent: agent
1023-
})
1049+
headers: headers,
1050+
agent: agent
1051+
})
10241052
.then<boolean>((response2: fetch.Response) => {
10251053
if (!response2.ok) {
10261054
if (response2.status === 404) {
@@ -1114,7 +1142,7 @@ export class InstallManager {
11141142
private _syncFile(sourcePath: string, destinationPath: string): void {
11151143
if (FileSystem.exists(sourcePath)) {
11161144
console.log('Updating ' + destinationPath);
1117-
FileSystem.copyFile({sourcePath, destinationPath});
1145+
FileSystem.copyFile({ sourcePath, destinationPath });
11181146
} else {
11191147
if (FileSystem.exists(destinationPath)) {
11201148
console.log('Deleting ' + destinationPath);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ export class PackageJsonUpdater {
192192
recheckShrinkwrap: false,
193193
networkConcurrency: undefined,
194194
collectLogFile: false,
195-
variant: variant
195+
variant: variant,
196+
withHooks: false
196197
};
197198
const installManager: InstallManager = new InstallManager(
198199
this._rushConfiguration,
@@ -238,7 +239,7 @@ export class PackageJsonUpdater {
238239

239240
const oldDependencyType: DependencyType | undefined =
240241
oldDevDependency ? oldDevDependency.dependencyType :
241-
oldDependency ? oldDependency.dependencyType : undefined;
242+
oldDependency ? oldDependency.dependencyType : undefined;
242243

243244
dependencyType = dependencyType || oldDependencyType || DependencyType.Regular;
244245

0 commit comments

Comments
 (0)