Skip to content

Commit 15ac5ac

Browse files
code-asherkylecarbs
authored andcommitted
Add flag to install an extension from the command line (#504)
1 parent b27e5e3 commit 15ac5ac

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

packages/server/src/cli.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { field, logger } from "@coder/logger";
22
import { ServerMessage, SharedProcessActive } from "@coder/protocol/src/proto";
3-
import { ChildProcess, fork, ForkOptions, spawn } from "child_process";
3+
import { ChildProcess, fork, ForkOptions } from "child_process";
44
import { randomFillSync } from "crypto";
55
import * as fs from "fs";
66
import * as fse from "fs-extra";
@@ -29,6 +29,7 @@ commander.version(process.env.VERSION || "development")
2929
.option("-N, --no-auth", "Start without requiring authentication.", undefined)
3030
.option("-H, --allow-http", "Allow http connections.", false)
3131
.option("-P, --password <value>", "Specify a password for authentication.")
32+
.option("--install-extension <value>", "Install an extension by its ID.")
3233
.option("--bootstrap-fork <name>", "Used for development. Never set.")
3334
.option("--extra-args <args>", "Used for development. Never set.")
3435
.arguments("Specify working directory.")
@@ -61,6 +62,8 @@ const bold = (text: string | number): string | number => {
6162
readonly cert?: string;
6263
readonly certKey?: string;
6364

65+
readonly installExtension?: string;
66+
6467
readonly bootstrapFork?: string;
6568
readonly extraArgs?: string;
6669
};
@@ -112,11 +115,11 @@ const bold = (text: string | number): string | number => {
112115
process.exit(1);
113116
}
114117

115-
((options.extraArgs ? JSON.parse(options.extraArgs) : []) as string[]).forEach((arg, i) => {
116-
// [0] contains the binary running the script (`node` for example) and
117-
// [1] contains the script name, so the arguments come after that.
118-
process.argv[i + 2] = arg;
119-
});
118+
process.argv = [
119+
process.argv[0],
120+
process.argv[1],
121+
...(options.extraArgs ? JSON.parse(options.extraArgs) : []),
122+
];
120123

121124
return requireModule(modulePath, builtInExtensionsDir);
122125
}
@@ -162,6 +165,26 @@ const bold = (text: string | number): string | number => {
162165
logger.warn('"--data-dir" is deprecated. Use "--user-data-dir" instead.');
163166
}
164167

168+
if (options.installExtension) {
169+
const fork = forkModule("vs/code/node/cli", [
170+
"--user-data-dir", dataDir,
171+
"--builtin-extensions-dir", builtInExtensionsDir,
172+
"--extensions-dir", extensionsDir,
173+
"--install-extension", options.installExtension,
174+
], {
175+
env: {
176+
VSCODE_ALLOW_IO: "true",
177+
VSCODE_LOGS: process.env.VSCODE_LOGS,
178+
},
179+
}, dataDir);
180+
181+
fork.stdout.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.info(l)));
182+
fork.stderr.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.error(l)));
183+
fork.on("exit", () => process.exit());
184+
185+
return;
186+
}
187+
165188
// TODO: fill in appropriate doc url
166189
logger.info("Additional documentation: http://github.com/codercom/code-server");
167190
logger.info("Initializing", field("data-dir", dataDir), field("extensions-dir", extensionsDir), field("working-dir", workingDir), field("log-dir", logDir));

packages/server/src/vscode/bootstrapFork.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string):
8282
* @param modulePath Path of the VS Code module to load.
8383
*/
8484
export const forkModule = (modulePath: string, args?: string[], options?: cp.ForkOptions, dataDir?: string): cp.ChildProcess => {
85-
let proc: cp.ChildProcess;
8685
const forkOptions: cp.ForkOptions = {
8786
stdio: [null, null, null, "ipc"],
8887
};
@@ -91,18 +90,27 @@ export const forkModule = (modulePath: string, args?: string[], options?: cp.For
9190
delete options.env.ELECTRON_RUN_AS_NODE;
9291
forkOptions.env = options.env;
9392
}
93+
9494
const forkArgs = ["--bootstrap-fork", modulePath];
9595
if (args) {
9696
forkArgs.push("--extra-args", JSON.stringify(args));
9797
}
9898
if (dataDir) {
99-
forkArgs.push("--data-dir", dataDir);
99+
forkArgs.push("--user-data-dir", dataDir);
100100
}
101+
102+
const nodeArgs = [];
101103
if (isCli) {
102-
proc = cp.spawn(process.execPath, [path.join(buildDir, "out", "cli.js"), ...forkArgs], forkOptions);
104+
nodeArgs.push(path.join(buildDir, "out", "cli.js"));
103105
} else {
104-
proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...forkArgs], forkOptions);
106+
nodeArgs.push(
107+
"--require", "ts-node/register",
108+
"--require", "tsconfig-paths/register",
109+
process.argv[1],
110+
);
105111
}
112+
113+
const proc = cp.spawn(process.execPath, [...nodeArgs, ...forkArgs], forkOptions);
106114
if (args && args[0] === "--type=watcherService" && os.platform() === "linux") {
107115
cp.exec(`renice -n 19 -p ${proc.pid}`, (error) => {
108116
if (error) {

packages/vscode/src/fill/environmentService.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import * as path from "path";
21
import * as paths from "./paths";
32
import * as environment from "vs/platform/environment/node/environmentService";
43

4+
/**
5+
* Customize paths using data received from the initialization message.
6+
*/
57
export class EnvironmentService extends environment.EnvironmentService {
68
public get sharedIPCHandle(): string {
79
return paths.getSocketPath() || super.sharedIPCHandle;

packages/vscode/webpack.bootstrap.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = merge(
6060
"native-keymap": path.join(vsFills, "native-keymap.ts"),
6161
"native-watchdog": path.join(vsFills, "native-watchdog.ts"),
6262
"vs/base/common/amd": path.resolve(vsFills, "amd.ts"),
63-
"vs/base/node/paths": path.resolve(vsFills, "paths.ts"),
63+
"vs/base/node/paths": path.join(vsFills, "paths.ts"),
6464
"vs/platform/product/node/package": path.resolve(vsFills, "package.ts"),
6565
"vs/platform/product/node/product": path.resolve(vsFills, "product.ts"),
6666
"vs/base/node/zip": path.resolve(vsFills, "zip.ts"),

scripts/vscode.patch

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ index 6fd8249..04c0933 100644
158158
+ backupMainService.initialize().catch(console.error);
159159
@@ -223,0 +236 @@ async function handshake(configuration: ISharedProcessConfiguration): Promise<vo
160160
+startup({ machineId: "1" });
161+
diff --git a/src/vs/code/node/cli.ts b/src/vs/code/node/cli.ts
162+
index 1f8b17a..2a875f9 100644
163+
--- a/src/vs/code/node/cli.ts
164+
+++ b/src/vs/code/node/cli.ts
165+
@@ -43,0 +44,3 @@ export async function main(argv: string[]): Promise<any> {
166+
+ const cli = await new Promise<IMainCli>((c, e) => require(['vs/code/node/cliProcessMain'], c, e));
167+
+ await cli.main(args);
168+
+ return; // Always just do this for now.
161169
diff --git a/src/vs/editor/browser/config/configuration.ts b/src/vs/editor/browser/config/configuration.ts
162170
index f97a692..0206957 100644
163171
--- a/src/vs/editor/browser/config/configuration.ts

0 commit comments

Comments
 (0)