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

feat: added unstable settings option #167

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,16 @@ We recommend you using [dvm](https://github.com/axetroy/dvm) for the manager Den

- `deno.import_map` - The file paths of Import Map. Default is `null`

- `deno.unstable` - If Deno's unstable mode is enabled. Default is `false`

We recommend that you do not set global configuration. It should be configured in `.vscode/settings.json` in the project directory:

```json5
// .vscode/settings.json
{
"deno.enable": true,
"deno.import_map": "./path/to/import_map.json",
"deno.unstable": false,
}
```

Expand Down
3 changes: 2 additions & 1 deletion __test__/vscode_settings/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"deno.enable": true,
"deno.import_map": "./import_map.json"
"deno.import_map": "./import_map.json",
"deno.unstable": true
}
2 changes: 0 additions & 2 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type DenoInfo = {
raw: string;
};
executablePath: string;
dtsFilepath: string;
};

// get typescript api from build-in extension
Expand Down Expand Up @@ -118,7 +117,6 @@ export class Extension {
raw: "",
},
executablePath: "",
dtsFilepath: "",
};
// get configuration of Deno
public getConfiguration(uri?: Uri): SynchronizedConfiguration {
Expand Down
5 changes: 4 additions & 1 deletion core/deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ test("core / deno", () => {
});

test("core / deno / getDenoDts()", () => {
expect(getDenoDts()).toBe(path.join(getDenoDir(), "lib.deno_runtime.d.ts"));
expect(getDenoDts(false)).toBe(path.join(getDenoDir(), "lib.deno.d.ts"));
expect(getDenoDts(true)).toBe(
path.join(getDenoDir(), "lib.deno.unstable.d.ts")
);
});

test("core / deno / URL2filepath()", () => {
Expand Down
7 changes: 5 additions & 2 deletions core/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export function getDenoDepsDir(): string {
return path.join(getDenoDir(), "deps");
}

export function getDenoDts(): string {
return path.join(getDenoDir(), "lib.deno_runtime.d.ts");
export function getDenoDts(unstable: boolean): string {
return path.join(
getDenoDir(),
unstable ? "lib.deno.unstable.d.ts" : "lib.deno.d.ts"
);
}

export function isInDeno(filepath: string): boolean {
Expand Down
2 changes: 2 additions & 0 deletions core/vscode_settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test("core / vscode_settings", async () => {
expect(config).toEqual({
enable: true,
import_map: "./import_map.json",
unstable: true,
} as DenoPluginConfig);

expect(readConfigurationFromVscodeSettings("./file/not/exist")).toEqual(
Expand All @@ -28,5 +29,6 @@ test("core / vscode_settings if it empty", async () => {
expect(config).toEqual({
enable: false,
import_map: undefined,
unstable: false,
} as DenoPluginConfig);
});
3 changes: 3 additions & 0 deletions core/vscode_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { pathExistsSync } from "./util";
export type DenoPluginConfig = {
enable: boolean;
import_map?: string;
unstable: boolean;
};

export function readConfigurationFromVscodeSettings(
Expand All @@ -28,10 +29,12 @@ export function readConfigurationFromVscodeSettings(

const isEnable = !!settings["deno.enable"];
const import_map = settings["deno.import_map"] || undefined;
const unstable = !!settings["deno.unstable"];

const configurationInProjectFolder: DenoPluginConfig = {
enable: isEnable,
import_map,
unstable,
};

return configurationInProjectFolder;
Expand Down
1 change: 1 addition & 0 deletions package.de-de.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"deno.command.restart_server": "Starte den Deno Language Server neu",
"deno.config.enabled": "Steuert, ob Deno aktiviert ist oder nicht.\n\n**In der globalen Konfiguration nicht empfohlen**",
"deno.config.import_map": "Der Dateipfad einer import map.\n\nDies kann ein Pfad sein, der relativ zum Projektpfad ist, oder ein absoluter Pfad.\n\n**In der globalen Konfiguration nicht empfohlen**",
"deno.config.unstable": "Controls if Deno's unstable mode types are used instead of stable mode types.\n\n**Not recommended in global configuration**",
"deno.initializing": "Deno Language Server wird initialisiert ...",
"err.not_install_deno": "Konnte `deno` in Ihrem $PATH nicht finden. Bitte installieren Sie `deno` und starten Sie die Erweiterung neu.",
"err.below_deno_minimum_requirements": "Deno's version is below minimum requirements. Please upgrade to v{0} or above.",
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
"./import_map.json",
"/absolute/path/to/import_map.json"
]
},
"deno.unstable": {
"type": "boolean",
"default": false,
"markdownDescription": "%deno.config.unstable%",
"scope": "resource",
"examples": [
true,
false
]
}
}
},
Expand Down
1 change: 1 addition & 0 deletions package.nl-nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"deno.command.restart_server": "Start Deno Language Server opnieuw",
"deno.config.enabled": "Bepaalt of Deno is ingeschakeld of niet.\n\n**Niet aanbevolen in globale configuratie**",
"deno.config.import_map": "Het bestandspad van een 'import map'.\n\nHet kan een pad zijn dat relatief is aan het projectpad, of een absoluut pad\n\n**Niet aanbevolen in globale configuratie**",
"deno.config.unstable": "Controls if Deno's unstable mode types are used instead of stable mode types.\n\n**Not recommended in global configuration**",
"deno.initializing": "Deno Language Server initialiseren ...",
"err.not_install_deno": "Kon `deno` niet vinden in uw $PATH. Installeer `deno` en start de extensie opnieuw.",
"err.below_deno_minimum_requirements": "Deno's version is below minimum requirements. Please upgrade to v{0} or above.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"deno.command.restart_server": "Restart Deno Language Server",
"deno.config.enabled": "Controls whether Deno is enabled or not.\n\n**Not recommended in global configuration**",
"deno.config.import_map": "The file path of an import map.\n\nIt can be a path that is relative to the project root, or an absolute path.\n\n**Not recommended in global configuration**",
"deno.config.unstable": "Controls if Deno's unstable mode types are used instead of stable mode types.\n\n**Not recommended in global configuration**",
"deno.initializing": "Initializing Deno Language Server...",
"err.not_install_deno": "Could not find `deno` in your $PATH. Please install `deno`, then restart the extension.",
"err.below_deno_minimum_requirements": "Deno's version is below minimum requirements. Please upgrade to v{0} or above.",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"deno.command.restart_server": "重启 Deno 语言服务器",
"deno.config.enabled": "是否启用 Deno。\n\n**不推荐在全局配置中设置**",
"deno.config.import_map": "Import Map 的文件路径。\n\n它可以是相对于项目目录的相对路径或者是绝对路径。\n\n**不推荐在全局配置中设置**",
"deno.config.unstable": "Controls if Deno's unstable mode types are used instead of stable mode types.\n\n**Not recommended in global configuration**",
"deno.initializing": "正在初始化 Deno 语言服务器...",
"err.not_install_deno": "在 $PATH 变量中找不到 Deno。请在设置后重新启动扩展。",
"err.below_deno_minimum_requirements": "Deno 的版本低于最小要求。 请升级至 v{0} 或以上。",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"deno.command.restart_server": "重啟 Deno 語言服務器",
"deno.config.enabled": "是否啟用 Deno。\n\n**不推薦在全局配置中設置**",
"deno.config.import_map": "Import Map 的文件路徑。\n\n它可以是相對於項目目錄的相對路徑或者是絕對路徑。\n\n**不推薦在全局配置中設置**",
"deno.config.unstable": "Controls if Deno's unstable mode types are used instead of stable mode types.\n\n**Not recommended in global configuration**",
"deno.initializing": "正在初始化 Deno 語言服務器...",
"err.not_install_deno": "在 $PATH 變量中找不到 Deno。請在設置後重新啟動擴展。",
"err.below_deno_minimum_requirements": "Deno 的版本低於最小要求。請升級至 v{0} 或以上。",
Expand Down
9 changes: 7 additions & 2 deletions server/src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ class Deno {
);
}
}
public async getTypes(): Promise<Buffer> {
const { stdout } = await execa(this.executablePath as string, ["types"]);
public async getTypes(unstable: boolean): Promise<Buffer> {
const { stdout } = await execa(this.executablePath as string, [
"types",
...(unstable && this.version && semver.gte(this.version.deno, "0.43.0")
? ["--unstable"]
: []),
]);

return Buffer.from(stdout, "utf8");
}
Expand Down
49 changes: 26 additions & 23 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,35 @@ connection.onInitialize(
}
);

async function ensureDenoDts(unstable: boolean) {
const currentDenoTypesContent = await deno.getTypes(unstable);
const denoDtsFile = getDenoDts(unstable);
const isExistDtsFile = await pathExists(denoDtsFile);

// if dst file not exist. then create a new one
if (!isExistDtsFile) {
await fs.writeFile(denoDtsFile, currentDenoTypesContent, { mode: 0o444 });
} else {
// set it to writable
await fs.chmod(denoDtsFile, 0o666);

const typesContent = await fs.readFile(denoDtsFile, { encoding: "utf8" });

if (typesContent.toString() !== currentDenoTypesContent.toString()) {
await fs.writeFile(denoDtsFile, currentDenoTypesContent, {
mode: 0o444,
});

// set to readonly
await fs.chmod(denoDtsFile, 0o444);
}
}
}

connection.onInitialized(async () => {
try {
await deno.init();
const currentDenoTypesContent = await deno.getTypes();
const denoDtsFile = getDenoDts();
const isExistDtsFile = await pathExists(denoDtsFile);

// if dst file not exist. then create a new one
if (!isExistDtsFile) {
await fs.writeFile(denoDtsFile, currentDenoTypesContent, { mode: 0o444 });
} else {
// set it to writable
await fs.chmod(denoDtsFile, 0o666);

const typesContent = await fs.readFile(denoDtsFile, { encoding: "utf8" });

if (typesContent.toString() !== currentDenoTypesContent.toString()) {
await fs.writeFile(denoDtsFile, currentDenoTypesContent, {
mode: 0o444,
});

// set to readonly
await fs.chmod(denoDtsFile, 0o444);
}
}
await Promise.all([ensureDenoDts(false), ensureDenoDts(true)]);
} catch (err) {
connection.sendNotification(Notification.error, err.message);
return;
Expand All @@ -116,7 +120,6 @@ connection.onInitialized(async () => {
version: deno.version ? deno.version : undefined,
executablePath: deno.executablePath,
DENO_DIR: getDenoDir(),
dtsFilepath: getDenoDts(),
});
connection.console.log("server initialized.");
});
Expand Down
3 changes: 3 additions & 0 deletions typescript-deno-plugin/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import equal from "deep-equal";
export type DenoPluginConfig = {
enable: boolean;
import_map: string;
unstable: boolean;
};

type UpdateDenoPluginConfig = {
enable?: boolean;
import_map?: string;
unstable?: boolean;
};

export class ConfigurationManager {
private static readonly defaultConfiguration: DenoPluginConfig = {
enable: false,
import_map: "",
unstable: false,
};

private readonly _configUpdatedListeners = new Set<() => void>();
Expand Down
2 changes: 1 addition & 1 deletion typescript-deno-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class DenoPlugin implements ts_module.server.PluginModule {
}

// Get typescript declaration File
const dtsFiles = [getDenoDts()];
const dtsFiles = [getDenoDts(this.configurationManager.config.unstable)];

const iterator = new Set(dtsFiles).entries();

Expand Down