Skip to content

Commit aac3c35

Browse files
committed
refactor: better code maintenability + optimized script execution
1 parent f79ebfc commit aac3c35

6 files changed

Lines changed: 85 additions & 22 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { ScriptManager } from '../scriptManager.js';
2+
import { FlowLauncher, sendJsonRpcRequest } from '../utils/flowLauncher.js';
3+
4+
type FlowViewControllerArguments = {
5+
manager: ScriptManager;
6+
};
7+
8+
export class FlowViewController {
9+
scriptManager: ScriptManager;
10+
11+
constructor({ manager }: FlowViewControllerArguments) {
12+
this.scriptManager = manager;
13+
}
14+
15+
// ─── METHOD: query ───────────────────────────────────────────────────────────────────
16+
queryScript(parameter: string = '') {
17+
const results = this.scriptManager.search(parameter ?? '');
18+
const resultsFmt = FlowLauncher.searchResultFormat(results);
19+
20+
// Send result to Flow laucher via JsonRPC
21+
sendJsonRpcRequest({ result: resultsFmt });
22+
}
23+
24+
// ─── METHOD: run ─────────────────────────────────────────────────────────────────────
25+
runSelectedScript(parameters: string) {
26+
const scriptArguments = JSON.parse(parameters);
27+
this.scriptManager.runScriptM(scriptArguments);
28+
}
29+
}

src/main.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
import process from 'node:process';
22
import type { Arguments } from './types.js';
33
import { ScriptManager } from './scriptManager.js';
4-
import { FlowLauncher, sendJsonRpcRequest } from './utils/flowLauncher.js';
5-
import { Clipboard } from './utils/clipboard.js';
6-
import type { Script } from './models/Script.js';
4+
import { FlowViewController } from './controllers/FlowViewController.js';
75

86
const args: Arguments = JSON.parse(process.argv[2] ?? '{}');
97
const { method, parameters } = args;
108

11-
let scriptManager = new ScriptManager();
12-
scriptManager.init();
9+
const scriptManager = new ScriptManager();
10+
const flowViewController = new FlowViewController({ manager: scriptManager });
1311

1412
if (method === 'query') {
15-
let results = FlowLauncher.searchResultFormat(
16-
scriptManager.search(parameters[0] ?? '')
17-
);
18-
sendJsonRpcRequest({ result: results });
13+
// Init scriptManager to load all scripts
14+
scriptManager.init();
15+
16+
flowViewController.queryScript(parameters[0] as string);
1917
}
2018

2119
if (method === 'run') {
22-
let clip = Clipboard.get();
23-
let activeScript = scriptManager.scripts.filter(
24-
script => script.name === parameters[0]
25-
)[0];
26-
const result = scriptManager.runScript(activeScript as Script, clip);
27-
Clipboard.copy(result);
28-
FlowLauncher.showMessage('New Result Copied!', 'Yeah !!!');
20+
flowViewController.runSelectedScript(parameters[0] as string);
2921
}

src/models/Script.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Metas } from '../types.js';
33
import { MAIN_FUNCTION_EXEC } from '../const.js';
44
import type { ScriptExecution } from './ScriptExecution.js';
55

6-
type ScriptArguments = {
6+
export type ScriptArguments = {
77
script: string;
88
parameters: Metas;
99
builtIn: boolean;
@@ -13,6 +13,10 @@ export class Script {
1313
isBuiltInt: boolean;
1414
scriptCode: string;
1515

16+
//TODO: Add icon support
17+
18+
metas: Metas;
19+
1620
name: string | undefined;
1721
desc: string | undefined;
1822
tags: string[] | undefined;
@@ -27,6 +31,8 @@ export class Script {
2731
this.isBuiltInt = builtIn;
2832

2933
// Define metas
34+
this.metas = parameters;
35+
3036
this.name = parameters['name'] as string;
3137
this.desc = parameters['description'] as string;
3238
this.tags = (parameters['tags'] as string).split(TAGS_SEPARATOR);

src/models/ScriptExecution.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { FlowLauncher } from '../utils/flowLauncher.js';
2-
31
type ScriptExecutionArguments = {
42
text: string;
53
};
64

5+
// ╭────────────────────────────────────────────────────────────────────────────────╮
6+
// │ TODO: postInfo and postError functions (notify user) │
7+
// │ TODO: insert function (clipboard + new value) │
8+
// ╰────────────────────────────────────────────────────────────────────────────────╯
9+
710
export class ScriptExecution {
811
fullText: string;
912
constructor({ text }: ScriptExecutionArguments) {

src/scriptManager.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import type { Metas } from './types.js';
4-
import { Script } from './models/Script.js';
4+
import { Script, type ScriptArguments } from './models/Script.js';
55
import { defaultScriptPath, metaEndTerm, metaStartTerm } from './const.js';
66
import { matchesMetas, stringRange } from './utils/utils.js';
77
import { ScriptExecution } from './models/ScriptExecution.js';
8+
import { Clipboard } from './utils/clipboard.js';
9+
import { FlowLauncher } from './utils/flowLauncher.js';
810

911
export class ScriptManager {
1012
scripts: Script[];
@@ -15,6 +17,7 @@ export class ScriptManager {
1517

1618
init() {
1719
this.loadDefaultScripts();
20+
//TODO: Manage users scripts
1821
}
1922

2023
loadDefaultScripts() {
@@ -71,9 +74,33 @@ export class ScriptManager {
7174
return results.sort(metaNameSort);
7275
}
7376

77+
runScriptM(scriptArguments: ScriptArguments) {
78+
// Get clipboard content
79+
const clip = Clipboard.get();
80+
81+
const script = new Script(scriptArguments);
82+
83+
const result = this.runScript(script, clip);
84+
85+
this.replaceText(result, clip);
86+
}
87+
7488
runScript(script: Script, text: string) {
7589
let scriptExecution = new ScriptExecution({ text: text });
90+
7691
script.run(scriptExecution);
77-
return scriptExecution.text;
92+
93+
return scriptExecution.text ?? '';
94+
}
95+
96+
replaceText(newText: string, originText: string) {
97+
if (newText !== originText) {
98+
// Update clipboard with new value
99+
Clipboard.copy(newText);
100+
101+
//TODO: Possibly change location in futur
102+
// Notify User from change
103+
FlowLauncher.showMessage('test');
104+
}
78105
}
79106
}

src/utils/flowLauncher.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ export const FlowLauncher = {
1212
Subtitle: script.desc ?? 'No description',
1313
JsonRPCAction: {
1414
method: 'run',
15-
parameters: [script.name ?? '']
15+
parameters: [
16+
JSON.stringify({
17+
script: script.scriptCode,
18+
parameters: script.metas,
19+
builtIn: script.isBuiltInt
20+
})
21+
]
1622
},
1723
IcoPath: 'icon\\app.png',
1824
score: 0

0 commit comments

Comments
 (0)