Skip to content
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
1 change: 1 addition & 0 deletions lib/renovate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as renovateWorker from './workers/global/index.ts';
vi.mock('./instrumentation/index.ts');
vi.mock('./proxy.ts');
vi.mock('./workers/global/index.ts');
vi.mock('./workers/global/config/parse/cli.ts');

describe('renovate', () => {
it('starts', async () => {
Expand Down
5 changes: 5 additions & 0 deletions lib/renovate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import 'source-map-support/register.js';
import './punycode.cjs';

void (async (): Promise<void> => {
// prints and exits the process if --version or --help is passed
const { parseEarlyFlags } =
await import('./workers/global/config/parse/cli.ts');
parseEarlyFlags();

// has to be imported before logger and other libraries which are instrumentalised
const otel = await import('./instrumentation/index.ts');
otel.init();
Expand Down
20 changes: 20 additions & 0 deletions lib/workers/global/config/parse/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { pkg } from '../../../../expose.ts';
import { DockerDatasource } from '../../../../modules/datasource/docker/index.ts';
import getArgv from './__fixtures__/argv.ts';
import * as cli from './cli.ts';
Expand Down Expand Up @@ -206,4 +207,23 @@ describe('workers/global/config/parse/cli', () => {
expect(cli.getConfig(argv)).toEqual({ requireConfig: 'optional' });
});
});

describe('.parseEarlyFlags(argv)', () => {
it('prints version and exits when --version is passed', () => {
const exitSpy = vi
.spyOn(process, 'exit')
.mockImplementation((() => undefined) as never);
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockReturnValue(true);

cli.parseEarlyFlags([...argv, '--version']);

expect(stdoutSpy).toHaveBeenCalledExactlyOnceWith(
expect.stringContaining(pkg.version),
);
expect(exitSpy).toHaveBeenCalledExactlyOnceWith(0);

stdoutSpy.mockRestore();
exitSpy.mockRestore();
});
});
});
76 changes: 43 additions & 33 deletions lib/workers/global/config/parse/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,9 @@ export function getCliName(option: ParseConfigOptions): string {
return `--${nameWithHyphens.toLowerCase()}`;
}

export function getConfig(input: string[]): AllConfig {
// massage migrated configuration keys
const argv = input
.map((a) =>
a
.replace(
'--allow-post-upgrade-command-templating',
'--allow-command-templating',
)
.replace('--allowed-post-upgrade-commands', '--allowed-commands')
.replace('--endpoints=', '--host-rules=')
.replace('--expose-env=true', '--trust-level=high')
.replace('--expose-env', '--trust-level=high')
.replace('--renovate-fork', '--include-forks')
.replace('"platform":"', '"hostType":"')
.replace('"endpoint":"', '"matchHost":"')
.replace('"host":"', '"matchHost":"')
.replace('--azure-auto-complete', '--platform-automerge') // migrate: azureAutoComplete
.replace('--git-lab-automerge', '--platform-automerge') // migrate: gitLabAutomerge
.replace(/^--dry-run$/, '--dry-run=true')
.replace(/^--require-config$/, '--require-config=true')
.replace('--aliases', '--registry-aliases')
.replace('--include-forks=true', '--fork-processing=enabled')
.replace('--include-forks', '--fork-processing=enabled')
.replace('--recreate-closed=false', '--recreate-when=auto')
.replace('--recreate-closed=true', '--recreate-when=always')
.replace('--recreate-closed', '--recreate-when=always'),
)
.filter((a) => !a.startsWith('--git-fs'));
function createProgram(): Command<[string[]]> {
const options = getOptions();

const config: Record<string, any> = {};

let program = new Command().arguments('[repositories...]');

options.forEach((option) => {
Expand Down Expand Up @@ -78,9 +48,49 @@ export function getConfig(input: string[]): AllConfig {
/* oxlint-enable no-console */
}

program = program
return program
.version(pkg.version, '-v, --version')
.on('--help', helpConsole)
.on('--help', helpConsole);
}

export function parseEarlyFlags(input: string[] = process.argv): void {
createProgram().allowUnknownOption().allowExcessArguments().parse(input);
}

export function getConfig(input: string[]): AllConfig {
// massage migrated configuration keys
const argv = input
.map((a) =>
a
.replace(
'--allow-post-upgrade-command-templating',
'--allow-command-templating',
)
.replace('--allowed-post-upgrade-commands', '--allowed-commands')
.replace('--endpoints=', '--host-rules=')
.replace('--expose-env=true', '--trust-level=high')
.replace('--expose-env', '--trust-level=high')
.replace('--renovate-fork', '--include-forks')
.replace('"platform":"', '"hostType":"')
.replace('"endpoint":"', '"matchHost":"')
.replace('"host":"', '"matchHost":"')
.replace('--azure-auto-complete', '--platform-automerge') // migrate: azureAutoComplete
.replace('--git-lab-automerge', '--platform-automerge') // migrate: gitLabAutomerge
.replace(/^--dry-run$/, '--dry-run=true')
.replace(/^--require-config$/, '--require-config=true')
.replace('--aliases', '--registry-aliases')
.replace('--include-forks=true', '--fork-processing=enabled')
.replace('--include-forks', '--fork-processing=enabled')
.replace('--recreate-closed=false', '--recreate-when=auto')
.replace('--recreate-closed=true', '--recreate-when=always')
.replace('--recreate-closed', '--recreate-when=always'),
)
.filter((a) => !a.startsWith('--git-fs'));
const options = getOptions();

const config: Record<string, any> = {};

createProgram()
.action((repositories: string[], opts: Record<string, unknown>) => {
if (repositories?.length) {
config.repositories = repositories;
Expand Down
Loading