Skip to content

Commit

Permalink
[patch] allow virmator flags to be passed after the command name
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Aug 13, 2024
1 parent 36dbdc6 commit fbff3e7
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 93 deletions.
88 changes: 44 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@virmator/mono-repo-root",
"version": "13.2.6",
"version": "13.2.7",
"private": true,
"type": "module",
"workspaces": [
Expand Down
6 changes: 3 additions & 3 deletions packages/compile/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@virmator/compile",
"version": "13.2.6",
"version": "13.2.7",
"description": "Default TS compile plugin for virmator.",
"keywords": [
"virmator",
Expand Down Expand Up @@ -33,12 +33,12 @@
"dependencies": {
"@augment-vir/common": "^29.3.0",
"@augment-vir/node-js": "^29.3.0",
"@virmator/core": "^13.2.6",
"@virmator/core": "^13.2.7",
"run-time-assertions": "^1.5.2"
},
"devDependencies": {
"@types/node": "^22.2.0",
"@virmator/plugin-testing": "^13.2.6",
"@virmator/plugin-testing": "^13.2.7",
"c8": "^10.1.2",
"chalk": "^5.3.0",
"concurrently": "^8.2.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@virmator/core",
"version": "13.2.6",
"version": "13.2.7",
"description": "Shared core functionality for all virmator plugins and the virmator CLI.",
"keywords": [
"automation",
Expand Down
53 changes: 51 additions & 2 deletions packages/core/src/api/parse-args.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {NpmDepType, PackageType, VirmatorEnv} from '@virmator/core';
import {emptyLog, NpmDepType, PackageType, VirmatorEnv} from '@virmator/core';
import assert from 'node:assert/strict';
import {join} from 'node:path';
import {describe, it} from 'node:test';
import {calculateUsedCommands} from './parse-args.js';
import {VirmatorPlugin} from '../plugin/plugin.js';
import {calculateUsedCommands, parseCliArgs} from './parse-args.js';

describe(calculateUsedCommands.name, () => {
it('calculates correctly', () => {
Expand Down Expand Up @@ -186,3 +187,51 @@ describe(calculateUsedCommands.name, () => {
);
});
});

describe(parseCliArgs.name, () => {
const examplePlugins = [
{
cliCommands: {
fake: {},
},
name: 'fake plugin',
} satisfies Pick<
VirmatorPlugin,
| 'name'
| 'cliCommands'
> as VirmatorPlugin,
] as const satisfies ReadonlyArray<Readonly<VirmatorPlugin>>;

function testParseCliArgs(cliCommand: string) {
return parseCliArgs({
cliCommand,
entryPointFilePath: '',
plugins: examplePlugins,
log: emptyLog,
});
}

it('parses command name', () => {
assert.deepStrictEqual(testParseCliArgs('fake'), {
commands: ['fake'],
filteredCommandArgs: [],
plugin: examplePlugins[0],
usedCommands: {fake: {subCommands: {}}},
virmatorFlags: {},
});
});
it('parses multiple args', () => {
assert.deepStrictEqual(testParseCliArgs('fake --no-deps some-arg --more-arg'), {
commands: ['fake'],
filteredCommandArgs: [
'some-arg',
'--more-arg',
],
plugin: examplePlugins[0],
usedCommands: {fake: {subCommands: {}}},
virmatorFlags: {
'--no-deps': true,
},
});
});
});
6 changes: 3 additions & 3 deletions packages/core/src/api/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ export function parseCliArgs({

const parsedArgs = relevantArgs.reduce(
(parsedArgs: ParsedArgs, arg) => {
if (parsedArgs.filteredCommandArgs.length) {
if (hasKey(virmatorFlags, arg)) {
parsedArgs.virmatorFlags[arg] = true;
} else if (parsedArgs.filteredCommandArgs.length) {
parsedArgs.filteredCommandArgs.push(arg);
} else if (isLengthAtLeast(parsedArgs.commands, 1)) {
const mainCommand = parsedArgs.commands[0];
Expand All @@ -164,8 +166,6 @@ export function parseCliArgs({
if (commandPlugin) {
(parsedArgs.commands as string[]).push(arg);
parsedArgs.plugin = commandPlugin.plugin;
} else if (hasKey(virmatorFlags, arg)) {
parsedArgs.virmatorFlags[arg] = true;
} else {
log.warning(`Ignored unknown flag: '${arg}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugin/plugin-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function createPluginLogger(
}

/** A {@link PluginLogger} implementation that does nothing. */
export const emptyLogger: PluginLogger = createPluginLogger({
export const emptyLog: PluginLogger = createPluginLogger({
stderr: {
write() {
return true;
Expand Down
Loading

0 comments on commit fbff3e7

Please sign in to comment.