Skip to content

Commit 4d4a92e

Browse files
committed
Minor fixes to make sure diffing works
1 parent dee54eb commit 4d4a92e

7 files changed

Lines changed: 114 additions & 57 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"vscode:prepublish": "npm run esbuild-base -- --minify",
5555
"build:base": "rm -rf dist/ && esbuild ./src/extension.ts --bundle --outfile=dist/main.js --external:vscode --format=cjs --platform=node",
5656
"build": "npm run build:base -- --sourcemap",
57-
"dev": "npm run build:base -- --sourcemap --watch",
57+
"build:watch": "npm run build:base -- --sourcemap --watch",
5858
"test-compile": "tsc -p ./",
5959
"package": "npx vsce package",
6060
"lint": "eslint . --fix --cache --ext .ts,.tsx",

server/src/routes.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@ import { FastifyInstance } from "fastify";
22
import prisma from "./db";
33
import jwt, { JwtPayload } from "jsonwebtoken";
44

5+
let dbDiff = "";
6+
57
export default async function routesPlugin(fastify: FastifyInstance) {
8+
fastify.route({
9+
method: "POST",
10+
url: "/changes",
11+
handler: async (request, reply) => {
12+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
13+
// @ts-ignore
14+
const { diff, deviceId, projectId } = request.body;
15+
16+
dbDiff = diff;
17+
18+
reply.send({ success: true });
19+
},
20+
});
21+
22+
fastify.route({
23+
method: "POST",
24+
url: "/change",
25+
handler: async (request, reply) => {
26+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27+
// @ts-ignore
28+
const { deviceId, projectId } = request.body;
29+
30+
reply.send({ success: true, diff: dbDiff });
31+
},
32+
});
33+
634
fastify.route({
735
method: "POST",
836
url: "/refresh",

solid/src/App.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,27 @@ export const [projects, { mutate: mutateProjects, refetch: refetchProjects }] =
6565
return projectsData;
6666
});
6767

68-
/**
69-
* Send changes to the server.
70-
* They can be accessed from any connected device by pressing 'Receive'
71-
* button of the device that they were sent from (nice english i got there)
72-
*/
73-
const onSend = () => {
74-
console.log("onSend");
68+
const onSend = ({
69+
deviceId,
70+
projectId,
71+
}: {
72+
deviceId: string;
73+
projectId: string;
74+
}) => {
7575
vscodeApi.postMessage({
7676
type: "send",
77+
deviceId,
78+
projectId,
7779
});
7880
};
7981

80-
/**
81-
* Each device in devices list have a 'Receive' button
82-
* which allows to download last changes from that device and apply them
83-
*/
8482
const onReceive = ({
8583
deviceId,
8684
projectId,
8785
}: {
8886
deviceId: string;
8987
projectId: string;
9088
}) => {
91-
console.log("onReceive");
9289
vscodeApi.postMessage({
9390
type: "receive",
9491
deviceId,
@@ -140,7 +137,14 @@ function App() {
140137
<div class="flex flex-col mt-3 justify-center items-stretch gap-2">
141138
<button
142139
class={`bg-vsgreen ${btn} py-2 px-6`}
143-
onClick={onSend}
140+
onClick={() =>
141+
onSend({
142+
deviceId: targetDevice()
143+
?.id as string,
144+
projectId: targetProject()
145+
?.id as string,
146+
})
147+
}
144148
>
145149
PUSH CHANGES TO SERVER
146150
</button>

src/extension.ts

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,14 @@ export async function activate(context: vscode.ExtensionContext) {
3333
context.subscriptions.push(
3434
vscode.commands.registerCommand(
3535
"codesync.sendChanges",
36-
async (deviceId: number) => {
36+
async ({ deviceId, projectId }) => {
3737
const git = await getGitAPI();
38-
console.log("sd");
3938
const repositories = git?.repositories || [];
4039
// TODO: should pick correct repo if multiple are open
4140
const repo = await git?.init(repositories[0].rootUri);
42-
4341
const cmd_add_to_index = `git status --porcelain | sed -r 's/^.{3}//' | while read line; do git add -N $line; done`;
44-
4542
const cwd = vscode.workspace.workspaceFolders![0].uri.fsPath;
46-
console.log(cwd);
43+
4744
child_process.exec(
4845
cmd_add_to_index,
4946
{
@@ -52,16 +49,11 @@ export async function activate(context: vscode.ExtensionContext) {
5249
(error, stdout, stderr) => {
5350
// TODO: move `| sed -r 's/^.{3}//' | while read line; do git add -N $line; done`
5451
// to here, to remove dependency on bash commands
55-
5652
if (error) {
5753
vscode.window.showErrorMessage(
58-
"Failed to create patch",
54+
"Failed to add to index.",
5955
error.message
6056
);
61-
} else {
62-
vscode.window.showInformationMessage(
63-
"Patch files created successfully"
64-
);
6557
}
6658
}
6759
);
@@ -73,23 +65,21 @@ export async function activate(context: vscode.ExtensionContext) {
7365
diff.staged = (await repo?.diff(true)) || "";
7466
diff.unstaged = (await repo?.diff()) || "";
7567

76-
console.log("STAGE", diff.staged);
77-
console.log("UNSTAGE", diff.unstaged);
78-
7968
const response = await axios.post(
8069
"http://localhost:3001/changes",
8170
{
82-
diff,
71+
diff: JSON.stringify(diff),
8372
deviceId,
73+
projectId,
8474
}
8575
);
8676

8777
if (response.data.success) {
88-
vscode.window.showInformationMessage("Changes saved");
89-
} else {
90-
vscode.window.showErrorMessage(
91-
"Error sending and saving message"
78+
vscode.window.showInformationMessage(
79+
"Successfully created diff"
9280
);
81+
} else {
82+
vscode.window.showErrorMessage("Error, try again");
9383
}
9484
}
9585
)
@@ -98,43 +88,78 @@ export async function activate(context: vscode.ExtensionContext) {
9888
context.subscriptions.push(
9989
vscode.commands.registerCommand(
10090
"codesync.applyChanges",
101-
async (deviceId: number) => {
102-
// const git = await getGitAPI();
103-
// const repositories = git?.repositories || [];
104-
// TODO: check if multiple repos open
105-
// const repo = await git?.init(repositories[0].rootUri);
91+
async ({ deviceId, projectId }) => {
92+
const git = await getGitAPI();
93+
const repositories = git?.repositories || [];
94+
// TODO: should pick correct repo if multiple are open
95+
const repo = await git?.init(repositories[0].rootUri);
10696

10797
const currentDir = vscode.workspace.workspaceFolders || [];
98+
const stagedPatch = `${currentDir[0].uri.fsPath}/staged.patch`;
99+
const unstagedPatch = `${currentDir[0].uri.fsPath}/unstaged.patch`;
108100

109101
try {
110-
const retrievedChanges = await axios.get(
111-
"http://localhost:3001/changes",
102+
const retrievedChanges = await axios.post(
103+
"http://localhost:3001/change",
112104
{
113-
data: { deviceId },
105+
deviceId,
106+
projectId,
114107
}
115108
);
116-
const diff = retrievedChanges.data.diff;
109+
if (!retrievedChanges.data.success) {
110+
throw new Error();
111+
}
112+
113+
const diff = JSON.parse(retrievedChanges.data.diff);
117114

118-
const stagedPatch = `${currentDir[0].uri.fsPath}/staged.patch`;
119115
await vscode.workspace.fs.writeFile(
120116
vscode.Uri.file(stagedPatch),
121117
Buffer.from(diff.staged)
122118
);
123119

124-
const unstagedPatch = `${currentDir[0].uri.fsPath}/unstaged.patch`;
125120
await vscode.workspace.fs.writeFile(
126121
vscode.Uri.file(unstagedPatch),
127122
Buffer.from(diff.unstaged)
128123
);
129-
} catch (error) {
130-
console.error(error);
131-
}
132124

133-
// repo?.apply(patcFilePath);
125+
await repo?.apply(stagedPatch);
126+
127+
const cmd_stage = `git add -A && git reset -- *.patch`;
128+
const cwd =
129+
vscode.workspace.workspaceFolders![0].uri.fsPath;
130+
131+
child_process.exec(
132+
cmd_stage,
133+
{
134+
cwd,
135+
},
136+
(error, stdout, stderr) => {
137+
// TODO: move `| sed -r 's/^.{3}//' | while read line; do git add -N $line; done`
138+
// to here, to remove dependency on bash commands
139+
if (error) {
140+
vscode.window.showErrorMessage(
141+
"Failed to staged.",
142+
error.message
143+
);
144+
}
145+
}
146+
);
134147

135-
// await vscode.workspace.fs.delete(vscode.Uri.file(patcFilePath));
148+
await repo?.apply(unstagedPatch);
136149

137-
vscode.window.showInformationMessage("Patch file created");
150+
await vscode.workspace.fs.delete(
151+
vscode.Uri.file(stagedPatch)
152+
);
153+
await vscode.workspace.fs.delete(
154+
vscode.Uri.file(unstagedPatch)
155+
);
156+
157+
vscode.window.showInformationMessage(
158+
"Success. Patch file created and applied"
159+
);
160+
} catch (error) {
161+
vscode.window.showErrorMessage("Error, try again");
162+
}
138163
}
139164
)
140165
);

src/test/runTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function main() {
1515
// Download VS Code, unzip it and run the integration test
1616
await runTests({ extensionDevelopmentPath, extensionTestsPath });
1717
} catch (err) {
18+
// eslint-disable-next-line no-console
1819
console.error("Failed to run tests");
1920
process.exit(1);
2021
}

src/test/suite/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as path from "path";
2-
import * as Mocha from "mocha";
3-
import * as glob from "glob";
1+
import path from "path";
2+
import Mocha from "mocha";
3+
import glob from "glob";
44

55
export function run(): Promise<void> {
66
// Create the mocha test
@@ -30,6 +30,7 @@ export function run(): Promise<void> {
3030
}
3131
});
3232
} catch (err) {
33+
// eslint-disable-next-line no-console
3334
console.error(err);
3435
e(err);
3536
}

src/webviewProvider.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,21 @@ export class CodesyncWebviewProvider implements vscode.WebviewViewProvider {
2222
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
2323

2424
webviewView.webview.onDidReceiveMessage((data) => {
25-
console.log("onDidReceiveMessage", data);
26-
2725
switch (data.type) {
2826
case "send": {
29-
console.log("send");
3027
vscode.commands.executeCommand("codesync.sendChanges", {
3128
deviceId: data.deviceId,
29+
projectId: data.projectId,
3230
});
3331
// vscode.window.activeTextEditor?.insertSnippet(
3432
// new vscode.SnippetString(`#${data.value}`)
3533
// );
3634
break;
3735
}
3836
case "receive": {
39-
console.log("receive");
4037
vscode.commands.executeCommand("codesync.applyChanges", {
4138
deviceId: data.deviceId,
39+
projectId: data.projectId,
4240
});
4341
// vscode.window.activeTextEditor?.insertSnippet(
4442
// new vscode.SnippetString(`#${data.value}`)

0 commit comments

Comments
 (0)