Skip to content

Commit 95a0740

Browse files
authored
Merge pull request #2889 from code-asher/remove-fs-extra
2 parents 556e562 + 964ebe8 commit 95a0740

File tree

9 files changed

+41
-82
lines changed

9 files changed

+41
-82
lines changed

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"_____": "",
2626
"fmt": "./ci/dev/fmt.sh",
2727
"lint": "./ci/dev/lint.sh",
28-
"test": "./ci/dev/test.sh",
28+
"test": "echo 'Run yarn test:unit or yarn test:e2e' && exit 1",
2929
"ci": "./ci/dev/ci.sh",
3030
"watch": "VSCODE_IPC_HOOK_CLI= NODE_OPTIONS=--max_old_space_size=32384 ts-node ./ci/dev/watch.ts",
3131
"icons": "./ci/dev/gen_icons.sh",
@@ -38,7 +38,6 @@
3838
"@types/compression": "^1.7.0",
3939
"@types/cookie-parser": "^1.4.2",
4040
"@types/express": "^4.17.8",
41-
"@types/fs-extra": "^8.0.1",
4241
"@types/http-proxy": "^1.17.4",
4342
"@types/js-yaml": "^3.12.3",
4443
"@types/node": "^12.12.7",
@@ -84,7 +83,6 @@
8483
"cookie-parser": "^1.4.5",
8584
"env-paths": "^2.2.0",
8685
"express": "^5.0.0-alpha.8",
87-
"fs-extra": "^9.0.1",
8886
"http-proxy": "^1.18.0",
8987
"httpolyglot": "^0.1.2",
9088
"js-yaml": "^3.13.1",

src/node/cli.ts

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { field, Level, logger } from "@coder/logger"
2-
import * as fs from "fs-extra"
2+
import { promises as fs } from "fs"
33
import yaml from "js-yaml"
44
import * as os from "os"
55
import * as path from "path"
@@ -385,7 +385,6 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
385385
const args = Object.assign({}, configArgs || {}, cliArgs)
386386

387387
if (!args["user-data-dir"]) {
388-
await copyOldMacOSDataDir()
389388
args["user-data-dir"] = paths.data
390389
}
391390

@@ -510,13 +509,22 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
510509
}
511510
}
512511

513-
if (!(await fs.pathExists(configPath))) {
514-
await fs.outputFile(configPath, await defaultConfigFile())
512+
await fs.mkdir(path.dirname(configPath), { recursive: true })
513+
514+
try {
515+
await fs.writeFile(configPath, await defaultConfigFile(), {
516+
flag: "wx", // wx means to fail if the path exists.
517+
})
515518
logger.info(`Wrote default config file to ${humanPath(configPath)}`)
519+
} catch (error) {
520+
// EEXIST is fine; we don't want to overwrite existing configurations.
521+
if (error.code !== "EEXIST") {
522+
throw error
523+
}
516524
}
517525

518-
const configFile = await fs.readFile(configPath)
519-
return parseConfigFile(configFile.toString(), configPath)
526+
const configFile = await fs.readFile(configPath, "utf8")
527+
return parseConfigFile(configFile, configPath)
520528
}
521529

522530
/**
@@ -599,21 +607,6 @@ function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
599607
return addr
600608
}
601609

602-
async function copyOldMacOSDataDir(): Promise<void> {
603-
if (os.platform() !== "darwin") {
604-
return
605-
}
606-
if (await fs.pathExists(paths.data)) {
607-
return
608-
}
609-
610-
// If the old data directory exists, we copy it in.
611-
const oldDataDir = path.join(os.homedir(), "Library/Application Support", "code-server")
612-
if (await fs.pathExists(oldDataDir)) {
613-
await fs.copy(oldDataDir, paths.data)
614-
}
615-
}
616-
617610
export const shouldRunVsCodeCli = (args: Args): boolean => {
618611
return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"]
619612
}

src/node/settings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from "@coder/logger"
22
import { Query } from "express-serve-static-core"
3-
import * as fs from "fs-extra"
3+
import { promises as fs } from "fs"
44
import * as path from "path"
55
import { paths } from "./util"
66

src/node/socket.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from "fs-extra"
1+
import { promises as fs } from "fs"
22
import * as net from "net"
33
import * as path from "path"
44
import * as tls from "tls"
@@ -75,7 +75,7 @@ export class SocketProxyProvider {
7575
this._proxyServer = this.findFreeSocketPath(this.proxyPipe)
7676
.then((pipe) => {
7777
this.proxyPipe = pipe
78-
return Promise.all([fs.mkdirp(tmpdir), fs.remove(this.proxyPipe)])
78+
return Promise.all([fs.mkdir(tmpdir, { recursive: true }), fs.rmdir(this.proxyPipe, { recursive: true })])
7979
})
8080
.then(() => {
8181
return new Promise((resolve) => {

src/node/util.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cp from "child_process"
22
import * as crypto from "crypto"
33
import envPaths from "env-paths"
4-
import * as fs from "fs-extra"
4+
import { promises as fs } from "fs"
55
import * as net from "net"
66
import * as os from "os"
77
import * as path from "path"
@@ -58,8 +58,11 @@ export const generateCertificate = async (hostname: string): Promise<{ cert: str
5858
const certPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.crt`)
5959
const certKeyPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.key`)
6060

61-
const checks = await Promise.all([fs.pathExists(certPath), fs.pathExists(certKeyPath)])
62-
if (!checks[0] || !checks[1]) {
61+
// Try generating the certificates if we can't access them (which probably
62+
// means they don't exist).
63+
try {
64+
await Promise.all([fs.access(certPath), fs.access(certKeyPath)])
65+
} catch (error) {
6366
// Require on demand so openssl isn't required if you aren't going to
6467
// generate certificates.
6568
const pem = require("pem") as typeof import("pem")
@@ -86,9 +89,10 @@ DNS.1 = ${hostname}
8689
},
8790
)
8891
})
89-
await fs.mkdirp(paths.data)
92+
await fs.mkdir(paths.data, { recursive: true })
9093
await Promise.all([fs.writeFile(certPath, certs.certificate), fs.writeFile(certKeyPath, certs.serviceKey)])
9194
}
95+
9296
return {
9397
cert: certPath,
9498
certKey: certKeyPath,

test/unit/cli.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Level, logger } from "@coder/logger"
2-
import * as fs from "fs-extra"
2+
import { promises as fs } from "fs"
33
import * as net from "net"
44
import * as os from "os"
55
import * as path from "path"
@@ -339,14 +339,14 @@ describe("cli", () => {
339339
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
340340

341341
beforeAll(async () => {
342-
await fs.remove(testDir)
343-
await fs.mkdirp(testDir)
342+
await fs.rmdir(testDir, { recursive: true })
343+
await fs.mkdir(testDir, { recursive: true })
344344
})
345345

346346
beforeEach(async () => {
347347
delete process.env.VSCODE_IPC_HOOK_CLI
348348
args = { _: [] }
349-
await fs.remove(vscodeIpcPath)
349+
await fs.rmdir(vscodeIpcPath, { recursive: true })
350350
})
351351

352352
it("should use existing if inside code-server", async () => {
@@ -360,7 +360,7 @@ describe("cli", () => {
360360

361361
it("should use existing if --reuse-window is set", async () => {
362362
args["reuse-window"] = true
363-
await expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
363+
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
364364

365365
await fs.writeFile(vscodeIpcPath, "test")
366366
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")

test/unit/socket.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { field, logger } from "@coder/logger"
2-
import * as fs from "fs-extra"
2+
import { promises as fs } from "fs"
33
import * as net from "net"
44
import * as path from "path"
55
import * as tls from "tls"
@@ -45,14 +45,14 @@ describe("SocketProxyProvider", () => {
4545
beforeAll(async () => {
4646
const cert = await generateCertificate("localhost")
4747
const options = {
48-
cert: fs.readFileSync(cert.cert),
49-
key: fs.readFileSync(cert.certKey),
48+
cert: await fs.readFile(cert.cert),
49+
key: await fs.readFile(cert.certKey),
5050
rejectUnauthorized: false,
5151
}
5252

53-
await fs.mkdirp(path.join(tmpdir, "tests"))
53+
await fs.mkdir(path.join(tmpdir, "tests"), { recursive: true })
5454
const socketPath = await provider.findFreeSocketPath(path.join(tmpdir, "tests/tls-socket-proxy"))
55-
await fs.remove(socketPath)
55+
await fs.rmdir(socketPath, { recursive: true })
5656

5757
return new Promise<void>((_resolve) => {
5858
const resolved: { [key: string]: boolean } = { client: false, server: false }
@@ -110,10 +110,10 @@ describe("SocketProxyProvider", () => {
110110
provider.stop() // We don't need more proxies.
111111

112112
proxy.write("server proxy->client")
113-
const dataFromServerToClient = await (await getData(fromServerToClient)).toString()
113+
const dataFromServerToClient = (await getData(fromServerToClient)).toString()
114114
expect(dataFromServerToClient).toBe("server proxy->client")
115115
client.write("client->server proxy")
116-
const dataFromClientToProxy = await (await getData(fromClientToProxy)).toString()
116+
const dataFromClientToProxy = (await getData(fromClientToProxy)).toString()
117117
expect(dataFromClientToProxy).toBe("client->server proxy")
118118
expect(errors).toEqual(0)
119119
})

test/unit/update.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from "fs-extra"
1+
import { promises as fs } from "fs"
22
import * as http from "http"
33
import * as path from "path"
44
import { SettingsProvider, UpdateSettings } from "../../src/node/settings"
@@ -53,8 +53,8 @@ describe.skip("update", () => {
5353
host: "localhost",
5454
})
5555
})
56-
await fs.remove(path.join(tmpdir, "tests/updates"))
57-
await fs.mkdirp(path.join(tmpdir, "tests/updates"))
56+
await fs.rmdir(path.join(tmpdir, "tests/updates"), { recursive: true })
57+
await fs.mkdir(path.join(tmpdir, "tests/updates"), { recursive: true })
5858
})
5959

6060
afterAll(() => {

yarn.lock

-36
Original file line numberDiff line numberDiff line change
@@ -1197,13 +1197,6 @@
11971197
"@types/qs" "*"
11981198
"@types/serve-static" "*"
11991199

1200-
"@types/fs-extra@^8.0.1":
1201-
version "8.1.1"
1202-
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-8.1.1.tgz#1e49f22d09aa46e19b51c0b013cb63d0d923a068"
1203-
integrity sha512-TcUlBem321DFQzBNuz8p0CLLKp0VvF/XH9E4KHNmgwyp4E3AfgI5cjiIVZWlbfThBop2qxFIh4+LeY6hVWWZ2w==
1204-
dependencies:
1205-
"@types/node" "*"
1206-
12071200
"@types/http-proxy@^1.17.4":
12081201
version "1.17.5"
12091202
resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.5.tgz#c203c5e6e9dc6820d27a40eb1e511c70a220423d"
@@ -1721,11 +1714,6 @@ asynckit@^0.4.0:
17211714
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
17221715
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=
17231716

1724-
at-least-node@^1.0.0:
1725-
version "1.0.0"
1726-
resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
1727-
integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
1728-
17291717
atob@^2.1.2:
17301718
version "2.1.2"
17311719
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
@@ -3726,16 +3714,6 @@ fs-extra@^8.1.0:
37263714
jsonfile "^4.0.0"
37273715
universalify "^0.1.0"
37283716

3729-
fs-extra@^9.0.1:
3730-
version "9.0.1"
3731-
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
3732-
integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
3733-
dependencies:
3734-
at-least-node "^1.0.0"
3735-
graceful-fs "^4.2.0"
3736-
jsonfile "^6.0.1"
3737-
universalify "^1.0.0"
3738-
37393717
fs-minipass@^2.0.0:
37403718
version "2.1.0"
37413719
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
@@ -4730,15 +4708,6 @@ jsonfile@^4.0.0:
47304708
optionalDependencies:
47314709
graceful-fs "^4.1.6"
47324710

4733-
jsonfile@^6.0.1:
4734-
version "6.0.1"
4735-
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
4736-
integrity sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==
4737-
dependencies:
4738-
universalify "^1.0.0"
4739-
optionalDependencies:
4740-
graceful-fs "^4.1.6"
4741-
47424711
jsprim@^1.2.2:
47434712
version "1.4.1"
47444713
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -7980,11 +7949,6 @@ universalify@^0.1.0:
79807949
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
79817950
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
79827951

7983-
universalify@^1.0.0:
7984-
version "1.0.0"
7985-
resolved "https://registry.yarnpkg.com/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d"
7986-
integrity sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==
7987-
79887952
[email protected], unpipe@~1.0.0:
79897953
version "1.0.0"
79907954
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"

0 commit comments

Comments
 (0)