Skip to content

Commit 86d70ec

Browse files
committed
Add constants file
1 parent 81f48b8 commit 86d70ec

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

packages/server/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
out
22
cli*
3+
!cli.ts
34
build
45
resources
56

67
# This file is generated when the binary is created.
78
# We want to use the parent tsconfig so we can ignore it.
8-
tsconfig.json
9+
tsconfig.json

packages/server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build:webpack": "rm -rf ./out && export CLI=true && ../../node_modules/.bin/webpack --config ./webpack.config.js",
99
"build:nexe": "node scripts/nexe.js",
1010
"build:bootstrap-fork": "cd ../vscode && npm run build:bootstrap-fork; mkdir -p ./packages/server/resources; cp ./bin/bootstrap-fork.js ../server/resources/bootstrap-fork.js",
11-
"build:default-extensions": "cd ../../lib/vscode && npx gulp vscode-linux-arm && cd ../.. && mkdir -p ./packages/server/resources; cp -r ./lib/VSCode-linux-arm/resources/app/extensions/* ./packages/server/resources/extensions/",
11+
"build:default-extensions": "cd ../../lib/vscode && npx gulp vscode-linux-arm && cd ../.. && mkdir -p ./packages/server/resources/extensions; cp -r ./lib/VSCode-linux-arm/resources/app/extensions/* ./packages/server/resources/extensions/",
1212
"build:web": "cd ../web; rm -rf ./out; NODE_ENV=production npm run build; rm -rf ../server/resources/web; mkdir -p ../server/resources/web; cp -r ./out/* ../server/resources/web",
1313
"build": "npm run build:bootstrap-fork && npm run build:webpack && npm run build:nexe"
1414
},

packages/server/src/cli.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { requireModule } from "./vscode/bootstrapFork";
1010
import { SharedProcess, SharedProcessState } from "./vscode/sharedProcess";
1111
import { setup as setupNativeModules } from "./modules";
1212
import { fillFs } from "./fill";
13+
import { isCli, serveStatic, buildDir } from "./constants";
1314

1415
export class Entry extends Command {
1516
public static description = "Start your own self-hosted browser-accessible VS Code";
@@ -50,7 +51,7 @@ export class Entry extends Command {
5051
logger.warn("Failed to remove extracted dependency.", field("dependency", "spdlog"), field("error", ex.message));
5152
}
5253

53-
if (process.env.CLI) {
54+
if (isCli) {
5455
fillFs();
5556
}
5657

@@ -60,7 +61,7 @@ export class Entry extends Command {
6061
Object.assign(process.env, JSON.parse(flags.env));
6162
}
6263

63-
const builtInExtensionsDir = path.join(process.env.BUILD_DIR || path.join(__dirname, ".."), "build/extensions");
64+
const builtInExtensionsDir = path.join(buildDir || path.join(__dirname, ".."), "build/extensions");
6465
if (flags["bootstrap-fork"]) {
6566
const modulePath = flags["bootstrap-fork"];
6667
if (!modulePath) {
@@ -74,8 +75,8 @@ export class Entry extends Command {
7475
const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-remote");
7576
const workingDir = args["workdir"];
7677

77-
if (process.env.BUILD_DIR && process.env.BUILD_DIR.startsWith(workingDir)) {
78-
logger.error("Cannot run binary inside of BUILD_DIR", field("build_dir", process.env.BUILD_DIR), field("cwd", process.cwd()));
78+
if (buildDir && buildDir.startsWith(workingDir)) {
79+
logger.error("Cannot run binary inside of BUILD_DIR", field("build_dir", buildDir), field("cwd", process.cwd()));
7980
process.exit(1);
8081
}
8182

@@ -114,7 +115,7 @@ export class Entry extends Command {
114115

115116
next();
116117
});
117-
if ((process.env.CLI === "false" || !process.env.CLI) && !process.env.SERVE_STATIC) {
118+
if (!isCli && !serveStatic) {
118119
const webpackConfig = require(path.join(__dirname, "..", "..", "web", "webpack.config.js"));
119120
const compiler = require("webpack")(webpackConfig);
120121
app.use(require("webpack-dev-middleware")(compiler, {
@@ -163,6 +164,6 @@ export class Entry extends Command {
163164
}
164165

165166
Entry.run(undefined, {
166-
root: process.env.BUILD_DIR as string || __dirname,
167+
root: buildDir || __dirname,
167168
//@ts-ignore
168169
}).catch(require("@oclif/errors/handle"));

packages/server/src/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isCli = typeof process.env.CLI !== "undefined" && process.env.CLI !== "false";
2+
export const serveStatic = typeof process.env.SERVE_STATIC !== "undefined" && process.env.SERVE_STATIC !== "false";
3+
export const buildDir = process.env.BUILD_DIR;

packages/server/src/fill.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "fs";
22
import * as util from "util";
3+
import { isCli } from "./constants";
34

45
const oldAccess = fs.access;
56
const existsWithinBinary = (path: fs.PathLike): Promise<boolean> => {
@@ -24,7 +25,7 @@ export const fillFs = (): void => {
2425
* For impls
2526
*/
2627

27-
if (!process.env.CLI) {
28+
if (!isCli) {
2829
throw new Error("Should not fill FS when not in CLI");
2930
}
3031

packages/server/src/modules.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as fs from "fs";
22
import * as path from "path";
3+
import { isCli, buildDir } from "./constants";
34

45
declare var __non_webpack_require__: typeof require;
56

67
/**
78
* Handling of native modules within the CLI
89
*/
910
export const setup = (dataDirectory: string): void => {
10-
if (!process.env.CLI) {
11+
if (!isCli) {
1112
return;
1213
}
1314

@@ -20,7 +21,7 @@ export const setup = (dataDirectory: string): void => {
2021
}
2122

2223
const unpackModule = (moduleName: string): void => {
23-
const memFile = path.join(process.env.BUILD_DIR!, "build/modules", moduleName + ".node");
24+
const memFile = path.join(buildDir!, "build/modules", moduleName + ".node");
2425
const diskFile = path.join(dataDirectory, "modules", moduleName + ".node");
2526
if (!fs.existsSync(diskFile)) {
2627
fs.writeFileSync(diskFile, fs.readFileSync(memFile));
@@ -38,4 +39,4 @@ export const setup = (dataDirectory: string): void => {
3839
return __non_webpack_require__(path.join(dataDirectory, "modules", modName + ".node"));
3940
};
4041
require("../../protocol/node_modules/node-pty/lib/index") as typeof import("../../protocol/node_modules/node-pty/src/index");
41-
};
42+
};

packages/server/src/server.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as path from "path";
1313
import * as util from "util";
1414
import * as ws from "ws";
1515
import { forkModule } from "./vscode/bootstrapFork";
16+
import { isCli, buildDir } from "./constants";
1617

1718
export const createApp = (registerMiddleware?: (app: express.Application) => void, options?: ServerOptions): {
1819
readonly express: express.Application;
@@ -69,8 +70,8 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi
6970
} : undefined);
7071
});
7172

72-
const baseDir = process.env.BUILD_DIR || path.join(__dirname, "..");
73-
if (process.env.CLI) {
73+
const baseDir = buildDir || path.join(__dirname, "..");
74+
if (isCli) {
7475
app.use(expressStaticGzip(path.join(baseDir, "build/web")));
7576
} else {
7677
app.use(express.static(path.join(baseDir, "resources/web")));
@@ -84,16 +85,14 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi
8485
// }
8586
const exists = fs.existsSync(fullPath);
8687
if (!exists) {
87-
res.status(404).end();
88-
return;
88+
return res.status(404).end();
8989
}
9090
const stat = await util.promisify(fs.stat)(fullPath);
9191
if (!stat.isFile()) {
9292
res.write("Resource must be a file.");
9393
res.status(422);
94-
res.end();
9594

96-
return;
95+
return res.end();
9796
}
9897
let mimeType = mime.lookup(fullPath);
9998
if (mimeType === false) {

packages/server/src/vscode/bootstrapFork.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from "fs";
33
import * as path from "path";
44
import * as zlib from "zlib";
55
import * as vm from "vm";
6+
import { isCli } from "../constants";
67

78
export const requireModule = (modulePath: string, builtInExtensionsDir: string): void => {
89
process.env.AMD_ENTRYPOINT = modulePath;
@@ -37,7 +38,7 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string):
3738
const readFile = (name: string): Buffer => {
3839
return fs.readFileSync(path.join(process.env.BUILD_DIR as string || path.join(__dirname, "../.."), "./build", name));
3940
};
40-
if (process.env.CLI) {
41+
if (isCli) {
4142
content = zlib.gunzipSync(readFile("bootstrap-fork.js.gz"));
4243
} else {
4344
content = readFile("../resources/bootstrap-fork.js");
@@ -63,7 +64,7 @@ export const forkModule = (modulePath: string, env?: NodeJS.ProcessEnv): cp.Chil
6364
const options: cp.SpawnOptions = {
6465
stdio: [null, null, null, "ipc"],
6566
};
66-
if (process.env.CLI === "true") {
67+
if (isCli) {
6768
proc = cp.execFile(process.execPath, args, options);
6869
} else {
6970
proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...args], options);

0 commit comments

Comments
 (0)