Skip to content

Commit

Permalink
feat: multiple regexes (#12)
Browse files Browse the repository at this point in the history
* feat: multiple regexes

* fix: improve help output

* chore: bump version

* fix: error wording
  • Loading branch information
tracernz authored Nov 15, 2024
1 parent 1c2cfed commit 61b299a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
4 changes: 2 additions & 2 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": "@flybywiresim/igniter",
"version": "1.2.3",
"version": "1.3.0",
"types": "dist/index.d.ts",
"description": "An intelligent task runner written in Typescript.",
"repository": {
Expand Down
30 changes: 17 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ npx igniter --help
Usage: igniter [options]
Options:
-V, --version output the version number
-c, --config <filename> set the configuration file name (default:
"igniter.config.mjs")
-j, --num-workers <number> set the maximum number of workers to use
(default: Number.MAX_SAFE_INTEGER)
-r, --regex <regex> regular expression used to filter tasks
-i, --invert if true, regex will be used to reject tasks
--no-cache do not skip tasks, even if hash matches cache
--no-tty do not show updating output, just show a single
render
-d, --dry-run skip all tasks to show configuration
--debug stop when an exception is thrown and show trace
-h, --help display help for command
-V, --version output the version number
-c, --config <filename> Set the configuration file name (default:
"igniter.config.mjs")
-j, --num-workers <number> Set the maximum number of workers to use
(default: Number.MAX_SAFE_INTEGER)
-r, --regex <regex> Regular expression used to filter tasks. When
multiple regex arguments are supplied, only
tasks that match all of the arguments will be
run
-i, --invert If true, the preceeding regex argument will be
used to reject tasks instead
--no-cache Do not skip tasks, even if hash matches cache
--no-tty Do not show updating output, just show a single
render
-d, --dry-run Skip all tasks to show configuration
--debug Stop when an exception is thrown and show trace
-h, --help display help for command
```
39 changes: 28 additions & 11 deletions src/Binary.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { Command } from 'commander';
import { Pool } from 'task-pool';
import { findConfigPath, loadConfigTask } from './Helpers';
import { Context } from './Library/Contracts/Context';
import { Context, FilterRegex } from './Library/Contracts/Context';
import { version } from '../package.json';
import Renderer from './Renderer';
import Cache from './Cache';

const allRegex: FilterRegex[] = [];

function addRegex(regex: string): FilterRegex[] {
allRegex.push({
regex: RegExp(regex),
invert: false,
});
return allRegex;
}

function addRegexInvert(): void {
if (allRegex.length === 0) {
throw new Error('Cannot invert the last regex when there are no regex args!');
}
allRegex[allRegex.length - 1].invert = true;
}

const binary = (new Command()).version(version)
.option('-c, --config <filename>', 'set the configuration file name', 'igniter.config.mjs')
.option('-j, --num-workers <number>', 'set the maximum number of workers to use', `${Number.MAX_SAFE_INTEGER}`)
.option('-r, --regex <regex>', 'regular expression used to filter tasks')
.option('-i, --invert', 'if true, regex will be used to reject tasks')
.option('--no-cache', 'do not skip tasks, even if hash matches cache')
.option('--no-tty', 'do not show updating output, just show a single render')
.option('-d, --dry-run', 'skip all tasks to show configuration')
.option('--debug', 'stop when an exception is thrown and show trace')
.option('-c, --config <filename>', 'Set the configuration file name', 'igniter.config.mjs')
.option('-j, --num-workers <number>', 'Set the maximum number of workers to use', `${Number.MAX_SAFE_INTEGER}`)
.option('-r, --regex <regex>', 'Regular expression used to filter tasks. When multiple regex arguments are supplied,'
+ ' only tasks that match all of the arguments will be run', addRegex)
.option('-i, --invert', 'If true, the preceeding regex argument will be used to reject tasks instead', addRegexInvert)
.option('--no-cache', 'Do not skip tasks, even if hash matches cache')
.option('--no-tty', 'Do not show updating output, just show a single render')
.option('-d, --dry-run', 'Skip all tasks to show configuration')
.option('--debug', 'Stop when an exception is thrown and show trace')
.parse(process.argv);

const options = binary.opts();
Expand All @@ -24,8 +42,7 @@ const context: Context = {
debug: options.debug,
configPath: await findConfigPath(options.config),
dryRun: options.dryRun,
filterRegex: options.regex ? RegExp(options.regex) : undefined,
invertRegex: options.invert,
filterRegex: options.regex,
taskPool: new Pool({ limit: options.numWorkers, timeout: 120000 }),
};

Expand Down
8 changes: 6 additions & 2 deletions src/Library/Contracts/Context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Pool } from 'task-pool';
import { Cache } from './Cache';

export interface FilterRegex {
regex: RegExp,
invert: boolean,
}

export interface Context {
debug: boolean,
configPath: string,
dryRun: boolean,
filterRegex: RegExp|undefined,
invertRegex: boolean,
filterRegex: FilterRegex[],
cache?: Cache,
taskPool: Pool,
}
8 changes: 4 additions & 4 deletions src/Library/Tasks/GenericTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ export default class GenericTask implements Task {
}

protected shouldSkipRegex(taskKey: string) {
if (this.context.dryRun) return true;
if (this.context.filterRegex === undefined) return this.context.invertRegex;
const regexMatches = this.context.filterRegex.test(taskKey);
return this.context.invertRegex ? regexMatches : !regexMatches;
if (this.context.dryRun) {
return true;
}
return !this.context.filterRegex.every((r) => (r.regex.test(taskKey) ? !r.invert : r.invert));
}

protected shouldSkipCache(taskKey: string) {
Expand Down

0 comments on commit 61b299a

Please sign in to comment.