Add update command and fix download reading

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent 7ac7dd1cd9
commit 37a7cc3a0c
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

1
.gitignore vendored

@ -4,3 +4,4 @@ node_modules
.vscode-test/ .vscode-test/
*.vsix *.vsix
build build
snekdown*-*x86_64*

@ -1,9 +0,0 @@
# Change Log
All notable changes to the "snekdown-preview" extension will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [Unreleased]
- Initial release

@ -18,6 +18,18 @@ downloads the latest one.
## Release Notes ## Release Notes
### 1.0.0 ### 0.9.3
- Add update command
### 0.9.2
- Fix downloader buffer behaviour
### 0.9.1
- Fix windows download path
### 0.9.0
Initial release of the snekdown extension Initial release of the snekdown extension

@ -13,7 +13,7 @@
"url": "https://github.com/Trivernis/snekdown-vscode-extension.git" "url": "https://github.com/Trivernis/snekdown-vscode-extension.git"
}, },
"description": "Preview and commands for the snekdown markdown flavour", "description": "Preview and commands for the snekdown markdown flavour",
"version": "0.9.2", "version": "0.9.3",
"engines": { "engines": {
"vscode": "^1.52.0" "vscode": "^1.52.0"
}, },
@ -25,6 +25,9 @@
"onCommand:snekdown.preview", "onCommand:snekdown.preview",
"onCommand:snekdown.preview-side", "onCommand:snekdown.preview-side",
"onCommand:snekdown.clear-cache", "onCommand:snekdown.clear-cache",
"onCommand:snekdown.update-binary",
"onCommand:snekdown.export-html",
"onCommand:snekdown.export-pdf",
"onLanguage:snekdown", "onLanguage:snekdown",
"onLanguage:markdown" "onLanguage:markdown"
], ],
@ -39,6 +42,10 @@
"command": "snekdown.clear-cache", "command": "snekdown.clear-cache",
"title": "Snekdown: Clear Cache" "title": "Snekdown: Clear Cache"
}, },
{
"command": "snekdown.update-binary",
"title": "Snekdown: Update Binary"
},
{ {
"command": "snekdown.preview", "command": "snekdown.preview",
"title": "Snekdown: Open Preview" "title": "Snekdown: Open Preview"

@ -9,18 +9,33 @@ let snekdownWebview: SnekdownWebView;
export async function activate(context: vscode.ExtensionContext) { export async function activate(context: vscode.ExtensionContext) {
const snekdownWrapper = new SnekdownWrapper(context); const snekdownWrapper = new SnekdownWrapper(context);
await snekdownWrapper.download();
await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "Downloading Snekdown"
}, async () => {
await snekdownWrapper.download()
})
context.subscriptions.push(vscode.commands.registerCommand('snekdown.init', async () => { context.subscriptions.push(vscode.commands.registerCommand('snekdown.init', async () => {
await snekdownWrapper.init(); await snekdownWrapper.init();
vscode.window.showInformationMessage("Snekdown Project initialized."); vscode.window.showInformationMessage("Snekdown Project initialized.");
})); }));
context.subscriptions.push(vscode.commands.registerCommand('snekdown.clear-ache', async () => { context.subscriptions.push(vscode.commands.registerCommand('snekdown.clear-cache', async () => {
await snekdownWrapper.clearCache(); await snekdownWrapper.clearCache();
vscode.window.showInformationMessage("Snekdown cache cleared."); vscode.window.showInformationMessage("Snekdown cache cleared.");
})) }))
context.subscriptions.push(vscode.commands.registerCommand('snekdown.update-binary', async () => {
await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "Downloading Snekdown"
}, async () => {
await snekdownWrapper.download(true)
});
}))
context.subscriptions.push(vscode.commands.registerTextEditorCommand('snekdown.preview', async () => { context.subscriptions.push(vscode.commands.registerTextEditorCommand('snekdown.preview', async () => {
if (!snekdownWebview || snekdownWebview.isDisposed) { if (!snekdownWebview || snekdownWebview.isDisposed) {
snekdownWebview = new SnekdownWebView(context, snekdownWrapper, vscode.ViewColumn.One); snekdownWebview = new SnekdownWebView(context, snekdownWrapper, vscode.ViewColumn.One);
@ -43,7 +58,10 @@ export async function activate(context: vscode.ExtensionContext) {
value: placeholderOutput, value: placeholderOutput,
}); });
if (outputFile) { if (outputFile) {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: "Rendering HTML" }, async () => { await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "Rendering HTML"
}, async () => {
await snekdownWrapper.renderToFile(inputFile, outputFile as string, RenderingFormat.html); await snekdownWrapper.renderToFile(inputFile, outputFile as string, RenderingFormat.html);
}); });
} }
@ -57,7 +75,10 @@ export async function activate(context: vscode.ExtensionContext) {
value: placeholderOutput, value: placeholderOutput,
}); });
if (outputFile) { if (outputFile) {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: "Rendering PDF" }, async () => { await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "Rendering PDF"
}, async () => {
await snekdownWrapper.renderToFile(inputFile, outputFile as string, RenderingFormat.pdf); await snekdownWrapper.renderToFile(inputFile, outputFile as string, RenderingFormat.pdf);
}); });
} }

@ -68,28 +68,27 @@ export class SnekdownWrapper {
/** /**
* Detects or downloads the snekdown executable * Detects or downloads the snekdown executable
*/ */
public async download () { public async download (force: boolean = false) {
let execPath: string; let execPath: string;
let res: any;
switch (os.platform()) { switch (os.platform()) {
case 'win32': case 'win32':
execPath = this.buildExecutablePath(SNEKDOWN_FILE_WINDOWS); execPath = this.buildExecutablePath(SNEKDOWN_FILE_WINDOWS);
await fetch(SNEKDOWN_URL_WINDOWS).then(res => {
fs.writeFileSync(execPath, res.buffer);
});
this.executable = execPath; this.executable = execPath;
await SnekdownWrapper.downloadFile(SNEKDOWN_URL_WINDOWS, this.executable, force);
break; break;
case 'linux': case 'linux':
if (fs.existsSync("/usr/bin/snekdown")) { if (fs.existsSync("/usr/bin/snekdown") && false) {
this.executable = "/usr/bin/snekdown"; this.executable = "/usr/bin/snekdown";
break; break;
} }
execPath = this.buildExecutablePath(SNEKDOWN_FILE_LINUX); execPath = this.buildExecutablePath(SNEKDOWN_FILE_LINUX);
await fetch(SNEKDOWN_URL_LINUX).then(res => {
fs.writeFileSync(execPath, res.buffer);
});
this.executable = execPath; this.executable = execPath;
await SnekdownWrapper.downloadFile(SNEKDOWN_URL_LINUX, this.executable, force);
chmodSync(execPath, "555"); chmodSync(execPath, "555");
break; break;
default: default:
@ -97,6 +96,23 @@ export class SnekdownWrapper {
} }
} }
/**
* Downloads a file if it doesn't exist
* @param url
* @param path
* @param force
*/
private static async downloadFile(url: string, path: string, force: boolean = false) {
if (fs.existsSync(path)) {
if (!force) {
return;
}
fs.unlinkSync(path);
}
const res = await fetch(url);
fs.writeFileSync(path, await res.buffer());
}
/** /**
* Builds the path in the extension folder for a given file * Builds the path in the extension folder for a given file
*/ */

Loading…
Cancel
Save