mirror of
https://github.com/sharevb/it-tools.git
synced 2026-12-05 10:45:10 +03:00
chore: global code formatting
This commit is contained in:
1 parent
5bf94bc567
commit
bc44870665
830 files changed
+21075
-13592
No files matched your search
@@ -1,68 +1,69 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildEditedCommand, buildOptionsObject, extractOptions, isOption, sanitizeOption } from './cli-command-editor.service';
|
||||
import {
|
||||
buildEditedCommand,
|
||||
buildOptionsObject,
|
||||
extractOptions,
|
||||
isOption,
|
||||
sanitizeOption,
|
||||
} from './cli-command-editor.service';
|
||||
|
||||
describe('cli-command-editor', () => {
|
||||
describe('extractOptions', () => {
|
||||
it ('extracts all the options from a command', () => {
|
||||
it('extracts all the options from a command', () => {
|
||||
expect(extractOptions('aws elb describe-load-balancers --load-balancer-name my-load-balancer')[0]).toContain(
|
||||
'--load-balancer-name',
|
||||
);
|
||||
|
||||
expect(
|
||||
extractOptions('aws elb describe-load-balancers --load-balancer-name my-load-balancer')[0],
|
||||
extractOptions(
|
||||
'aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy',
|
||||
)[0],
|
||||
).toContain('--load-balancer-name');
|
||||
|
||||
expect(
|
||||
extractOptions('aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy')[0],
|
||||
).toContain('--load-balancer-name');
|
||||
|
||||
expect(
|
||||
extractOptions('aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy')[1],
|
||||
extractOptions(
|
||||
'aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy',
|
||||
)[1],
|
||||
).toContain('--debug');
|
||||
|
||||
expect(
|
||||
extractOptions('aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy')[2],
|
||||
extractOptions(
|
||||
'aws elb describe-load-balancers --load-balancer-name my-load-balancer --debug --query my-queryyy',
|
||||
)[2],
|
||||
).toContain('--query');
|
||||
});
|
||||
|
||||
it('extracts all the option from a command with a mix of hyphen and double hyphens', () => {
|
||||
expect(
|
||||
extractOptions('npm i lodash -g --legacy-peer-deps')[0],
|
||||
).toContain('-g');
|
||||
expect(extractOptions('npm i lodash -g --legacy-peer-deps')[0]).toContain('-g');
|
||||
|
||||
expect(
|
||||
extractOptions('npm i lodash -g --legacy-peer-deps')[1],
|
||||
).toContain('--legacy-peer-deps');
|
||||
expect(extractOptions('npm i lodash -g --legacy-peer-deps')[1]).toContain('--legacy-peer-deps');
|
||||
});
|
||||
|
||||
it('shouldn\'t extract any options from a command without options', () => {
|
||||
expect(
|
||||
extractOptions('npm i lodash'),
|
||||
).toEqual([]);
|
||||
it("shouldn't extract any options from a command without options", () => {
|
||||
expect(extractOptions('npm i lodash')).toEqual([]);
|
||||
});
|
||||
|
||||
it('shouldn\'t return any options if command is not passed', () => {
|
||||
it("shouldn't return any options if command is not passed", () => {
|
||||
expect(extractOptions()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildOptionsObject', () => {
|
||||
it('returns a valid options object with the given options', () => {
|
||||
expect(
|
||||
buildOptionsObject(['--debug', '--load-balancer-names']),
|
||||
).toEqual({
|
||||
expect(buildOptionsObject(['--debug', '--load-balancer-names'])).toEqual({
|
||||
'--debug': '',
|
||||
'--load-balancer-names': '',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an empty obnject with blank options array', () => {
|
||||
expect(
|
||||
buildOptionsObject([]),
|
||||
).toEqual({});
|
||||
expect(buildOptionsObject([])).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('sanitizeOption', () => {
|
||||
it('returns the sanitized option without `id` suffix', () => {
|
||||
expect(sanitizeOption('--debug-id-1dfsj'))
|
||||
.toEqual('--debug');
|
||||
expect(sanitizeOption('--debug-id-1dfsj')).toEqual('--debug');
|
||||
});
|
||||
|
||||
it('returns the blank string', () => {
|
||||
@@ -87,54 +88,66 @@ describe('cli-command-editor', () => {
|
||||
describe('buildEditedCommand', () => {
|
||||
it('returns the edited command', () => {
|
||||
expect(
|
||||
buildEditedCommand({
|
||||
'--debug-id-1dfsj': 'stdin',
|
||||
'-p': '',
|
||||
'-m': 'nahhhh',
|
||||
}, {
|
||||
'--debug-id-1dfsj': 'stdin',
|
||||
'-p': '',
|
||||
'-m': 'nahhhh',
|
||||
}, 'aws node --debug stdio -p -m okayyy'),
|
||||
buildEditedCommand(
|
||||
{
|
||||
'--debug-id-1dfsj': 'stdin',
|
||||
'-p': '',
|
||||
'-m': 'nahhhh',
|
||||
},
|
||||
{
|
||||
'--debug-id-1dfsj': 'stdin',
|
||||
'-p': '',
|
||||
'-m': 'nahhhh',
|
||||
},
|
||||
'aws node --debug stdio -p -m okayyy',
|
||||
),
|
||||
).toEqual('aws node --debug stdin -p -m nahhhh');
|
||||
|
||||
expect(
|
||||
buildEditedCommand({
|
||||
'-d-id-1dfsj': '',
|
||||
'-p-id-fdsd': '4444:3333',
|
||||
'-p-id-fddd': '3333:4444',
|
||||
'--name-id-nnnn': 'clickhouse-server',
|
||||
'--ulimit-id-uuuu': 'nofile=3333:4444',
|
||||
}, {
|
||||
'-d-id-1dfsj': '',
|
||||
'-p-id-fdsd': '4444:3333',
|
||||
'-p-id-fddd': '3333:4444',
|
||||
'--name-id-nnnn': 'clickhouse-server',
|
||||
'--ulimit-id-uuuu': 'nofile=3333:4444',
|
||||
}, 'docker run -d -p 18123:8123 -p 19000:9000 --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server'),
|
||||
).toEqual('docker run -d -p 4444:3333 -p 3333:4444 --name clickhouse-server --ulimit nofile=3333:4444 clickhouse/clickhouse-server');
|
||||
buildEditedCommand(
|
||||
{
|
||||
'-d-id-1dfsj': '',
|
||||
'-p-id-fdsd': '4444:3333',
|
||||
'-p-id-fddd': '3333:4444',
|
||||
'--name-id-nnnn': 'clickhouse-server',
|
||||
'--ulimit-id-uuuu': 'nofile=3333:4444',
|
||||
},
|
||||
{
|
||||
'-d-id-1dfsj': '',
|
||||
'-p-id-fdsd': '4444:3333',
|
||||
'-p-id-fddd': '3333:4444',
|
||||
'--name-id-nnnn': 'clickhouse-server',
|
||||
'--ulimit-id-uuuu': 'nofile=3333:4444',
|
||||
},
|
||||
'docker run -d -p 18123:8123 -p 19000:9000 --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server',
|
||||
),
|
||||
).toEqual(
|
||||
'docker run -d -p 4444:3333 -p 3333:4444 --name clickhouse-server --ulimit nofile=3333:4444 clickhouse/clickhouse-server',
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the edited command when options object and CLI options order doesn\'t match', () => {
|
||||
it("returns the edited command when options object and CLI options order doesn't match", () => {
|
||||
expect(
|
||||
buildEditedCommand({
|
||||
'-d-id-t1dd3': 'true',
|
||||
'--install-id-only123': 'nodemon',
|
||||
}, {
|
||||
'--install-id-only123': 'nodem',
|
||||
'-d-id-t1dd3': 'false',
|
||||
}, 'npm --install nodem -d false'),
|
||||
buildEditedCommand(
|
||||
{
|
||||
'-d-id-t1dd3': 'true',
|
||||
'--install-id-only123': 'nodemon',
|
||||
},
|
||||
{
|
||||
'--install-id-only123': 'nodem',
|
||||
'-d-id-t1dd3': 'false',
|
||||
},
|
||||
'npm --install nodem -d false',
|
||||
),
|
||||
).toBe('npm --install nodemon -d true');
|
||||
});
|
||||
|
||||
it('returns the original command', () => {
|
||||
expect(
|
||||
buildEditedCommand({}, {}, 'npm install nodemon'),
|
||||
).toBe('npm install nodemon');
|
||||
expect(buildEditedCommand({}, {}, 'npm install nodemon')).toBe('npm install nodemon');
|
||||
|
||||
expect(
|
||||
buildEditedCommand({}, {}, 'aws load-balancer describe-load-balancers all'),
|
||||
).toBe('aws load-balancer describe-load-balancers all');
|
||||
expect(buildEditedCommand({}, {}, 'aws load-balancer describe-load-balancers all')).toBe(
|
||||
'aws load-balancer describe-load-balancers all',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -14,15 +14,17 @@ export function extractOptions(command: string = ''): string[] {
|
||||
const tokens = command.split(' ');
|
||||
|
||||
// map each token of the command to an option
|
||||
const options = tokens.map((token: string) => {
|
||||
// every option in a starts with either a hyphen or double hyphens
|
||||
if (isOption(token)) {
|
||||
const randomId = generateRandomId();
|
||||
return `${token}-${randomId}`;
|
||||
}
|
||||
const options = tokens
|
||||
.map((token: string) => {
|
||||
// every option in a starts with either a hyphen or double hyphens
|
||||
if (isOption(token)) {
|
||||
const randomId = generateRandomId();
|
||||
return `${token}-${randomId}`;
|
||||
}
|
||||
|
||||
return '';
|
||||
}).filter((option: string): boolean => !!option);
|
||||
return '';
|
||||
})
|
||||
.filter((option: string): boolean => !!option);
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -40,7 +42,11 @@ export function sanitizeOption(option: string): string {
|
||||
return option.split('-id')?.[0];
|
||||
}
|
||||
|
||||
export function buildEditedCommand(options: Record<string, string>, originalOptions: Record<string, string>, command: string): string {
|
||||
export function buildEditedCommand(
|
||||
options: Record<string, string>,
|
||||
originalOptions: Record<string, string>,
|
||||
command: string,
|
||||
): string {
|
||||
if (!Object.keys(options).length) {
|
||||
return command;
|
||||
}
|
||||
@@ -53,12 +59,11 @@ export function buildEditedCommand(options: Record<string, string>, originalOpti
|
||||
// order as they appear in the original command, this is done
|
||||
// to handle the interpolation of edited option values into the
|
||||
// command
|
||||
originalOptions = Object.entries(options)
|
||||
.reduce((previousValue: Record<string, string>, currentValue: string[]) => {
|
||||
previousValue[currentValue[0]] = currentValue[1];
|
||||
originalOptions = Object.entries(options).reduce((previousValue: Record<string, string>, currentValue: string[]) => {
|
||||
previousValue[currentValue[0]] = currentValue[1];
|
||||
|
||||
return previousValue;
|
||||
}, originalOptions);
|
||||
return previousValue;
|
||||
}, originalOptions);
|
||||
|
||||
const defaultValues: Record<string, string> = {};
|
||||
// replacing the options and their values (if any) with formatter ($i) to
|
||||
@@ -92,8 +97,7 @@ export function buildEditedCommand(options: Record<string, string>, originalOpti
|
||||
|
||||
if (originalOptions[key]) {
|
||||
editedCommand = editedCommand.replace(`$${i}`, `${keyWithoutIdSuffix} ${originalOptions[key]}`);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
const value = defaultValues[`$${i}`];
|
||||
const replaceValue = value ? `${keyWithoutIdSuffix} ${value}` : keyWithoutIdSuffix;
|
||||
editedCommand = editedCommand.replace(`$${i}`, replaceValue);
|
||||
|
||||
@@ -25,7 +25,11 @@ const command = ref('');
|
||||
:placeholder="$t('tools.cli-command-editor.placeholder')"
|
||||
:aria-placeholder="t('tools.cli-command-editor.placeholder')"
|
||||
raw-text
|
||||
@update:value="() => { command = inputCommand }"
|
||||
@update:value="
|
||||
() => {
|
||||
command = inputCommand;
|
||||
}
|
||||
"
|
||||
/>
|
||||
<div v-for="option in options" :key="option" flex justify-center>
|
||||
<c-input-text
|
||||
@@ -38,17 +42,15 @@ const command = ref('');
|
||||
:placeholder="service.sanitizeOption(option)"
|
||||
:aria-placeholder="service.sanitizeOption(option)"
|
||||
mt-6
|
||||
@update:value="() => { command = service.buildEditedCommand(optionsInput, optionsObject, inputCommand) }"
|
||||
@update:value="
|
||||
() => {
|
||||
command = service.buildEditedCommand(optionsInput, optionsObject, inputCommand);
|
||||
}
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<c-text-copyable
|
||||
v-if="command"
|
||||
:value="command"
|
||||
|
||||
:show-icon="false"
|
||||
mt-6 font-mono
|
||||
/>
|
||||
<c-text-copyable v-if="command" :value="command" :show-icon="false" mt-6 font-mono />
|
||||
</n-gi>
|
||||
</n-grid>
|
||||
</c-card>
|
||||
|
||||
Reference in new issue
Block a user