UNPKG

1.11 kBJavaScriptView Raw
1const path = require('path');
2const execFileSync = require('child_process').execFileSync;
3const vscode = require('vscode');
4
5function execCLI(input) {
6 const cli = path.resolve(__dirname, 'node_modules/es-beautifier/lib/cli.js');
7 return execFileSync('node', [cli], { input, encoding: 'utf8' });
8}
9
10function beautify() {
11 const editor = vscode.window.activeTextEditor;
12 if (editor) {
13 try {
14 const text = editor.document.getText();
15 const beautified = execCLI(text);
16 editor.edit((builder) => {
17 const start = editor.document.positionAt(0);
18 const end = editor.document.positionAt(text.length);
19 const range = new vscode.Range(start, end);
20 builder.replace(range, beautified);
21 });
22 } catch (e) {
23 console.error('es-beautifier failed:', e);
24 vscode.window.showInformationMessage('es-beautifier failed. See the cons ole log in the DevTools.');
25 }
26 }
27}
28
29exports.activate = function activate(context) {
30 const disposable = vscode.commands.registerCommand('extension.esBeautifier', beautify);
31 context.subscriptions.push(disposable);
32};