我无法在vscode扩展名中创建文件

时间:2020-08-27 17:19:04

标签: javascript vscode-extensions

我正在尝试进行第一个扩展,为了进行测试,我决定只创建一个扩展程序,该扩展程序将使用入门代码创建HTML文件。但是我不知道为什么这行不通。 (我正在使用JavaScript)

这是错误:

“创建HTML模板”命令导致错误(运行贡献的

命令:“ extension.createTemplate”失败。)

代码如下:

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "extension" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with  registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('extension.createTemplate', function () {
        const htmlCode = `Insert HTML here.`;

        const folderPath = vscode.workspace.workspaceFolders[0].uri.toString().split(":")[1];

        fs.writeFile(path.join(folderPath, "index.html"), htmlCode, err => {
            if(err) {
                console.error(err);
                return vscode.window.showErrorMessage("Failed to create HTML file.");
            }

            vscode.window.showInformationMessage("Successfully created HTML template.");
        });
    });

    context.subscriptions.push(disposable);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate
}

0 个答案:

没有答案