Skip to content

Commit 8b5034f

Browse files
authored
Merge pull request #8 from appwrite/fix-issues
chore: add more git commands
2 parents 14fc1f8 + b7fe28b commit 8b5034f

File tree

17 files changed

+1176
-908
lines changed

17 files changed

+1176
-908
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Synapse
22

3-
Operating system gateway for remote serverless environments. Synapse provides a WebSocket-based interface to interact with terminal sessions, manage files, and monitor system resources remotely.
3+
Operating system gateway for remote serverless environments. Synapse provides a WebSocket-based interface to interact with terminal sessions, manage files, monitor system resources, perform Git operations, and more.
44

55
## Features
66

@@ -137,17 +137,17 @@ await git.push();
137137

138138
```typescript
139139
// Lint and format code
140-
import { Synapse, CodeStyle } from "@appwrite.io/synapse";
140+
import { Synapse, Code } from "@appwrite.io/synapse";
141141

142142
const synapse = new Synapse();
143-
const codeStyle = new CodeStyle(synapse);
143+
const code = new Code(synapse);
144144

145145
// Format code with specific options
146146
const code = `function hello(name) {
147147
return "Hello, " + name;
148148
}`;
149149

150-
const formatResult = await codeStyle.format(code, {
150+
const formatResult = await code.format(code, {
151151
language: "javascript",
152152
indent: 2,
153153
singleQuote: true,
@@ -157,7 +157,7 @@ const formatResult = await codeStyle.format(code, {
157157
console.log("Formatted code:", formatResult.data);
158158

159159
// Lint code for potential issues
160-
const lintResult = await codeStyle.lint(code, {
160+
const lintResult = await code.lint(code, {
161161
language: "javascript",
162162
rules: {
163163
semi: "error",

package.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@appwrite.io/synapse",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Operating system gateway for remote serverless environments",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -22,23 +22,25 @@
2222
},
2323
"license": "MIT",
2424
"dependencies": {
25+
"eslint": "^9.24.0",
2526
"node-pty": "^1.0.0",
26-
"ws": "^8.18.1",
27-
"eslint": "^9.23.0",
28-
"prettier": "^3.5.3"
27+
"prettier": "^3.5.3",
28+
"ws": "^8.18.1"
2929
},
3030
"devDependencies": {
3131
"@types/jest": "^29.5.14",
32-
"@types/node": "^22.13.13",
33-
"@types/ws": "^8.18.0",
34-
"@typescript-eslint/eslint-plugin": "^8.28.0",
35-
"@typescript-eslint/parser": "^8.28.0",
32+
"@types/node": "^22.14.1",
33+
"@types/node-fetch": "^2.6.12",
34+
"@types/ws": "^8.18.1",
35+
"@typescript-eslint/eslint-plugin": "^8.29.1",
36+
"@typescript-eslint/parser": "^8.29.1",
3637
"esbuild": "^0.25.2",
3738
"esbuild-plugin-file-path-extensions": "^2.1.4",
3839
"jest": "^29.7.0",
39-
"ts-jest": "^29.3.0",
40+
"node-fetch": "^3.3.2",
41+
"ts-jest": "^29.3.2",
4042
"tsup": "^8.4.0",
41-
"typescript": "^5.8.2"
43+
"typescript": "^5.8.3"
4244
},
4345
"bugs": {
4446
"url": "https://github.com/appwrite/synapse/issues"

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { CodeStyle } from "./services/codestyle";
1+
export { Code } from "./services/code";
22
export { Filesystem } from "./services/filesystem";
33
export { Git } from "./services/git";
44
export { System } from "./services/system";

src/services/code.ts

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { ESLint } from "eslint";
2+
import { format, Options } from "prettier";
3+
import { Synapse } from "../synapse";
4+
5+
export interface FormatOptions {
6+
language: string;
7+
indent?: number;
8+
useTabs?: boolean;
9+
semi?: boolean;
10+
singleQuote?: boolean;
11+
printWidth?: number;
12+
}
13+
14+
export interface LintOptions {
15+
language: string;
16+
rules?: Record<string, "error" | "warn" | "off">;
17+
}
18+
19+
export interface FormatResult {
20+
success: boolean;
21+
data?: string;
22+
error?: string;
23+
}
24+
25+
export interface LintResult {
26+
success: boolean;
27+
data?: {
28+
issues: Array<{
29+
line: number;
30+
column: number;
31+
severity: "error" | "warning";
32+
rule: string;
33+
message: string;
34+
}>;
35+
};
36+
error?: string;
37+
}
38+
39+
export class Code {
40+
private synapse: Synapse;
41+
42+
/**
43+
* Creates a new CodeStyle instance
44+
* @param synapse The Synapse instance for WebSocket communication
45+
*/
46+
constructor(synapse: Synapse) {
47+
this.synapse = synapse;
48+
}
49+
50+
private log(message: string): void {
51+
const timestamp = new Date().toISOString();
52+
console.log(`[Code][${timestamp}] ${message}`);
53+
}
54+
55+
private getParserForLanguage(language: string): string {
56+
const languageMap: Record<string, string> = {
57+
javascript: "babel",
58+
typescript: "typescript",
59+
json: "json",
60+
html: "html",
61+
css: "css",
62+
markdown: "markdown",
63+
yaml: "yaml",
64+
};
65+
66+
return languageMap[language.toLowerCase()] || "babel";
67+
}
68+
69+
private toPrettierOptions(
70+
language: string,
71+
options?: FormatOptions,
72+
): Options {
73+
const parser = this.getParserForLanguage(language);
74+
75+
return {
76+
parser,
77+
tabWidth: options?.indent || 2,
78+
useTabs: options?.useTabs || false,
79+
semi: options?.semi !== undefined ? options.semi : true,
80+
singleQuote: options?.singleQuote || false,
81+
printWidth: options?.printWidth || 80,
82+
};
83+
}
84+
85+
/**
86+
* Format code according to specified options
87+
* @param code The code to format
88+
* @param options Formatting options
89+
* @returns A promise resolving to the formatting result
90+
*/
91+
async format(code: string, options: FormatOptions): Promise<FormatResult> {
92+
try {
93+
if (!code || typeof code !== "string") {
94+
return {
95+
success: false,
96+
error: "Invalid code input: code must be a non-empty string",
97+
};
98+
}
99+
100+
if (!options.language) {
101+
return {
102+
success: false,
103+
error: "Language must be specified in format options",
104+
};
105+
}
106+
107+
this.log(`Formatting code with language: ${options.language}`);
108+
109+
const prettierOptions = this.toPrettierOptions(options.language, options);
110+
const formattedCode = await format(code, prettierOptions);
111+
112+
return {
113+
success: true,
114+
data: formattedCode,
115+
};
116+
} catch (error) {
117+
this.log(
118+
`Formatting failed: ${error instanceof Error ? error.message : "Unknown error"}`,
119+
);
120+
return {
121+
success: false,
122+
error: `Formatting failed: ${error instanceof Error ? error.message : "Unknown error"}`,
123+
};
124+
}
125+
}
126+
127+
/**
128+
* Lint code to identify issues
129+
* @param code The code to lint
130+
* @param options Linting options
131+
* @returns A promise resolving to the linting result
132+
*/
133+
async lint(code: string, options: LintOptions): Promise<LintResult> {
134+
try {
135+
if (!code || typeof code !== "string") {
136+
return {
137+
success: false,
138+
error: "Invalid code input: code must be a non-empty string",
139+
};
140+
}
141+
142+
if (!options.language) {
143+
return {
144+
success: false,
145+
error: "Language must be specified in lint options",
146+
};
147+
}
148+
149+
this.log(`Linting code with language: ${options.language}`);
150+
151+
const eslintOptions = {
152+
overrideConfig: {
153+
languageOptions: {
154+
ecmaVersion: 2020,
155+
sourceType: "module",
156+
},
157+
rules: options.rules || {},
158+
},
159+
overrideConfigFile: true,
160+
} as ESLint.Options;
161+
162+
const linter = new ESLint(eslintOptions);
163+
const eslintResult = await linter.lintText(code);
164+
165+
return {
166+
success: true,
167+
data: {
168+
issues: eslintResult.flatMap((result) =>
169+
result.messages.map((message) => ({
170+
line: message.line,
171+
column: message.column,
172+
severity: message.severity === 2 ? "error" : "warning",
173+
rule: message.ruleId || "",
174+
message: message.message,
175+
})),
176+
),
177+
},
178+
};
179+
} catch (error) {
180+
this.log(
181+
`Linting failed: ${error instanceof Error ? error.message : "Unknown error"}`,
182+
);
183+
return {
184+
success: false,
185+
error: `Linting failed: ${error instanceof Error ? error.message : "Unknown error"}`,
186+
};
187+
}
188+
}
189+
}

0 commit comments

Comments
 (0)