From 8051c06857d02d5df9acd37f1b97a264f1deab68 Mon Sep 17 00:00:00 2001 From: Suraj Mandal Date: Wed, 18 Sep 2024 06:30:47 +0000 Subject: [PATCH] - Make changes to package and tsconfig files to support web - Used vscode.workspace.fs to read and write files instead of fs --- package-lock.json | 4 +- package.json | 230 +++++++++++++++++++++++----------------------- src/extension.ts | 17 ++-- tsconfig.json | 4 +- 4 files changed, 127 insertions(+), 128 deletions(-) diff --git a/package-lock.json b/package-lock.json index d7caed8..8855221 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "context-copy", - "version": "0.0.1", + "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "context-copy", - "version": "0.0.1", + "version": "0.3.0", "devDependencies": { "@types/mocha": "^10.0.7", "@types/node": "20.x", diff --git a/package.json b/package.json index bfdea82..435b285 100644 --- a/package.json +++ b/package.json @@ -1,113 +1,117 @@ -{ - "name": "context-copy", - "displayName": "Context Copy", - "publisher": "collinc777", - "repository": { - "type": "git", - "url": "https://github.com/collinc777/context-copy" - }, - "description": "VS Code extension that enables one-click copying of multiple files as markdown. Features automatic language detection, file path inclusion, and clipboard integration for seamless code sharing and documentation.", - "version": "0.3.0", - "engines": { - "vscode": "^1.91.0" - }, - "icon": "logo.png", - "categories": [ - "Other" - ], - "main": "./out/extension.js", - "activationEvents": [], - "contributes": { - "commands": [ - { - "command": "extension.filesToMarkdown", - "title": "Copy for Context" - }, - { - "command": "extension.tabsToMarkdown", - "title": "Copy All Open Tabs for Context" - }, - { - "command": "extension.singleFileToMarkdown", - "title": "Copy This File for Context" - }, - { - "command": "extension.tabGroupToMarkdown", - "title": "Copy This Tab Group for Context" - } - ], - "menus": { - "explorer/context": [ - { - "command": "extension.filesToMarkdown" - } - ], - "editor/title": [ - { - "command": "extension.tabsToMarkdown" - } - ], - "editor/title/context": [ - { - "command": "extension.singleFileToMarkdown", - "group": "navigation@999", - "when": "resource" - }, - { - "command": "extension.tabGroupToMarkdown", - "group": "navigation@999" - }, - { - "command": "extension.tabsToMarkdown", - "group": "navigation@999" - } - ], - "commandPalette": [ - { - "command": "extension.filesToMarkdown", - "title": "Copy for Context" - }, - { - "command": "extension.tabsToMarkdown", - "title": "Copy All Open Tabs for Context" - }, - { - "command": "extension.singleFileToMarkdown", - "title": "Copy This File for Context" - }, - { - "command": "extension.tabGroupToMarkdown", - "title": "Copy This Tab Group for Context" - } - ] - } - }, - "scripts": { - "vscode:prepublish": "npm run compile", - "compile": "tsc -p ./", - "watch": "tsc -watch -p ./", - "pretest": "npm run compile && npm run lint", - "lint": "eslint 'src/**/*.{js,ts,tsx}' --quiet", - "lint:fix": "eslint 'src/**/*.{js,ts,tsx}' --quiet --fix", - "format": "prettier --write 'src/**/*.{js,ts,tsx}'", - "format:check": "prettier --check 'src/**/*.{js,ts,tsx}'", - "test": "node ./out/test/runTest.js", - "package": "vsce package", - "cpi": "npm run compile && npm run package && code-insiders --install-extension $(ls context-copy-*.vsix | head -n 1)" - }, - "devDependencies": { - "@types/mocha": "^10.0.7", - "@types/node": "20.x", - "@types/vscode": "^1.91.0", - "@typescript-eslint/eslint-plugin": "^7.14.1", - "@typescript-eslint/parser": "^7.11.0", - "@vscode/test-cli": "^0.0.10", - "eslint": "^8.57.0", - "eslint-config-prettier": "^9.1.0", - "eslint-plugin-prettier": "^5.2.1", - "husky": "^9.1.4", - "lint-staged": "^15.2.9", - "prettier": "^3.3.3", - "typescript": "^5.4.5" - } -} \ No newline at end of file +{ + "name": "context-copy", + "displayName": "Context Copy", + "publisher": "collinc777", + "repository": { + "type": "git", + "url": "https://github.com/collinc777/context-copy" + }, + "description": "VS Code extension that enables one-click copying of multiple files as markdown. Features automatic language detection, file path inclusion, and clipboard integration for seamless code sharing and documentation.", + "version": "0.3.0", + "engines": { + "vscode": "^1.91.0" + }, + "icon": "logo.png", + "categories": [ + "Other" + ], + "main": "./out/extension.js", + "activationEvents": [], + "contributes": { + "commands": [ + { + "command": "extension.filesToMarkdown", + "title": "Copy for Context" + }, + { + "command": "extension.tabsToMarkdown", + "title": "Copy All Open Tabs for Context" + }, + { + "command": "extension.singleFileToMarkdown", + "title": "Copy This File for Context" + }, + { + "command": "extension.tabGroupToMarkdown", + "title": "Copy This Tab Group for Context" + } + ], + "menus": { + "explorer/context": [ + { + "command": "extension.filesToMarkdown" + } + ], + "editor/title": [ + { + "command": "extension.tabsToMarkdown" + } + ], + "editor/title/context": [ + { + "command": "extension.singleFileToMarkdown", + "group": "navigation@999", + "when": "resource" + }, + { + "command": "extension.tabGroupToMarkdown", + "group": "navigation@999" + }, + { + "command": "extension.tabsToMarkdown", + "group": "navigation@999" + } + ], + "commandPalette": [ + { + "command": "extension.filesToMarkdown", + "title": "Copy for Context" + }, + { + "command": "extension.tabsToMarkdown", + "title": "Copy All Open Tabs for Context" + }, + { + "command": "extension.singleFileToMarkdown", + "title": "Copy This File for Context" + }, + { + "command": "extension.tabGroupToMarkdown", + "title": "Copy This Tab Group for Context" + } + ] + }, + "extensionKind": [ + "workspace", + "web" + ] + }, + "scripts": { + "vscode:prepublish": "npm run compile", + "compile": "tsc -p ./", + "watch": "tsc -watch -p ./", + "pretest": "npm run compile && npm run lint", + "lint": "eslint 'src/**/*.{js,ts,tsx}' --quiet", + "lint:fix": "eslint 'src/**/*.{js,ts,tsx}' --quiet --fix", + "format": "prettier --write 'src/**/*.{js,ts,tsx}'", + "format:check": "prettier --check 'src/**/*.{js,ts,tsx}'", + "test": "node ./out/test/runTest.js", + "package": "vsce package", + "cpi": "npm run compile && npm run package && code-insiders --install-extension $(ls context-copy-*.vsix | head -n 1)" + }, + "devDependencies": { + "@types/mocha": "^10.0.7", + "@types/node": "20.x", + "@types/vscode": "^1.91.0", + "@typescript-eslint/eslint-plugin": "^7.14.1", + "@typescript-eslint/parser": "^7.11.0", + "@vscode/test-cli": "^0.0.10", + "eslint": "^8.57.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.2.1", + "husky": "^9.1.4", + "lint-staged": "^15.2.9", + "prettier": "^3.3.3", + "typescript": "^5.4.5" + } +} diff --git a/src/extension.ts b/src/extension.ts index 2cb4e4e..49739bc 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,4 @@ import * as vscode from 'vscode'; -import * as fs from 'fs'; export function activate(context: vscode.ExtensionContext) { console.log('Activating extension "file-to-markdown"'); @@ -87,10 +86,7 @@ export function activate(context: vscode.ExtensionContext) { } ); - context.subscriptions.push(disposable1); - context.subscriptions.push(disposable2); - context.subscriptions.push(disposable3); - context.subscriptions.push(disposable4); + context.subscriptions.push(disposable1, disposable2, disposable3, disposable4); } async function convertFilesToMarkdown(files: vscode.Uri[]) { @@ -98,18 +94,19 @@ async function convertFilesToMarkdown(files: vscode.Uri[]) { for (const file of files) { const relativePath = vscode.workspace.asRelativePath(file); - let fileContent = fs.readFileSync(file.fsPath, 'utf8'); + const fileContent = await vscode.workspace.fs.readFile(file); - // Remove BOM if present - if (fileContent.charCodeAt(0) === 0xfeff) { - fileContent = fileContent.slice(1); + // Convert Uint8Array to string and remove BOM if present + let fileContentString = new TextDecoder().decode(fileContent); + if (fileContentString.charCodeAt(0) === 0xfeff) { + fileContentString = fileContentString.slice(1); } const languageId = await getLanguageId(file); markdownContent += `## File: ${relativePath}\n`; markdownContent += '```' + languageId + '\n'; - markdownContent += fileContent + '\n'; + markdownContent += fileContentString + '\n'; markdownContent += '```\n\n'; } diff --git a/tsconfig.json b/tsconfig.json index 6954702..4c51366 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,7 @@ "module": "Node16", "target": "ES2022", "outDir": "out", - "lib": [ - "ES2022" - ], + "lib": ["ES2022", "DOM"], "sourceMap": true, "rootDir": "src", "strict": true /* enable all strict type-checking options */