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
9 changes: 9 additions & 0 deletions src/core/cliArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** first top-level subcommand, ignoring global flags (e.g. --debug). */
export function getTopLevelCommand(argv: string[]): string | undefined {
const args = argv.slice(2);
return args.find((arg) => !arg.startsWith('-'));
}

export function isUpdateCommand(argv: string[] = process.argv): boolean {
return getTopLevelCommand(argv) === 'update';
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './commands/release.js';
import { updateCommand } from './commands/update.js';
import { enableCommand } from './commands/enable.js';
import { isUpdateCommand } from './core/cliArgs.js';
import { printCliError, resolveDebugFlag } from './core/cliError.js';
import { ui } from './core/ui.js';

Expand Down Expand Up @@ -245,6 +246,11 @@ function checkForUpdates(): void {
return;
}

// user is already updating; don't suggest running `ensemble update` again.
if (isUpdateCommand()) {
return;
}

// IMPORTANT: This command string must remain a static literal and MUST NOT
// interpolate user-controlled input to avoid shell injection risks.
const child = exec(
Expand Down
28 changes: 28 additions & 0 deletions tests/core/cliArgs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from 'vitest';

import { getTopLevelCommand, isUpdateCommand } from '../../src/core/cliArgs.js';

describe('cliArgs', () => {
describe('getTopLevelCommand', () => {
it.each([
[['node', 'ensemble', 'update'], 'update'],
[['node', 'ensemble', '--debug', 'update'], 'update'],
[['node', 'ensemble', 'push'], 'push'],
[['node', 'ensemble', '--debug', 'push', '--app', 'uat'], 'push'],
[['node', 'ensemble'], undefined],
])('parses %j', (argv, expected) => {
expect(getTopLevelCommand(argv)).toBe(expected);
});
});

describe('isUpdateCommand', () => {
it.each([
[['node', 'ensemble', 'update'], true],
[['node', 'ensemble', '--debug', 'update'], true],
[['node', 'ensemble', 'push'], false],
[['node', 'ensemble', 'release', 'create'], false],
])('detects update command in %j', (argv, expected) => {
expect(isUpdateCommand(argv)).toBe(expected);
});
});
});
Loading