diff --git a/packages/server/.gitignore b/packages/server/.gitignore index 82dd717b..97b40dd7 100644 --- a/packages/server/.gitignore +++ b/packages/server/.gitignore @@ -1,8 +1,9 @@ out cli* +!cli.ts build resources # This file is generated when the binary is created. # We want to use the parent tsconfig so we can ignore it. -tsconfig.json \ No newline at end of file +tsconfig.json diff --git a/packages/server/package.json b/packages/server/package.json index 7d864f74..3064e193 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -8,7 +8,7 @@ "build:webpack": "rm -rf ./out && export CLI=true && ../../node_modules/.bin/webpack --config ./webpack.config.js", "build:nexe": "node scripts/nexe.js", "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", - "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/", + "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/", "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", "build": "npm run build:bootstrap-fork && npm run build:webpack && npm run build:nexe" }, diff --git a/packages/server/src/cli.ts b/packages/server/src/cli.ts index f0c9dc2e..dfbdc61a 100644 --- a/packages/server/src/cli.ts +++ b/packages/server/src/cli.ts @@ -10,6 +10,7 @@ import { requireModule } from "./vscode/bootstrapFork"; import { SharedProcess, SharedProcessState } from "./vscode/sharedProcess"; import { setup as setupNativeModules } from "./modules"; import { fillFs } from "./fill"; +import { isCli, serveStatic, buildDir } from "./constants"; export class Entry extends Command { public static description = "Start your own self-hosted browser-accessible VS Code"; @@ -50,7 +51,7 @@ export class Entry extends Command { logger.warn("Failed to remove extracted dependency.", field("dependency", "spdlog"), field("error", ex.message)); } - if (process.env.CLI) { + if (isCli) { fillFs(); } @@ -60,7 +61,7 @@ export class Entry extends Command { Object.assign(process.env, JSON.parse(flags.env)); } - const builtInExtensionsDir = path.join(process.env.BUILD_DIR || path.join(__dirname, ".."), "build/extensions"); + const builtInExtensionsDir = path.join(buildDir || path.join(__dirname, ".."), "build/extensions"); if (flags["bootstrap-fork"]) { const modulePath = flags["bootstrap-fork"]; if (!modulePath) { @@ -74,8 +75,8 @@ export class Entry extends Command { const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-remote"); const workingDir = args["workdir"]; - if (process.env.BUILD_DIR && process.env.BUILD_DIR.startsWith(workingDir)) { - logger.error("Cannot run binary inside of BUILD_DIR", field("build_dir", process.env.BUILD_DIR), field("cwd", process.cwd())); + if (buildDir && buildDir.startsWith(workingDir)) { + logger.error("Cannot run binary inside of BUILD_DIR", field("build_dir", buildDir), field("cwd", process.cwd())); process.exit(1); } @@ -114,7 +115,7 @@ export class Entry extends Command { next(); }); - if ((process.env.CLI === "false" || !process.env.CLI) && !process.env.SERVE_STATIC) { + if (!isCli && !serveStatic) { const webpackConfig = require(path.join(__dirname, "..", "..", "web", "webpack.config.js")); const compiler = require("webpack")(webpackConfig); app.use(require("webpack-dev-middleware")(compiler, { @@ -163,6 +164,6 @@ export class Entry extends Command { } Entry.run(undefined, { - root: process.env.BUILD_DIR as string || __dirname, + root: buildDir || __dirname, //@ts-ignore }).catch(require("@oclif/errors/handle")); diff --git a/packages/server/src/constants.ts b/packages/server/src/constants.ts new file mode 100644 index 00000000..dee4b267 --- /dev/null +++ b/packages/server/src/constants.ts @@ -0,0 +1,3 @@ +export const isCli = typeof process.env.CLI !== "undefined" && process.env.CLI !== "false"; +export const serveStatic = typeof process.env.SERVE_STATIC !== "undefined" && process.env.SERVE_STATIC !== "false"; +export const buildDir = process.env.BUILD_DIR; diff --git a/packages/server/src/fill.ts b/packages/server/src/fill.ts index 693013c7..94c373cd 100644 --- a/packages/server/src/fill.ts +++ b/packages/server/src/fill.ts @@ -1,5 +1,6 @@ import * as fs from "fs"; import * as util from "util"; +import { isCli } from "./constants"; const oldAccess = fs.access; const existsWithinBinary = (path: fs.PathLike): Promise => { @@ -24,7 +25,7 @@ export const fillFs = (): void => { * For impls */ - if (!process.env.CLI) { + if (!isCli) { throw new Error("Should not fill FS when not in CLI"); } diff --git a/packages/server/src/modules.ts b/packages/server/src/modules.ts index 0c063f89..d8af91d5 100644 --- a/packages/server/src/modules.ts +++ b/packages/server/src/modules.ts @@ -1,5 +1,6 @@ import * as fs from "fs"; import * as path from "path"; +import { isCli, buildDir } from "./constants"; declare var __non_webpack_require__: typeof require; @@ -7,7 +8,7 @@ declare var __non_webpack_require__: typeof require; * Handling of native modules within the CLI */ export const setup = (dataDirectory: string): void => { - if (!process.env.CLI) { + if (!isCli) { return; } @@ -20,7 +21,7 @@ export const setup = (dataDirectory: string): void => { } const unpackModule = (moduleName: string): void => { - const memFile = path.join(process.env.BUILD_DIR!, "build/modules", moduleName + ".node"); + const memFile = path.join(buildDir!, "build/modules", moduleName + ".node"); const diskFile = path.join(dataDirectory, "modules", moduleName + ".node"); if (!fs.existsSync(diskFile)) { fs.writeFileSync(diskFile, fs.readFileSync(memFile)); @@ -38,4 +39,4 @@ export const setup = (dataDirectory: string): void => { return __non_webpack_require__(path.join(dataDirectory, "modules", modName + ".node")); }; require("../../protocol/node_modules/node-pty/lib/index") as typeof import("../../protocol/node_modules/node-pty/src/index"); -}; \ No newline at end of file +}; diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index a508cc2f..2473bb42 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -13,6 +13,7 @@ import * as path from "path"; import * as util from "util"; import * as ws from "ws"; import { forkModule } from "./vscode/bootstrapFork"; +import { isCli, buildDir } from "./constants"; export const createApp = (registerMiddleware?: (app: express.Application) => void, options?: ServerOptions): { readonly express: express.Application; @@ -69,8 +70,8 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi } : undefined); }); - const baseDir = process.env.BUILD_DIR || path.join(__dirname, ".."); - if (process.env.CLI) { + const baseDir = buildDir || path.join(__dirname, ".."); + if (isCli) { app.use(expressStaticGzip(path.join(baseDir, "build/web"))); } else { app.use(express.static(path.join(baseDir, "resources/web"))); @@ -84,16 +85,14 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi // } const exists = fs.existsSync(fullPath); if (!exists) { - res.status(404).end(); - return; + return res.status(404).end(); } const stat = await util.promisify(fs.stat)(fullPath); if (!stat.isFile()) { res.write("Resource must be a file."); res.status(422); - res.end(); - return; + return res.end(); } let mimeType = mime.lookup(fullPath); if (mimeType === false) { diff --git a/packages/server/src/vscode/bootstrapFork.ts b/packages/server/src/vscode/bootstrapFork.ts index 2a05dacb..a92abb95 100644 --- a/packages/server/src/vscode/bootstrapFork.ts +++ b/packages/server/src/vscode/bootstrapFork.ts @@ -3,6 +3,7 @@ import * as fs from "fs"; import * as path from "path"; import * as zlib from "zlib"; import * as vm from "vm"; +import { isCli } from "../constants"; export const requireModule = (modulePath: string, builtInExtensionsDir: string): void => { process.env.AMD_ENTRYPOINT = modulePath; @@ -37,7 +38,7 @@ export const requireModule = (modulePath: string, builtInExtensionsDir: string): const readFile = (name: string): Buffer => { return fs.readFileSync(path.join(process.env.BUILD_DIR as string || path.join(__dirname, "../.."), "./build", name)); }; - if (process.env.CLI) { + if (isCli) { content = zlib.gunzipSync(readFile("bootstrap-fork.js.gz")); } else { content = readFile("../resources/bootstrap-fork.js"); @@ -63,7 +64,7 @@ export const forkModule = (modulePath: string, env?: NodeJS.ProcessEnv): cp.Chil const options: cp.SpawnOptions = { stdio: [null, null, null, "ipc"], }; - if (process.env.CLI === "true") { + if (isCli) { proc = cp.execFile(process.execPath, args, options); } else { proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...args], options);