Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1dc696

Browse files
committedMar 12, 2019
Fix trash
1 parent 3155eb7 commit e1dc696

File tree

8 files changed

+435
-206
lines changed

8 files changed

+435
-206
lines changed
 

‎package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
},
5252
"dependencies": {
5353
"node-loader": "^0.6.0",
54-
"trash": "^4.3.0",
5554
"webpack-merge": "^4.2.1"
5655
}
5756
}

‎packages/ide/src/fill/electron.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,8 @@ class Clipboard {
180180

181181
class Shell {
182182
public async moveItemToTrash(path: string): Promise<void> {
183-
await client.evaluate((_helper, path) => {
184-
const trash = __non_webpack_require__("trash") as typeof import("trash");
185-
186-
return trash(path);
183+
await client.evaluate((helper, path) => {
184+
return helper.modules.trash(path);
187185
}, path);
188186
}
189187
}

‎packages/protocol/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"google-protobuf": "^3.6.1",
77
"node-pty-prebuilt": "^0.7.6",
88
"spdlog": "^0.7.2",
9+
"trash": "^4.3.0",
910
"tslib": "^1.9.3",
1011
"ws": "^6.1.2"
1112
},

‎packages/protocol/src/common/helpers.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="../../../../lib/vscode/src/typings/spdlog.d.ts" />
2+
/// <reference path="../../node_modules/node-pty-prebuilt/typings/node-pty.d.ts" />
23
import { ChildProcess, SpawnOptions, ForkOptions } from "child_process";
34
import { EventEmitter } from "events";
45
import { Socket } from "net";
@@ -20,22 +21,25 @@ interface ActiveEvalEmitter {
2021
on(event: string, cb: (...args: any[]) => void): void;
2122
}
2223

24+
/**
25+
* For any non-external modules that are not built in, we need to require and
26+
* access them server-side. A require on the client-side won't work since that
27+
* code won't exist on the server (and bloat the client with an unused import),
28+
* and we can't manually import on the server-side and then call
29+
* `__webpack_require__` on the client-side because Webpack stores modules by
30+
* their paths which would require us to hard-code the path.
31+
*/
32+
export interface Modules {
33+
pty: typeof import("node-pty");
34+
spdlog: typeof import("spdlog");
35+
trash: typeof import("trash");
36+
}
37+
2338
/**
2439
* Helper class for server-side evaluations.
2540
*/
2641
export class EvalHelper {
27-
// For any non-external modules that are not built in, we need to require and
28-
// access them here. A require on the client-side won't work since that code
29-
// won't exist on the server (and bloat the client with an unused import), and
30-
// we can't manually import on the server-side and then call
31-
// `__webpack_require__` on the client-side because Webpack stores modules by
32-
// their paths which would require us to hard-code the path. These aren't
33-
// required immediately so we have a chance to unpack the .node files and set
34-
// their locations.
35-
public modules = {
36-
spdlog: require("spdlog") as typeof import("spdlog"),
37-
pty: require("node-pty-prebuilt") as typeof import("node-pty"),
38-
};
42+
public constructor(public modules: Modules) {}
3943

4044
/**
4145
* Some spawn code tries to preserve the env (the debug adapter for instance)
@@ -113,11 +117,11 @@ export class ActiveEvalHelper implements ActiveEvalEmitter {
113117
* Helper class for server-side active evaluations.
114118
*/
115119
export class ServerActiveEvalHelper extends ActiveEvalHelper implements EvalHelper {
116-
private readonly evalHelper = new EvalHelper();
117-
public modules = this.evalHelper.modules;
120+
private readonly evalHelper: EvalHelper;
118121

119-
public constructor(emitter: ActiveEvalEmitter, public readonly fork: ForkProvider) {
122+
public constructor(public modules: Modules, emitter: ActiveEvalEmitter, public readonly fork: ForkProvider) {
120123
super(emitter);
124+
this.evalHelper = new EvalHelper(modules);
121125
}
122126

123127
public preserveEnv(options: SpawnOptions | ForkOptions): void {
@@ -208,7 +212,7 @@ export class ServerActiveEvalHelper extends ActiveEvalHelper implements EvalHelp
208212
}
209213

210214
public createUnique(id: number | "stdout" | "stderr" | "stdin"): ServerActiveEvalHelper {
211-
return new ServerActiveEvalHelper(this.createUniqueEmitter(id), this.fork);
215+
return new ServerActiveEvalHelper(this.modules, this.createUniqueEmitter(id), this.fork);
212216
}
213217
}
214218

‎packages/protocol/src/node/evaluate.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as vm from "vm";
44
import { logger, field } from "@coder/logger";
55
import { NewEvalMessage, EvalFailedMessage, EvalDoneMessage, ServerMessage, EvalEventMessage } from "../proto";
66
import { SendableConnection } from "../common/connection";
7-
import { ServerActiveEvalHelper, EvalHelper, ForkProvider } from "../common/helpers";
7+
import { ServerActiveEvalHelper, EvalHelper, ForkProvider, Modules } from "../common/helpers";
88
import { stringify, parse } from "../common/util";
99

1010
export interface ActiveEvaluation {
@@ -57,9 +57,15 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
5757
onDispose();
5858
};
5959

60+
const modules: Modules = {
61+
spdlog: require("spdlog"),
62+
pty: require("node-pty-prebuilt"),
63+
trash: require("trash"),
64+
};
65+
6066
let eventEmitter = message.getActive() ? new EventEmitter(): undefined;
6167
const sandbox = {
62-
helper: eventEmitter ? new ServerActiveEvalHelper({
68+
helper: eventEmitter ? new ServerActiveEvalHelper(modules, {
6369
removeAllListeners: (event?: string): void => {
6470
eventEmitter!.removeAllListeners(event);
6571
},
@@ -89,7 +95,7 @@ export const evaluate = (connection: SendableConnection, message: NewEvalMessage
8995
connection.send(serverMsg.serializeBinary());
9096
},
9197
// tslint:enable no-any
92-
}, fork || cpFork) : new EvalHelper(),
98+
}, fork || cpFork) : new EvalHelper(modules),
9399
_Buffer: Buffer,
94100
// When the client is ran from Webpack, it will replace
95101
// __non_webpack_require__ with require, which we then need to provide to

0 commit comments

Comments
 (0)
Please sign in to comment.