Skip to content

Commit

Permalink
Add optional "npmrc" flag to leverage alternative NPM repositories (#258
Browse files Browse the repository at this point in the history
)

fixes #252

Allow the CLI to accept an optional --npmrc flag to include additional rules when resolving NPM dependencies for comparing alternate versions of a package. This is required when working with private repositories/packages.
  • Loading branch information
Westbrook authored Jan 9, 2024
1 parent bfea185 commit fc8fda9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ tach http://example.com
| `--package-version` / `-p` | _(none)_ | Specify an NPM package version to swap in ([details](#swap-npm-dependencies)) |
| `--browser` / `-b` | `chrome` | Which browsers to launch in automatic mode, comma-delimited (chrome, firefox, safari, edge, ie) ([details](#browsers)) |
| `--window-size` | `1024,768` | "width,height" in pixels of the browser windows that will be created |
| `--sample-size` / `-n` | `50` | Minimum number of times to run each benchmark ([details](#minimum-sample-size)) |
| `--sample-size` / `-n` | `50` | Minimum number of times to run each benchmark ([details](#minimum-sample-size)) |
| `--auto-sample-conditions` | `0%` | The degrees of difference to try and resolve when auto-sampling ("N%" or "Nms", comma-delimited) ([details](#auto-sample-conditions)) |
| `--timeout` | `3` | The maximum number of minutes to spend auto-sampling ([details](#auto-sample)) |
| `--measure` | `callback` | Which time interval to measure (`callback`, `global`, `fcp`) ([details](#measurement-modes)) |
Expand Down
6 changes: 5 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ $ tach http://example.com
for (const {npmInstalls, mountPoints, specs} of plans) {
promises.push(
...npmInstalls.map((install) =>
prepareVersionDirectory(install, config.forceCleanNpmInstall)
prepareVersionDirectory(
install,
config.forceCleanNpmInstall,
config.npmrc
)
)
);
promises.push(
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Config {
resolveBareModules: boolean;
remoteAccessibleHost: string;
forceCleanNpmInstall: boolean;
npmrc?: string;
csvFileStats: string;
csvFileRaw: string;
}
Expand All @@ -49,6 +50,7 @@ export async function makeConfig(opts: Opts): Promise<Config> {
csvFileStats: opts['csv-file'],
csvFileRaw: opts['csv-file-raw'],
forceCleanNpmInstall: opts['force-clean-npm-install'],
npmrc: opts['npmrc'],
githubCheck: opts['github-check']
? parseGithubCheckFlag(opts['github-check'])
: undefined,
Expand Down Expand Up @@ -149,6 +151,7 @@ export function applyDefaults(partial: Partial<Config>): Config {
partial.forceCleanNpmInstall !== undefined
? partial.forceCleanNpmInstall
: defaults.forceCleanNpmInstall,
npmrc: partial.npmrc !== undefined ? partial.npmrc : '',
githubCheck: partial.githubCheck,
autoSampleConditions:
partial.autoSampleConditions !== undefined
Expand Down
7 changes: 7 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export const optDefs: commandLineUsage.OptionDefinition[] = [
type: Boolean,
defaultValue: false,
},
{
name: 'npmrc',
description: `.npmrc file to copy into the test install directory.`,
type: String,
defaultValue: '',
},
{
name: 'browser',
description:
Expand Down Expand Up @@ -253,6 +259,7 @@ export interface Opts {
'remote-accessible-host': string;
'window-size': string;
'force-clean-npm-install': boolean;
npmrc?: string;
'csv-file': string;
'csv-file-raw': string;
'json-file': string;
Expand Down
6 changes: 5 additions & 1 deletion src/test/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite('makeConfig', function () {
const argv = ['random-global.html'];
const expected: Config = {
mode: 'automatic',
npmrc: '',
sampleSize: 50,
timeout: 3,
root: '.',
Expand Down Expand Up @@ -77,6 +78,7 @@ suite('makeConfig', function () {
const argv = ['--config=random-global.json'];
const expected: Config = {
mode: 'automatic',
npmrc: '',
sampleSize: 50,
timeout: 3,
root: testData,
Expand Down Expand Up @@ -122,7 +124,7 @@ suite('makeConfig', function () {
const argv = ['--config=random-global.json', '--manual'];
const expected: Config = {
mode: 'manual',

npmrc: '',
sampleSize: 50,
timeout: 3,
root: testData,
Expand Down Expand Up @@ -173,6 +175,7 @@ suite('makeConfig', function () {
];
const expected: Config = {
mode: 'automatic',
npmrc: '',
csvFileStats: 'stats.csv',
csvFileRaw: 'raw.csv',
jsonFile: 'out.json',
Expand Down Expand Up @@ -219,6 +222,7 @@ suite('makeConfig', function () {
const argv = ['--config=deprecated-horizons.json'];
const expected: Config = {
mode: 'automatic',
npmrc: '',
csvFileStats: '',
csvFileRaw: '',
jsonFile: '',
Expand Down
10 changes: 9 additions & 1 deletion src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ const installSuccessFile = '__TACHOMETER_INSTALL_SUCCESS__';
*/
export async function prepareVersionDirectory(
{installDir, packageJson}: NpmInstall,
forceCleanInstall: boolean
forceCleanInstall: boolean,
npmrc?: string
): Promise<void> {
if (forceCleanInstall) {
await fsExtra.remove(installDir);
Expand All @@ -324,6 +325,13 @@ export async function prepareVersionDirectory(
path.join(installDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
if (npmrc) {
await fsExtra.copy(
path.resolve(npmrc),
path.join(installDir, '.npmrc'),
{}
);
}
await runNpm(['install'], {cwd: installDir});
await fsExtra.writeFile(path.join(installDir, installSuccessFile), '');
}
Expand Down

0 comments on commit fc8fda9

Please sign in to comment.