Skip to content

Commit

Permalink
class router added and used.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 12, 2024
1 parent 2dec4e6 commit 02e559d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
34 changes: 34 additions & 0 deletions src/scripts/lib/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class Router {
private DEFAULT_ACTION = 'default';
private readonly actions: Map<string, (...args: string[]) => void> = new Map();
private beforeRun: (action: string, args: string[]) => void = () => {};

constructor() {
this.add(this.DEFAULT_ACTION, () => {
console.log('Actions available:', [...this.actions.keys()]);
});
}

onBeforeRun(callback: (action: string, args: string[]) => void): this {
this.beforeRun = callback;
return this;
}

add(action: string, callback: (...args: string[]) => void) {
this.actions.set(action, callback);
}

run(args: string[]) {
const action = args[0] ?? this.DEFAULT_ACTION;
const optionalArgs = args.slice(1);

this.beforeRun(action, optionalArgs);

if (this.actions.has(action)) {
this.actions.get(action)!(...optionalArgs);
} else {
console.error(`Unknown action: ${action}`);
this.actions.get(this.DEFAULT_ACTION)!();
}
}
}
41 changes: 16 additions & 25 deletions src/scripts/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,21 @@ import { createBaseWorldConfig } from '@/lib/config/world';
import { createBaseTypesConfig } from '@/lib/config/types';
import { createDummyDrawer } from '@/lib/drawer/dummy';
import { CompoundsAnalyzer } from '@/lib/analysis/compounds';
import { Router } from '@/scripts/lib/router';

function router() {
const action = process.argv[2] ?? 'default';
const args = process.argv.slice(3);

switch (action) {
case 'test':
testAction(...args);
break;
case 'default':
defaultAction();
break;
default:
console.error(`Unknown action: ${action}`);
defaultAction();
break;
}
}

function defaultAction() {
console.log('Actions available:', ['test']);
}

function testAction(...args: string[]) {
const router = new Router();

router.onBeforeRun((action) => {
console.log('');
console.log('***********************');
console.log('** COMMAND LINE TOOL **');
console.log('***********************');
console.log('');
console.log(`Action to run: "${action}"`);
console.log('');
})

router.add('test', (...args: string[]) => {
console.log('[START] test action', args);

const worldConfig = createBaseWorldConfig();
Expand Down Expand Up @@ -58,6 +49,6 @@ function testAction(...args: string[]) {
console.log(compounds.itemLengthByTypesSummary);

console.log('[FINISH]');
}
});

router();
router.run(process.argv.slice(2));

0 comments on commit 02e559d

Please sign in to comment.