Skip to content

Add flag to install an extension from the command line #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions packages/server/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { field, logger } from "@coder/logger";
import { ServerMessage, SharedProcessActive } from "@coder/protocol/src/proto";
import { ChildProcess, fork, ForkOptions, spawn } from "child_process";
import { ChildProcess, fork, ForkOptions } from "child_process";
import { randomFillSync } from "crypto";
import * as fs from "fs";
import * as fse from "fs-extra";
Expand Down Expand Up @@ -29,6 +29,7 @@ commander.version(process.env.VERSION || "development")
.option("-N, --no-auth", "Start without requiring authentication.", undefined)
.option("-H, --allow-http", "Allow http connections.", false)
.option("-P, --password <value>", "Specify a password for authentication.")
.option("--install-extension <value>", "Install an extension by its ID.")
.option("--bootstrap-fork <name>", "Used for development. Never set.")
.option("--extra-args <args>", "Used for development. Never set.")
.arguments("Specify working directory.")
Expand Down Expand Up @@ -61,6 +62,8 @@ const bold = (text: string | number): string | number => {
readonly cert?: string;
readonly certKey?: string;

readonly installExtension?: string;

readonly bootstrapFork?: string;
readonly extraArgs?: string;
};
Expand Down Expand Up @@ -112,11 +115,11 @@ const bold = (text: string | number): string | number => {
process.exit(1);
}

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

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

if (options.installExtension) {
const fork = forkModule("vs/code/node/cli", [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super cool that this just works 🎊

"--user-data-dir", dataDir,
"--builtin-extensions-dir", builtInExtensionsDir,
"--extensions-dir", extensionsDir,
"--install-extension", options.installExtension,
], {
env: {
VSCODE_ALLOW_IO: "true",
VSCODE_LOGS: process.env.VSCODE_LOGS,
},
}, dataDir);

fork.stdout.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.info(l)));
fork.stderr.on("data", (d: Buffer) => d.toString().split("\n").forEach((l) => logger.error(l)));
fork.on("exit", () => process.exit());

return;
}

// TODO: fill in appropriate doc url
logger.info("Additional documentation: http://github.com/codercom/code-server");
logger.info("Initializing", field("data-dir", dataDir), field("extensions-dir", extensionsDir), field("working-dir", workingDir), field("log-dir", logDir));
Expand Down
16 changes: 12 additions & 4 deletions packages/server/src/vscode/bootstrapFork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string):
* @param modulePath Path of the VS Code module to load.
*/
export const forkModule = (modulePath: string, args?: string[], options?: cp.ForkOptions, dataDir?: string): cp.ChildProcess => {
let proc: cp.ChildProcess;
const forkOptions: cp.ForkOptions = {
stdio: [null, null, null, "ipc"],
};
Expand All @@ -91,18 +90,27 @@ export const forkModule = (modulePath: string, args?: string[], options?: cp.For
delete options.env.ELECTRON_RUN_AS_NODE;
forkOptions.env = options.env;
}

const forkArgs = ["--bootstrap-fork", modulePath];
if (args) {
forkArgs.push("--extra-args", JSON.stringify(args));
}
if (dataDir) {
forkArgs.push("--data-dir", dataDir);
forkArgs.push("--user-data-dir", dataDir);
}

const nodeArgs = [];
if (isCli) {
proc = cp.spawn(process.execPath, [path.join(buildDir, "out", "cli.js"), ...forkArgs], forkOptions);
nodeArgs.push(path.join(buildDir, "out", "cli.js"));
} else {
proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...forkArgs], forkOptions);
nodeArgs.push(
"--require", "ts-node/register",
"--require", "tsconfig-paths/register",
process.argv[1],
);
}

const proc = cp.spawn(process.execPath, [...nodeArgs, ...forkArgs], forkOptions);
if (args && args[0] === "--type=watcherService" && os.platform() === "linux") {
cp.exec(`renice -n 19 -p ${proc.pid}`, (error) => {
if (error) {
Expand Down
4 changes: 3 additions & 1 deletion packages/vscode/src/fill/environmentService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as path from "path";
import * as paths from "./paths";
import * as environment from "vs/platform/environment/node/environmentService";

/**
* Customize paths using data received from the initialization message.
*/
export class EnvironmentService extends environment.EnvironmentService {
public get sharedIPCHandle(): string {
return paths.getSocketPath() || super.sharedIPCHandle;
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/webpack.bootstrap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = merge(
"native-keymap": path.join(vsFills, "native-keymap.ts"),
"native-watchdog": path.join(vsFills, "native-watchdog.ts"),
"vs/base/common/amd": path.resolve(vsFills, "amd.ts"),
"vs/base/node/paths": path.resolve(vsFills, "paths.ts"),
"vs/base/node/paths": path.join(vsFills, "paths.ts"),
"vs/platform/product/node/package": path.resolve(vsFills, "package.ts"),
"vs/platform/product/node/product": path.resolve(vsFills, "product.ts"),
"vs/base/node/zip": path.resolve(vsFills, "zip.ts"),
Expand Down
8 changes: 8 additions & 0 deletions scripts/vscode.patch
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ index 6fd8249..039d31a 100644
@@ -223,0 +228,2 @@ async function handshake(configuration: ISharedProcessConfiguration): Promise<vo
+
+startup({ machineId: "1" });
diff --git a/src/vs/code/node/cli.ts b/src/vs/code/node/cli.ts
index 1f8b17a..2a875f9 100644
--- a/src/vs/code/node/cli.ts
+++ b/src/vs/code/node/cli.ts
@@ -43,0 +44,3 @@ export async function main(argv: string[]): Promise<any> {
+ const cli = await new Promise<IMainCli>((c, e) => require(['vs/code/node/cliProcessMain'], c, e));
+ await cli.main(args);
+ return; // Always just do this for now.
diff --git a/src/vs/editor/browser/config/configuration.ts b/src/vs/editor/browser/config/configuration.ts
index f97a692..0206957 100644
--- a/src/vs/editor/browser/config/configuration.ts
Expand Down