From f7c1ebf66716e79d595ab921e20f1871be41a348 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 12 Mar 2019 15:45:44 -0500 Subject: [PATCH] Fix moving data directory across devices --- packages/protocol/src/common/util.ts | 19 ------------------- packages/protocol/src/node/server.ts | 8 ++++---- packages/server/src/cli.ts | 11 +++++------ packages/server/src/server.ts | 5 +++-- packages/vscode/src/fill/storageDatabase.ts | 4 ++-- 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/packages/protocol/src/common/util.ts b/packages/protocol/src/common/util.ts index 983376c6..45ff225a 100644 --- a/packages/protocol/src/common/util.ts +++ b/packages/protocol/src/common/util.ts @@ -97,22 +97,3 @@ export const parse = (arg: string): any => { // tslint:disable-line no-any return arg ? convert(JSON.parse(arg)) : arg; }; - -export const mkdirP = async (path: string): Promise => { - // Since our fills require this file, we can't import them up top or we get - // circular dependency issue. - const { mkdir } = require("fs") as typeof import("fs"); - const { promisify } = require("util") as typeof import("util"); - const split = path.replace(/^\/*|\/*$/g, "").split("/"); - let dir = ""; - while (split.length > 0) { - dir += "/" + split.shift(); - try { - await promisify(mkdir)(dir); - } catch (error) { - if (error.code !== "EEXIST" && error.code !== "EISDIR") { - throw error; - } - } - } -}; diff --git a/packages/protocol/src/node/server.ts b/packages/protocol/src/node/server.ts index fa36129c..0afebbaf 100644 --- a/packages/protocol/src/node/server.ts +++ b/packages/protocol/src/node/server.ts @@ -1,10 +1,10 @@ +import { mkdirp } from "fs-extra"; import * as os from "os"; import { logger, field } from "@coder/logger"; import { Pong, ClientMessage, WorkingInitMessage, ServerMessage } from "../proto"; import { evaluate, ActiveEvaluation } from "./evaluate"; import { ForkProvider } from "../common/helpers"; import { ReadWriteConnection } from "../common/connection"; -import { mkdirP } from "../common/util"; export interface ServerOptions { readonly workingDirectory: string; @@ -42,9 +42,9 @@ export class Server { } Promise.all([ - mkdirP(this.options.cacheDirectory), - mkdirP(this.options.dataDirectory), - mkdirP(this.options.workingDirectory), + mkdirp(this.options.cacheDirectory), + mkdirp(this.options.dataDirectory), + mkdirp(this.options.workingDirectory), ]).catch((error) => { logger.error(error.message, field("error", error)); }); diff --git a/packages/server/src/cli.ts b/packages/server/src/cli.ts index 10f19dc4..f8820628 100644 --- a/packages/server/src/cli.ts +++ b/packages/server/src/cli.ts @@ -1,5 +1,5 @@ +import * as fse from "fs-extra"; import { field, logger } from "@coder/logger"; -import { mkdirP } from "@coder/protocol"; import { ServerMessage, SharedProcessActiveMessage } from "@coder/protocol/src/proto"; import { Command, flags } from "@oclif/command"; import { fork, ForkOptions, ChildProcess } from "child_process"; @@ -55,16 +55,15 @@ export class Entry extends Command { if (!fs.existsSync(dataDir)) { const oldDataDir = path.resolve(path.join(os.homedir(), ".code-server")); if (fs.existsSync(oldDataDir)) { - await mkdirP(path.basename(dataDir)); - fs.renameSync(oldDataDir, dataDir); + await fse.move(oldDataDir, dataDir); logger.info(`Moved data directory from ${oldDataDir} to ${dataDir}`); } } await Promise.all([ - mkdirP(cacheHome), - mkdirP(dataDir), - mkdirP(workingDir), + fse.mkdirp(cacheHome), + fse.mkdirp(dataDir), + fse.mkdirp(workingDir), ]); setupNativeModules(dataDir); diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index 48873bbd..d4f45562 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -1,5 +1,6 @@ +import { mkdirp } from "fs-extra"; import { logger, field } from "@coder/logger"; -import { mkdirP, ReadWriteConnection } from "@coder/protocol"; +import { ReadWriteConnection } from "@coder/protocol"; import { Server, ServerOptions } from "@coder/protocol/src/node/server"; import * as express from "express"; //@ts-ignore @@ -259,7 +260,7 @@ export const createApp = async (options: CreateAppOptions): Promise<{ }); req.on("end", async () => { const body = data.join(""); - await mkdirP(path.dirname(fullPath)); + await mkdirp(path.dirname(fullPath)); fs.writeFileSync(fullPath, body); logger.debug("Wrote resource", field("path", fullPath), field("content-length", body.length)); res.status(200); diff --git a/packages/vscode/src/fill/storageDatabase.ts b/packages/vscode/src/fill/storageDatabase.ts index de69d0ac..54709a7a 100644 --- a/packages/vscode/src/fill/storageDatabase.ts +++ b/packages/vscode/src/fill/storageDatabase.ts @@ -1,9 +1,9 @@ import { readFile, writeFile } from "fs"; +import { mkdirp } from "fs-extra"; import * as path from "path"; import { promisify } from "util"; import { IDisposable } from "@coder/disposable"; import { logger, field } from "@coder/logger"; -import { mkdirP } from "@coder/protocol"; import { Event } from "vs/base/common/event"; import * as workspaceStorage from "vs/base/node/storage"; import * as globalStorage from "vs/platform/storage/node/storageIpc"; @@ -78,7 +78,7 @@ class StorageDatabase implements workspaceStorage.IStorageDatabase { } private async save(): Promise { - await mkdirP(path.dirname(this.path)); + await mkdirp(path.dirname(this.path)); return promisify(writeFile)(this.path, this.content); }