Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
refactor: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jan 30, 2020
1 parent 749e181 commit a9760fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
15 changes: 5 additions & 10 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ interface DenoInfo {
DENO_DIR: string;
version: string;
executablePath: string;
dtsFilepath: string;
}

let denoInfo: DenoInfo = {
DENO_DIR: "",
version: "",
executablePath: ""
executablePath: "",
dtsFilepath: ""
};

const config: SynchronizedConfiguration = {
Expand Down Expand Up @@ -167,9 +169,9 @@ function synchronizeConfiguration(api: TypescriptAPI) {
const config = getConfiguration();

if (!config.dtsFilepaths) {
const dtsFilepath = getDenoDtsFilepath();
const dtsFilepath = denoInfo.dtsFilepath;
if (dtsFilepath) {
config.dtsFilepaths = [getDenoDtsFilepath()];
config.dtsFilepaths = [dtsFilepath];
}
}

Expand Down Expand Up @@ -216,13 +218,6 @@ function withConfigValue<C, K extends Extract<keyof C, string>>(
}
}

function getDenoDtsFilepath(): string {
if (!denoInfo.DENO_DIR) {
return "";
}
return path.join(denoInfo.DENO_DIR, "lib.deno_runtime.d.ts");
}

// get typescript api from build-in extension
// https://github.com/microsoft/vscode/blob/master/extensions/typescript-language-features/src/api.ts
async function getTypescriptAPI(): Promise<TypescriptAPI> {
Expand Down
4 changes: 3 additions & 1 deletion server/src/deno.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from "stream";
import execa from "execa";
import which from "which";
import { join } from "path";

interface Version {
deno: string;
Expand All @@ -26,7 +27,8 @@ interface FormatOptions {
class Deno {
public version!: Version | void;
public executablePath!: string | void;
public DENO_DIR = this.getDenoDir();
public readonly DENO_DIR = this.getDenoDir();
public readonly dtsFilepath = join(this.DENO_DIR, "lib.deno_runtime.d.ts");
constructor() {}
public async init() {
this.executablePath = await this.getExecutablePath();
Expand Down
33 changes: 16 additions & 17 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,29 @@ connection.onInitialize(
}
);

function getDenoDtsFilepath(): string {
return path.join(deno.DENO_DIR, "lib.deno_runtime.d.ts");
}

connection.onInitialized(async () => {
try {
await deno.init();
const currentDenoTypesContent = await deno.getTypes();
const typeFilepath = getDenoDtsFilepath();
const isExistDtsFile = await isFilepathExist(typeFilepath);
const isExistDtsFile = await isFilepathExist(deno.dtsFilepath);
const fileOptions = { encoding: "utf8" };

// if dst file not exist. then create a new one
if (!isExistDtsFile) {
await fs.writeFile(typeFilepath, currentDenoTypesContent, {
encoding: "utf8"
});
await fs.writeFile(
deno.dtsFilepath,
currentDenoTypesContent,
fileOptions
);
} else {
const typesContent = await fs.readFile(typeFilepath, {
encoding: "utf8"
});
const typesContent = await fs.readFile(deno.dtsFilepath, fileOptions);

if (typesContent.toString() !== currentDenoTypesContent.toString()) {
await fs.writeFile(typeFilepath, currentDenoTypesContent, {
encoding: "utf8"
});
await fs.writeFile(
deno.dtsFilepath,
currentDenoTypesContent,
fileOptions
);
}
}
} catch (err) {
Expand All @@ -95,9 +93,10 @@ connection.onInitialized(async () => {
connection.sendNotification("init", {
version: deno.version ? deno.version.deno : undefined,
executablePath: deno.executablePath,
DENO_DIR: deno.DENO_DIR
DENO_DIR: deno.DENO_DIR,
dtsFilepath: deno.dtsFilepath
});
connection.console.log("server start");
connection.console.log("server initialized.");
});

connection.onDocumentFormatting(async params => {
Expand Down

0 comments on commit a9760fe

Please sign in to comment.