From 31062c4634dc29d6fdacc787bbf93a9b21240b61 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Mon, 22 Jul 2019 16:54:57 +0200 Subject: [PATCH] add read/write sample, tweak all samples --- fsconsumer-sample/package.json | 14 +++++-- fsconsumer-sample/src/extension.ts | 62 +++++++++++++++++++----------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/fsconsumer-sample/package.json b/fsconsumer-sample/package.json index 0a837809..9cbd19c8 100644 --- a/fsconsumer-sample/package.json +++ b/fsconsumer-sample/package.json @@ -16,18 +16,26 @@ ], "activationEvents": [ "onCommand:fsConsume/findJS", - "onCommand:fsConsume/sumSizes" + "onCommand:fsConsume/sumSizes", + "onCommand:fsConsume/readWriteFile" ], "main": "./out/src/extension", "contributes": { "commands": [ { "command": "fsConsume/findJS", - "title": "ConsumeFS: Find JavaScript" + "title": "Find JavaScript", + "category": "ConsumeFS" }, { "command": "fsConsume/sumSizes", - "title": "ConsumeFS: Sum file sizes in folder" + "title": "Sum file sizes in folder", + "category": "ConsumeFS" + }, + { + "command": "fsConsume/readWriteFile", + "title": "Read and Write file", + "category": "ConsumeFS" } ] }, diff --git a/fsconsumer-sample/src/extension.ts b/fsconsumer-sample/src/extension.ts index bc7c7e1a..b45d3524 100644 --- a/fsconsumer-sample/src/extension.ts +++ b/fsconsumer-sample/src/extension.ts @@ -1,56 +1,74 @@ 'use strict'; import * as vscode from 'vscode'; -import * as path from 'path'; +import { posix } from 'path'; +import { StringDecoder } from 'string_decoder'; export function activate(context: vscode.ExtensionContext) { // Sample 1 - Check if for a TypeScript file a JavaScript files exists // * use `uri.with` to derive a new uri without loosing things like scheme, authority, query, or fragment - // * use `path.path` because uris use slashes to separate paths, e.g. backslash is a valid file name character - vscode.commands.registerCommand('fsConsume/findJS', async () => { - if (!vscode.window.activeTextEditor) { + // * use `path.posix` because uris use slashes to separate paths, e.g. backslash is a valid file name character + vscode.commands.registerCommand('fsConsume/findJS', async function () { + if (!vscode.window.activeTextEditor || + posix.extname(vscode.window.activeTextEditor.document.uri.path) !== '.ts' + ) { return vscode.window.showInformationMessage('Open a TypeScript file first'); } const tsUri = vscode.window.activeTextEditor.document.uri; - - if (path.posix.extname(tsUri.path) !== '.ts') { - return vscode.window.showInformationMessage('Open a TypeScript file first'); - } - - const jsUri = tsUri.with({ path: path.posix.join(tsUri.path, '..', path.posix.basename(tsUri.path, '.ts')) }); + const jsPath = posix.join(tsUri.path, '..', posix.basename(tsUri.path, '.ts') + '.js'); + const jsUri = tsUri.with({ path: jsPath }); try { await vscode.workspace.fs.stat(jsUri); - vscode.window.showInformationMessage('JS file does exist'); + vscode.window.showInformationMessage(`${jsUri.toString(true)} file does exist`); } catch { - vscode.window.showInformationMessage('JS file does *not* exist'); + vscode.window.showInformationMessage(`${jsUri.toString(true)} file does *not* exist`); } }); // Sample 2 - Sum up the size of all files inside a folder (none recursive) - vscode.commands.registerCommand('fsConsume/sumSizes', async () => { + vscode.commands.registerCommand('fsConsume/sumSizes', async function () { - const sumUpFolder = async (folder: vscode.Uri) => { + async function sizeOfAllFilesInFolder(folder: vscode.Uri) { let sum = 0; for (const [name, type] of await vscode.workspace.fs.readDirectory(folder)) { if (type === vscode.FileType.File) { - const stat = await vscode.workspace.fs.stat(folder.with({ path: path.posix.join(folder.path, name) })); + const filePath = posix.join(folder.path, name); + const stat = await vscode.workspace.fs.stat(folder.with({ path: filePath })); sum += stat.size; } } return sum; - }; + } if (!vscode.workspace.workspaceFolders) { return vscode.window.showInformationMessage('No folder or workspace opened'); + } - } else if (vscode.workspace.workspaceFolders) { - let sum = 0; - for (let i = 0; i < vscode.workspace.workspaceFolders.length; i++) { - sum += await sumUpFolder(vscode.workspace.workspaceFolders[i].uri); - } - return vscode.window.showInformationMessage(`Sum of file sizes is ${sum}bytes`); + for (const folder of vscode.workspace.workspaceFolders) { + const total = await sizeOfAllFilesInFolder(folder.uri); + vscode.window.showInformationMessage(`${total} bytes in ${folder.uri.toString(true)}`); } }); + + // Sample 3 - Create a Uint8Array from a string and reverse + vscode.commands.registerCommand('fsConsume/readWriteFile', async function () { + + if (!vscode.workspace.workspaceFolders) { + return vscode.window.showInformationMessage('No folder or workspace opened'); + } + + const writeStr = '1€ is 1.12$ is 0.9£'; + const writeData = Buffer.from(writeStr); + + const folder = vscode.workspace.workspaceFolders[0].uri; + const fileUri = folder.with({ path: posix.join(folder.path, 'test.txt') }); + await vscode.workspace.fs.writeFile(fileUri, writeData); + + const readData = await vscode.workspace.fs.readFile(fileUri); + const readStr = new StringDecoder().end(Buffer.from(readData)); + vscode.window.showInformationMessage(readStr); + }); + }