Skip to content

Commit 6fb4520

Browse files
committed
feat: add ScriptManager system and metadata + custom script example
1 parent 1138e52 commit 6fb4520

8 files changed

Lines changed: 170 additions & 0 deletions

File tree

src/const.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { fileURLToPath } from 'node:url';
2+
import path from 'node:path';
3+
4+
/////////////////////
5+
// Paths //
6+
/////////////////////
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = path.dirname(__filename);
10+
const __rootpath = path.resolve(__dirname, '..');
11+
12+
export const defaultScriptPath = path.join(__rootpath, 'src', 'scripts/');
13+
14+
/////////////////////
15+
// Metas //
16+
/////////////////////
17+
18+
export const metaStartTerm = '/**';
19+
export const metaEndTerm = '**/';

src/main.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import process from 'node:process';
2+
import type { Arguments } from './types.js';
3+
import { ScriptManager } from './scriptManager.js';
4+
5+
const args: Arguments = JSON.parse(process.argv[2] ?? '{}');
6+
const { method, parameters } = args;
7+
8+
if (method === 'query') {
9+
}
10+
11+
if (method === 'do_something_for_query') {
12+
}
13+
14+
let scriptManager = new ScriptManager();
15+
scriptManager.init();
16+
17+
console.log(scriptManager.scripts);

src/models/Script.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Metas } from '../types.js';
2+
3+
type ScriptArguments = {
4+
script: string;
5+
parameters: Metas;
6+
builtIn: boolean;
7+
};
8+
9+
export class Script {
10+
isBuiltInt: boolean;
11+
scriptCode: string;
12+
13+
name: string | undefined;
14+
desc: string | undefined;
15+
tags: string[] | undefined;
16+
bias: number | undefined;
17+
18+
constructor({ script, parameters, builtIn }: ScriptArguments) {
19+
this.scriptCode = script;
20+
this.isBuiltInt = builtIn;
21+
22+
this.name = parameters['name'] as string;
23+
this.desc = parameters['description'] as string;
24+
this.tags = (parameters['tags'] as string).split(',');
25+
this.bias = parameters['bias'] as number;
26+
}
27+
}

src/scriptManager.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { Metas } from './types.js';
4+
import { defaultScriptPath, metaEndTerm, metaStartTerm } from './const.js';
5+
import { stringRange } from './utils/utils.js';
6+
import { Script } from './models/Script.js';
7+
8+
export class ScriptManager {
9+
scripts: Script[];
10+
11+
constructor() {
12+
this.scripts = [];
13+
}
14+
15+
init() {
16+
this.loadDefaultScripts();
17+
}
18+
19+
loadDefaultScripts() {
20+
let scriptsFilenames = fs
21+
.readdirSync(defaultScriptPath)
22+
.filter(file => file.includes('.js'));
23+
24+
scriptsFilenames.forEach(filename => {
25+
this.loadScript(path.join(defaultScriptPath, filename));
26+
});
27+
}
28+
29+
loadScript(path: string) {
30+
try {
31+
let script: string = fs.readFileSync(path).toString();
32+
33+
let meta = stringRange(script, metaStartTerm, metaEndTerm);
34+
let jsonMeta = JSON.parse(meta) as Metas;
35+
36+
let scriptObject = new Script({
37+
script: script,
38+
parameters: jsonMeta,
39+
builtIn: true
40+
});
41+
42+
this.scripts.push(scriptObject);
43+
} catch (e) {
44+
console.error(`Unable to load ${path}, Error: ${e}`);
45+
}
46+
}
47+
}

src/scripts/JoinLines.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
{
3+
"api":1,
4+
"name":"Join Lines",
5+
"description":"Joins all lines without any delimiter.",
6+
"author":"riesentoaster",
7+
"icon":"collapse",
8+
"tags":"join"
9+
}
10+
**/
11+
12+
function main(input) {
13+
input.text = input.text.replace(/\n/g, '');
14+
}

src/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/////////////////////////////
2+
// Flow Launcher //
3+
/////////////////////////////
4+
5+
export type Arguments = {
6+
method: string;
7+
parameters: string[];
8+
};
9+
10+
export type Result = {
11+
Title: string;
12+
Subtitle: string;
13+
JsonRPCAction?: {
14+
method: string;
15+
parameters: string[];
16+
};
17+
IcoPath?: string;
18+
score?: number;
19+
};
20+
21+
/////////////////////
22+
// Metas //
23+
/////////////////////
24+
25+
export type Metas = {
26+
[key: string]: any;
27+
};

src/utils/metas.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function stringRange(str: string, start: string, end: string): string {
2+
return str.substring(str.indexOf(start) + start.length, str.indexOf(end));
3+
}

src/utils/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Return part of a string based on terms
3+
* @param str The string range.
4+
* @param start The term searched to start range.
5+
* @param end The term searched to end range.
6+
*/
7+
export function stringRange(str: string, start: string, end: string): string {
8+
const NOT_FOUND = -1;
9+
let startPos = str.indexOf(start);
10+
let endPos = str.indexOf(end);
11+
12+
if (startPos === NOT_FOUND || endPos === NOT_FOUND)
13+
throw new Error('Search terms not found');
14+
15+
return str.substring(str.indexOf(start) + start.length, str.indexOf(end));
16+
}

0 commit comments

Comments
 (0)