Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdn): add config for gem, npm and pip #2767

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cli/install-tool/install-legacy-tool.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';

Check warning on line 1 in src/cli/install-tool/install-legacy-tool.service.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/install-tool/install-legacy-tool.service.ts#L1

Added line #L1 was not covered by tests
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { EnvService } from '../services';
Expand All @@ -13,7 +14,10 @@
logger.debug(`Installing legacy tool ${tool} v${version} ...`);
const env: NodeJS.ProcessEnv = {};

const pipIndex = this.envSvc.replaceUrl(defaultPipRegistry);
const pipIndex = this.envSvc.replaceUrl(
defaultPipRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_PIP),
);

Check warning on line 20 in src/cli/install-tool/install-legacy-tool.service.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/install-tool/install-legacy-tool.service.ts#L17-L20

Added lines #L17 - L20 were not covered by tests
if (pipIndex !== defaultPipRegistry) {
env.PIP_INDEX_URL = pipIndex;
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/services/env.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@ describe('env.service', () => {
expect(e.replaceUrl('https://registry.npmjs.org')).toBe(
'https://npm.example.test',
);
expect(e.replaceUrl('https://registry.npmjs.org', false)).toBe(
'https://registry.npmjs.org',
);
});
});
10 changes: 8 additions & 2 deletions src/cli/services/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@ export class EnvService {
return this.ignoredTools.has(tool.toUpperCase());
}

public replaceUrl(src: string): string {
/**
* Replace the source url with the optional cdn and replacement urls
* @param src the source url
* @param cdn should the cdn url be used
* @returns the replaced url
*/
public replaceUrl(src: string, cdn = true): string {
let tgt = src;

if (env.CONTAINERBASE_CDN) {
if (env.CONTAINERBASE_CDN && cdn) {
tgt = src.replace(/^https:\//, env.CONTAINERBASE_CDN.replace(/\/$/, ''));
}

Expand Down
9 changes: 6 additions & 3 deletions src/cli/tools/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs, { appendFile, chmod, mkdir, readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { env as penv } from 'node:process';
import is from '@sindresorhus/is';
import { isNonEmptyStringAndNotWhitespace, isString } from '@sindresorhus/is';

Check warning on line 4 in src/cli/tools/node/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/utils.ts#L4

Added line #L4 was not covered by tests
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import type { PackageJson } from 'type-fest';
Expand Down Expand Up @@ -32,7 +32,10 @@
env.npm_config_cache = tmp;
}

const registry = this.envSvc.replaceUrl(defaultRegistry);
const registry = this.envSvc.replaceUrl(
defaultRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_NPM),
);

Check warning on line 38 in src/cli/tools/node/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/utils.ts#L35-L38

Added lines #L35 - L38 were not covered by tests
if (registry !== defaultRegistry) {
env.npm_config_registry = registry;
}
Expand Down Expand Up @@ -171,7 +174,7 @@
return;
}

if (is.string(pkg.bin)) {
if (isString(pkg.bin)) {

Check warning on line 177 in src/cli/tools/node/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/node/utils.ts#L177

Added line #L177 was not covered by tests
await this.shellwrapper({
srcDir: src,
name: pkg.name ?? this.tool(version),
Expand Down
6 changes: 5 additions & 1 deletion src/cli/tools/ruby/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { chmod, mkdir, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';

Check warning on line 3 in src/cli/tools/ruby/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/ruby/utils.ts#L3

Added line #L3 was not covered by tests
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
Expand All @@ -22,7 +23,10 @@
const env: NodeJS.ProcessEnv = {};
const args: string[] = [];

const registry = this.envSvc.replaceUrl(defaultRegistry);
const registry = this.envSvc.replaceUrl(
defaultRegistry,
isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_CDN_GEM),
);

Check warning on line 29 in src/cli/tools/ruby/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/cli/tools/ruby/utils.ts#L26-L29

Added lines #L26 - L29 were not covered by tests
if (registry !== defaultRegistry) {
args.push('--clear-sources', '--source', registry);
}
Expand Down