Skip to content

Commit

Permalink
Merge pull request #67 from coc-extensions/refactor_coc_utils
Browse files Browse the repository at this point in the history
Refactor coc utils
  • Loading branch information
Yatao Li authored Sep 17, 2019
2 parents c173c37 + 3c8b513 commit 408e1ab
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 119 deletions.
206 changes: 174 additions & 32 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@
"watch": "tsc -watch -p ./"
},
"extensionDependencies": [],
"dependencies": {},
"dependencies": {
"coc-utils": "0.0.8"
},
"devDependencies": {
"@types/node": "^10.3.3",
"typescript": "^3.5.2",
Expand Down
4 changes: 2 additions & 2 deletions src/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import * as net from 'net';
import { commands, workspace, ExtensionContext, events } from 'coc.nvim';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, StreamInfo } from 'coc.nvim';
import { fileURLToPath } from './utils'
import { getDefaultPowerShellPath, getPlatformDetails } from './platform';
import { fileURLToPath, getPlatformDetails } from 'coc-utils'
import { getDefaultPowerShellPath } from './platform';
import settings = require("./settings");
import * as process from './process';
import { EvaluateRequestMessage, IEvaluateRequestArguments } from "./messages";
Expand Down
34 changes: 1 addition & 33 deletions src/client/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs = require("fs");
import path = require("path");
import process = require("process");
import Settings = require("./settings");
import { IPlatformDetails, OperatingSystem } from 'coc-utils'

const linuxExePath = "/usr/bin/pwsh";
const linuxPreviewExePath = "/usr/bin/pwsh-preview";
Expand All @@ -16,44 +17,11 @@ const snapPreviewExePath = "/snap/bin/pwsh-preview";
const macOSExePath = "/usr/local/bin/pwsh";
const macOSPreviewExePath = "/usr/local/bin/pwsh-preview";

export enum OperatingSystem {
Unknown,
Windows,
MacOS,
Linux,
}

export interface IPlatformDetails {
operatingSystem: OperatingSystem;
isOS64Bit: boolean;
isProcess64Bit: boolean;
}

export interface IPowerShellExeDetails {
versionName: string;
exePath: string;
}

export function getPlatformDetails(): IPlatformDetails {
let operatingSystem = OperatingSystem.Unknown;

if (process.platform === "win32") {
operatingSystem = OperatingSystem.Windows;
} else if (process.platform === "darwin") {
operatingSystem = OperatingSystem.MacOS;
} else if (process.platform === "linux") {
operatingSystem = OperatingSystem.Linux;
}

const isProcess64Bit = process.arch === "x64";

return {
operatingSystem,
isOS64Bit: isProcess64Bit || process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432"),
isProcess64Bit,
};
}

/**
* Gets the default instance of PowerShell for the specified platform.
* On Windows, the default version of PowerShell is "Windows PowerShell".
Expand Down
3 changes: 2 additions & 1 deletion src/client/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import vscode = require("coc.nvim");
import Settings = require("./settings");
import utils = require("./utils");
import crypto = require("crypto");
import { isWindowsOS } from 'coc-utils'

export class PowerShellProcess {
public static escapeSingleQuotes(pspath: string): string {
Expand Down Expand Up @@ -57,7 +58,7 @@ export class PowerShellProcess {
let powerShellArgs: string[] = []

// Only add ExecutionPolicy param on Windows
if (utils.isWindowsOS()) {
if (isWindowsOS()) {
powerShellArgs.push("-ExecutionPolicy", "Bypass");
}

Expand Down
3 changes: 2 additions & 1 deletion src/client/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import vscode = require("coc.nvim");
import utils = require("./utils");
import { isWindowsOS } from 'coc-utils'

enum CodeFormattingPreset {
Custom,
Expand Down Expand Up @@ -118,7 +119,7 @@ export function load(): ISettings {

// TODO: Remove when PSReadLine is out of preview
const featureFlags = [];
if (utils.isWindowsOS()) {
if (isWindowsOS()) {
featureFlags.push("PSReadLine");
}

Expand Down
50 changes: 1 addition & 49 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,11 @@
"use strict";

import fs = require("fs");
import os = require("os");
import path = require("path");
import { Uri } from 'coc.nvim'
import { ensurePathExists, checkIfFileExists } from 'coc-utils'

export let PowerShellLanguageId = "powershell";

export function fileURLToPath(x: string) {
return Uri.parse(x).fsPath
}

export function sleep(ms: number) {
return new Promise((resolve, __) => setTimeout(resolve, ms))
}

export function ensurePathExists(targetPath: string) {
// Ensure that the path exists
try {
fs.mkdirSync(targetPath);
} catch (e) {
// If the exception isn't to indicate that the folder exists already, rethrow it.
if (e.code !== "EEXIST") {
throw e;
}
}
}

export function getPipePath(pipeName: string) {
if (os.platform() === "win32") {
return "\\\\.\\pipe\\" + pipeName;
} else {
// Windows uses NamedPipes where non-Windows platforms use Unix Domain Sockets.
// This requires connecting to the pipe file in different locations on Windows vs non-Windows.
return path.join(os.tmpdir(), `CoreFxPipe_${pipeName}`);
}
}

export interface IEditorServicesSessionDetails {
status: string;
reason: string;
Expand Down Expand Up @@ -111,20 +80,3 @@ export function deleteSessionFile(sessionFilePath: string) {
}
}

export function checkIfFileExists(filePath: string): boolean {
try {
fs.accessSync(filePath, fs.constants.R_OK);
return true;
} catch (e) {
return false;
}
}

export function getTimestampString() {
const time = new Date();
return `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}]`;
}

export function isWindowsOS(): boolean {
return os.platform() === "win32";
}

0 comments on commit 408e1ab

Please sign in to comment.