diff --git a/extension.ts b/extension.ts index 71a3923425c..12e818372a0 100644 --- a/extension.ts +++ b/extension.ts @@ -98,6 +98,9 @@ export async function activate(context: vscode.ExtensionContext) { extensionContext = context; extensionContext.subscriptions.push(StatusBar); + // Set the storage path to be used by history files + Globals.extensionStoragePath = context.globalStoragePath; + if (vscode.window.activeTextEditor) { const filepathComponents = vscode.window.activeTextEditor.document.fileName.split(/\\|\//); Register.putByKey(filepathComponents[filepathComponents.length - 1], '%', undefined, true); diff --git a/package-lock.json b/package-lock.json index da93630ef00..83c2e0adbd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -252,11 +252,6 @@ "normalize-path": "^2.1.1" } }, - "appdirectory": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/appdirectory/-/appdirectory-0.1.0.tgz", - "integrity": "sha1-62yBYyDnsqsW9e2ZfyjYIF31Y3U=" - }, "append-buffer": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/append-buffer/-/append-buffer-1.0.2.tgz", diff --git a/package.json b/package.json index 0dbfa3d8a5f..bcfde3370f9 100644 --- a/package.json +++ b/package.json @@ -856,10 +856,8 @@ "postinstall": "node ./node_modules/vscode/bin/install" }, "dependencies": { - "appdirectory": "0.1.0", "diff-match-patch": "1.0.4", "lodash.escaperegexp": "4.1.2", - "mkdirp": "0.5.1", "neovim": "4.5.0", "untildify": "4.0.0", "winston": "3.2.1", diff --git a/src/globals.ts b/src/globals.ts index 7c03a638167..1bb74c23a5d 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -10,4 +10,6 @@ export class Globals { static mockModeHandler: ModeHandler; static mockConfiguration: IConfiguration; + + static extensionStoragePath: string; } diff --git a/src/history/historyFile.ts b/src/history/historyFile.ts index 31bad299e12..3977b19ff28 100644 --- a/src/history/historyFile.ts +++ b/src/history/historyFile.ts @@ -2,24 +2,20 @@ import * as fs from 'fs'; import * as path from 'path'; import { Logger } from '../util/logger'; import { configuration } from '../configuration/configuration'; -import { getExtensionDirPath } from '../util/util'; import { promisify } from 'util'; - -const mkdirp = require('mkdirp'); +import { Globals } from './../globals'; export class HistoryFile { private readonly _logger = Logger.get('HistoryFile'); private _historyFileName: string; - private _historyDir: string; private _history: string[] = []; public get historyFilePath(): string { - return path.join(this._historyDir, this._historyFileName); + return path.join(Globals.extensionStoragePath, this._historyFileName); } - constructor(historyFileName: string, historyDir?: string) { + constructor(historyFileName: string) { this._historyFileName = historyFileName; - this._historyDir = historyDir ? historyDir : getExtensionDirPath(); } public async add(value: string | undefined): Promise { @@ -69,9 +65,9 @@ export class HistoryFile { data = await promisify(fs.readFile)(this.historyFilePath, 'utf-8'); } catch (err) { if (err.code === 'ENOENT') { - this._logger.debug(`History does not exist. path=${this._historyDir}`); + this._logger.debug(`History does not exist. path=${this.historyFilePath}`); } else { - this._logger.warn(`Failed to load history. path=${this._historyDir} err=${err}.`); + this._logger.warn(`Failed to load history. path=${this.historyFilePath} err=${err}.`); } return; } @@ -87,15 +83,22 @@ export class HistoryFile { } this._history = parsedData; } catch (e) { - this._logger.warn(`Deleting corrupted history file. path=${this._historyDir} err=${e}.`); + this._logger.warn(`Deleting corrupted history file. path=${this.historyFilePath} err=${e}.`); this.clear(); } } private async save(): Promise { try { - // create supplied directory. if directory already exists, do nothing. - await promisify(mkdirp)(this._historyDir, 0o775); + // create supplied directory. if directory already exists, do nothing and move on + try { + await promisify(fs.mkdir)(Globals.extensionStoragePath, { recursive: true }); + } catch (createDirectoryErr) { + if (createDirectoryErr.code !== 'EEXIST') { + throw createDirectoryErr; + } + } + // create file await promisify(fs.writeFile)(this.historyFilePath, JSON.stringify(this._history), 'utf-8'); } catch (err) { @@ -106,13 +109,13 @@ export class HistoryFile { } export class SearchHistory extends HistoryFile { - constructor(historyFileDir?: string) { - super('.search_history', historyFileDir); + constructor() { + super('.search_history'); } } export class CommandLineHistory extends HistoryFile { - constructor(historyFileDir?: string) { - super('.cmdline_history', historyFileDir); + constructor() { + super('.cmdline_history'); } } diff --git a/src/state/globalState.ts b/src/state/globalState.ts index 7cdaa1efd43..9f5b4158234 100644 --- a/src/state/globalState.ts +++ b/src/state/globalState.ts @@ -7,7 +7,6 @@ import { SearchHistory } from '../history/historyFile'; import { SearchState, SearchDirection } from './searchState'; import { SubstituteState } from './substituteState'; import { configuration } from '../configuration/configuration'; -import { VimState } from './vimState'; /** * State which stores global state (across editors) diff --git a/src/util/util.ts b/src/util/util.ts index 89d5e60abab..e732ca374e0 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -1,5 +1,4 @@ import * as vscode from 'vscode'; -import AppDirectory = require('appdirectory'); import { Logger } from './logger'; import { Position } from '../common/motion/position'; import { Range } from '../common/motion/range'; @@ -38,15 +37,6 @@ export async function getCursorsAfterSync(timeoutInMilliseconds: number = 0): Pr ); } -export function getExtensionDirPath(): string { - const logger = Logger.get('getExtensionDirPath'); - const dirs = new AppDirectory('VSCodeVim'); - - logger.debug('VSCodeVim Cache Directory: ' + dirs.userCache()); - - return dirs.userCache(); -} - /** * This function execute a shell command and return the standard output as string. */ diff --git a/test/cmd_line/historyFile.test.ts b/test/cmd_line/historyFile.test.ts index e54fa1b0c62..30ab3afc71c 100644 --- a/test/cmd_line/historyFile.test.ts +++ b/test/cmd_line/historyFile.test.ts @@ -4,11 +4,11 @@ import * as os from 'os'; import { HistoryFile } from '../../src/history/historyFile'; import { assertEqual, setupWorkspace, cleanUpWorkspace, rndName } from '../testUtils'; import { configuration } from '../../src/configuration/configuration'; +import { Globals } from '../../src/globals'; suite('HistoryFile', () => { let history: HistoryFile; let run_cmds: string[]; - const tmpDir = os.tmpdir(); const assertArrayEquals = (expected: any[], actual: any[]) => { assertEqual(expected.length, actual.length); @@ -25,7 +25,8 @@ suite('HistoryFile', () => { run_cmds.push(i.toString()); } - history = new HistoryFile(rndName(), tmpDir); + Globals.extensionStoragePath = os.tmpdir(); + history = new HistoryFile(rndName()); await history.load(); });