Skip to content

Commit

Permalink
feat(server): save server to data path
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Jul 12, 2019
1 parent 505d222 commit 7c0a42e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
lib
node_modules/
server/*
!server/.gitkeep
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ tsconfig.json
webpack.config.js
yarn.lock
yarn-error.log
server/*
!server/.gitkeep
Empty file removed server/.gitkeep
Empty file.
17 changes: 8 additions & 9 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import unzipper from 'unzipper';
import tunnel from 'tunnel';
import got from 'got';

import { ExtensionContext, workspace } from 'coc.nvim';
import { workspace } from 'coc.nvim';
import { Agent } from 'http';

function getAgent(): Agent | undefined {
Expand All @@ -26,20 +26,20 @@ function getAgent(): Agent | undefined {
}
}

async function getLatestVersion(): Promise<string> {
let ver = '1.0.0';
async function getLatestVersionTag(): Promise<string> {
let tag = 'v1.0.0';
const apiURL = 'https://api.github.com/repos/latex-lsp/texlab/releases/latest';
try {
const agent = getAgent();
const resp = await got(apiURL, { agent });
ver = JSON.parse(resp.body).tag_name;
tag = JSON.parse(resp.body).tag_name;
} catch (_e) {}

return ver;
return tag;
}

export async function downloadServer(context: ExtensionContext): Promise<void> {
const ver = await getLatestVersion();
export async function downloadServer(root: string): Promise<void> {
const ver = await getLatestVersionTag();
const agent = getAgent();

const urls = {
Expand All @@ -48,8 +48,7 @@ export async function downloadServer(context: ExtensionContext): Promise<void> {
darwin: `https://github.com/latex-lsp/texlab/releases/download/${ver}/texlab-x86_64-macos.tar.gz`
};
const url = urls[os.platform()];
const path = context.asAbsolutePath('server');
const extract = os.platform() === 'win32' ? () => unzipper.Extract({ path }) : () => tar.x({ C: path });
const extract = os.platform() === 'win32' ? () => unzipper.Extract({ path: root }) : () => tar.x({ C: root });

let statusItem = workspace.createStatusBarItem(0, { progress: true });
statusItem.text = 'Downloading TexLab Server';
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import fs from 'fs';
import os from 'os';
import path from 'path';

import { commands, ServerOptions, services, ExtensionContext, workspace, LanguageClientOptions, LanguageClient } from 'coc.nvim';
import { downloadServer } from './downloader';

export async function activate(context: ExtensionContext): Promise<void> {
const serverPath = getServerPath(context);
const serverRoot = context.storagePath;
if (!fs.existsSync(serverRoot)) {
fs.mkdirSync(serverRoot);
}
const serverPath = getServerPath(serverRoot);
if (!fs.existsSync(serverPath)) {
workspace.showMessage(`TexLab Server is not found, downloading...`);
try {
await downloadServer(context);
await downloadServer(serverRoot);
workspace.showMessage(`Download TexLab Server success`);
} catch (_e) {
workspace.showMessage(`Download TexLab Server failed`);
Expand All @@ -27,7 +32,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(services.registLanguageClient(client));
context.subscriptions.push(
commands.registerCommand('texlab.UpdateLanguageServer', async () => {
await downloadServer(context)
await downloadServer(serverRoot)
.then(() => {
workspace.showMessage(`Update TexLab Server success`);
})
Expand All @@ -43,9 +48,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
});
}

function getServerPath(context: ExtensionContext): string {
function getServerPath(root: string): string {
const name = os.platform() === 'win32' ? 'texlab.exe' : 'texlab';
return context.asAbsolutePath(`./server/${name}`);
return path.join(root, name);
}

function getServerOptions(serverPath: string): ServerOptions {
Expand Down

0 comments on commit 7c0a42e

Please sign in to comment.