Skip to content

Commit 6be88d7

Browse files
committed
its working
1 parent 41a9313 commit 6be88d7

File tree

2 files changed

+73
-82
lines changed

2 files changed

+73
-82
lines changed

src/node/routes/vscode.ts

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { logger } from "@coder/logger"
22
import * as express from "express"
3+
import * as path from "path"
34
import { WebsocketRequest } from "../../../typings/pluginapi"
45
import { logError } from "../../common/util"
56
import { toVsCodeArgs } from "../cli"
67
import { isDevMode } from "../constants"
78
import { authenticated, ensureAuthenticated, redirect, self } from "../http"
89
import { SocketProxyProvider } from "../socket"
9-
import { loadAMDModule } from "../util"
10+
import { isFile, loadAMDModule } from "../util"
1011
import { Router as WsRouter } from "../wsRouter"
1112
import { errorHandler } from "./errors"
1213

@@ -33,42 +34,47 @@ export class CodeServerRouteWrapper {
3334
})
3435
}
3536

36-
const { query } = await req.settings.read()
37+
const settings = await req.settings.read()
38+
const lastOpened = settings.query || {}
3739

38-
if (!query && req.args["_"].length > 0) {
39-
// TODO@jsjoeio - we alos need to check for workspace arg
40-
const folder = req.args["_"][0]
41-
42-
await req.settings.write({
43-
query: {
44-
folder,
45-
},
46-
})
47-
48-
const to = self(req)
49-
return redirect(req, res, to, {
50-
folder,
51-
})
40+
// Ew means the workspace was closed so clear the last folder/workspace.
41+
if (req.query.ew) {
42+
delete lastOpened.folder
43+
delete lastOpened.workspace
5244
}
53-
if (query) {
54-
// Ew means the workspace was closed so clear the last folder/workspace.
55-
if (req.query.ew) {
56-
delete query.folder
57-
delete query.workspace
58-
}
5945

46+
if (!req.query.folder && !req.query.workspace) {
6047
// Redirect to the last folder/workspace if nothing else is opened.
6148
if (
62-
!req.query.folder &&
63-
!req.query.workspace &&
64-
(query.folder || query.workspace) &&
49+
(lastOpened.folder || lastOpened.workspace) &&
6550
!req.args["ignore-last-opened"] // This flag disables this behavior.
6651
) {
6752
const to = self(req)
6853
return redirect(req, res, to, {
69-
folder: query.folder,
70-
workspace: query.workspace,
54+
folder: lastOpened.folder,
55+
workspace: lastOpened.workspace,
7156
})
57+
} else if (req.args._.length > 0) {
58+
let folder = undefined
59+
let workspace = undefined
60+
const newQueries: any = {}
61+
if (req.args._.length) {
62+
const lastEntry = path.resolve(req.args._[req.args._.length - 1])
63+
const entryIsFile = await isFile(lastEntry)
64+
if (entryIsFile && path.extname(lastEntry) === ".code-workspace") {
65+
workspace = lastEntry
66+
newQueries.workspace = workspace
67+
} else if (!entryIsFile) {
68+
folder = lastEntry
69+
newQueries.folder = folder
70+
}
71+
72+
const to = self(req)
73+
return redirect(req, res, to, {
74+
folder,
75+
workspace,
76+
})
77+
}
7278
}
7379
}
7480

test/unit/node/routes/vscode.test.ts

+40-55
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
import { promises as fs } from "fs"
2-
import { Response } from "node-fetch"
32
import * as path from "path"
43
import { clean, tmpdir } from "../../../utils/helpers"
54
import * as httpserver from "../../../utils/httpserver"
65
import * as integration from "../../../utils/integration"
76

8-
interface WorkbenchConfig {
9-
folderUri?: {
10-
path: string
11-
}
12-
workspaceUri?: {
13-
path: string
14-
}
15-
}
16-
177
describe("vscode", () => {
188
let codeServer: httpserver.HttpServer | undefined
199

@@ -52,52 +42,25 @@ describe("vscode", () => {
5242
}
5343
})
5444

55-
/**
56-
* Get the workbench config from the provided response.
57-
*/
58-
const getConfig = async (resp: Response): Promise<WorkbenchConfig> => {
59-
expect(resp.status).toBe(200)
60-
const html = await resp.text()
61-
const match = html.match(/<meta id="vscode-workbench-web-configuration" data-settings="(.+)">/)
62-
if (!match || !match[1]) {
63-
throw new Error("Unable to find workbench configuration")
64-
}
65-
const config = match[1].replace(/&quot;/g, '"')
66-
try {
67-
return JSON.parse(config)
68-
} catch (error) {
69-
console.error("Failed to parse workbench configuration", config)
70-
throw error
71-
}
72-
}
73-
74-
it("should have no default folder or workspace", async () => {
75-
codeServer = await integration.setup(["--auth=none"], "")
76-
77-
const config = await getConfig(await codeServer.fetch("/"))
78-
expect(config.folderUri).toBeUndefined()
79-
expect(config.workspaceUri).toBeUndefined()
80-
})
81-
82-
it("should have a default folder", async () => {
83-
const defaultDir = await tmpdir(testName)
84-
codeServer = await integration.setup(["--auth=none", defaultDir], "")
45+
it("should redirect to the passed in workspace", async () => {
46+
const workspace = path.join(await tmpdir(testName), "test.code-workspace")
47+
await fs.writeFile(workspace, "")
48+
codeServer = await integration.setup(["--auth=none", workspace], "")
8549

86-
// At first it will load the directory provided on the command line.
87-
const config = await getConfig(await codeServer.fetch("/"))
88-
expect(config.folderUri?.path).toBe(defaultDir)
89-
expect(config.workspaceUri).toBeUndefined()
50+
const resp = await codeServer.fetch("/")
51+
const url = new URL(resp.url)
52+
expect(url.pathname).toBe("/")
53+
expect(url.search).toBe(`?workspace=${workspace}`)
9054
})
9155

92-
it("should have a default workspace", async () => {
93-
const defaultWorkspace = path.join(await tmpdir(testName), "test.code-workspace")
94-
await fs.writeFile(defaultWorkspace, "")
95-
codeServer = await integration.setup(["--auth=none", defaultWorkspace], "")
56+
it("should redirect to the passed in directory", async () => {
57+
const folder = await tmpdir(testName)
58+
codeServer = await integration.setup(["--auth=none", folder], "")
9659

97-
// At first it will load the workspace provided on the command line.
98-
const config = await getConfig(await codeServer.fetch("/"))
99-
expect(config.folderUri).toBeUndefined()
100-
expect(config.workspaceUri?.path).toBe(defaultWorkspace)
60+
const resp = await codeServer.fetch("/")
61+
const url = new URL(resp.url)
62+
expect(url.pathname).toBe("/")
63+
expect(url.search).toBe(`?folder=${folder}`)
10164
})
10265

10366
it("should redirect to last query folder/workspace", async () => {
@@ -136,7 +99,31 @@ describe("vscode", () => {
13699
await resp.text()
137100
})
138101

139-
it.only("should add the folder as a query param maintaining the slashes", async () => {
102+
it("should add the workspace as a query param maintaining the slashes", async () => {
103+
const workspace = path.join(await tmpdir(testName), "test.code-workspace")
104+
await fs.writeFile(workspace, "")
105+
codeServer = await integration.setup(["--auth=none", workspace], "")
106+
107+
let resp = await codeServer.fetch("/", undefined)
108+
109+
expect(resp.status).toBe(200)
110+
const url = new URL(resp.url)
111+
expect(url.search).toBe(`?workspace=${workspace}`)
112+
await resp.text()
113+
})
114+
115+
it("should do nothing when nothing is passed in", async () => {
116+
codeServer = await integration.setup(["--auth=none"], "")
117+
118+
let resp = await codeServer.fetch("/", undefined)
119+
120+
expect(resp.status).toBe(200)
121+
const url = new URL(resp.url)
122+
expect(url.search).toBe("")
123+
await resp.text()
124+
})
125+
126+
it("should add the folder as a query param maintaining the slashes", async () => {
140127
const folder = await tmpdir(testName)
141128
codeServer = await integration.setup(["--auth=none", folder], "")
142129

@@ -148,8 +135,6 @@ describe("vscode", () => {
148135
await resp.text()
149136
})
150137

151-
// TODO@jsjoeio - what about workspace?
152-
153138
it("should not redirect when last opened is ignored", async () => {
154139
codeServer = await integration.setup(["--auth=none", "--ignore-last-opened"], "")
155140

0 commit comments

Comments
 (0)