Archived
1
0

Replace fs-extra with fs.promises

Remove the Mac directory copy instead of refactoring it since we've had
this for a long time now and I think it's safe to assume that users
running code-server on Mac don't have the old directory anymore.
This commit is contained in:
Asher
2021-03-15 16:15:24 -05:00
parent 3ef7cc7d03
commit 964ebe8d0a
9 changed files with 37 additions and 78 deletions

View File

@ -1,5 +1,5 @@
import { field, Level, logger } from "@coder/logger"
import * as fs from "fs-extra"
import { promises as fs } from "fs"
import yaml from "js-yaml"
import * as os from "os"
import * as path from "path"
@ -385,7 +385,6 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
const args = Object.assign({}, configArgs || {}, cliArgs)
if (!args["user-data-dir"]) {
await copyOldMacOSDataDir()
args["user-data-dir"] = paths.data
}
@ -510,13 +509,22 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
}
}
if (!(await fs.pathExists(configPath))) {
await fs.outputFile(configPath, await defaultConfigFile())
await fs.mkdir(path.dirname(configPath), { recursive: true })
try {
await fs.writeFile(configPath, await defaultConfigFile(), {
flag: "wx", // wx means to fail if the path exists.
})
logger.info(`Wrote default config file to ${humanPath(configPath)}`)
} catch (error) {
// EEXIST is fine; we don't want to overwrite existing configurations.
if (error.code !== "EEXIST") {
throw error
}
}
const configFile = await fs.readFile(configPath)
return parseConfigFile(configFile.toString(), configPath)
const configFile = await fs.readFile(configPath, "utf8")
return parseConfigFile(configFile, configPath)
}
/**
@ -599,21 +607,6 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
return addr
}
async function copyOldMacOSDataDir(): Promise<void> {
if (os.platform() !== "darwin") {
return
}
if (await fs.pathExists(paths.data)) {
return
}
// If the old data directory exists, we copy it in.
const oldDataDir = path.join(os.homedir(), "Library/Application Support", "code-server")
if (await fs.pathExists(oldDataDir)) {
await fs.copy(oldDataDir, paths.data)
}
}
export const shouldRunVsCodeCli = (args: Args): boolean => {
return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"]
}

View File

@ -1,6 +1,6 @@
import { logger } from "@coder/logger"
import { Query } from "express-serve-static-core"
import * as fs from "fs-extra"
import { promises as fs } from "fs"
import * as path from "path"
import { paths } from "./util"

View File

@ -1,4 +1,4 @@
import * as fs from "fs-extra"
import { promises as fs } from "fs"
import * as net from "net"
import * as path from "path"
import * as tls from "tls"
@ -75,7 +75,7 @@ export class SocketProxyProvider {
this._proxyServer = this.findFreeSocketPath(this.proxyPipe)
.then((pipe) => {
this.proxyPipe = pipe
return Promise.all([fs.mkdirp(tmpdir), fs.remove(this.proxyPipe)])
return Promise.all([fs.mkdir(tmpdir, { recursive: true }), fs.rmdir(this.proxyPipe, { recursive: true })])
})
.then(() => {
return new Promise((resolve) => {

View File

@ -1,7 +1,7 @@
import * as cp from "child_process"
import * as crypto from "crypto"
import envPaths from "env-paths"
import * as fs from "fs-extra"
import { promises as fs } from "fs"
import * as net from "net"
import * as os from "os"
import * as path from "path"
@ -58,8 +58,11 @@ export const generateCertificate = async (hostname: string): Promise<{ cert: str
const certPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.crt`)
const certKeyPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.key`)
const checks = await Promise.all([fs.pathExists(certPath), fs.pathExists(certKeyPath)])
if (!checks[0] || !checks[1]) {
// Try generating the certificates if we can't access them (which probably
// means they don't exist).
try {
await Promise.all([fs.access(certPath), fs.access(certKeyPath)])
} catch (error) {
// Require on demand so openssl isn't required if you aren't going to
// generate certificates.
const pem = require("pem") as typeof import("pem")
@ -86,9 +89,10 @@ DNS.1 = ${hostname}
},
)
})
await fs.mkdirp(paths.data)
await fs.mkdir(paths.data, { recursive: true })
await Promise.all([fs.writeFile(certPath, certs.certificate), fs.writeFile(certKeyPath, certs.serviceKey)])
}
return {
cert: certPath,
certKey: certKeyPath,