无法调试/用console.log时,如何获取VS Code扩展中templateDir变量值?
获取VS Code扩展模板目录路径的方法
我需要找到所安装VS Code扩展的模板存储位置及存储方式,以便修改代码添加默认模板。已定位到TemplatesManager.prototype.getTemplates函数中的templateDir变量,但无法通过调试或console.log获取其值,尝试输出无结果,现附上templatesManager.js代码,寻求获取该变量值的方法。
'use strict'; var fs = require('fs'); var path = require('path'); var os = require('os'); /** * Main class to handle the logic of the File Templates * @export * @class TemplatesManager */ var TemplatesManager = (function () { function TemplatesManager(config) { this.config = config; } /** * Returns a list of available file templates by reading the Templates Directory. * @returns string[] */ TemplatesManager.prototype.getTemplates = function () { // @TODO make this async (use promises ???) var templateDir = this.getTemplatesDir(); var templateFiles = fs.readdirSync(templateDir).map(function (item) { if (!/^\./.exec(item)) { return fs.statSync(path.join(templateDir, item)).isFile() ? item : null; } return null; }).filter(function (filename) { return filename !== null; }); return templateFiles; }; /** * Read the contents of a templates * @param filename The name of the template file. * @return Buffer */ TemplatesManager.prototype.getTemplate = function (filename) { return fs.readFileSync(path.join(this.getTemplatesDir(), filename), 'utf8'); }; /** * Returns the templates directory location. * If no user configuration is found, the extension will look for * templates in USER_DATA_DIR/Code/FileTemplates. * Otherwise it will look for the path defined in the extension configuration. * @return {string} */ TemplatesManager.prototype.getTemplatesDir = function () { return this.config.get('templates_dir', this.getDefaultTemplatesDir()); }; /** * Returns the default templates location based on the user OS. * @returns {string} */ TemplatesManager.prototype.getDefaultTemplatesDir = function () { var userDataDir = null; switch (process.platform) { case 'linux': userDataDir = path.join(os.homedir(), '.config'); break; case 'darwin': userDataDir = path.join(os.homedir(), 'Library', 'Application Support'); break; case 'win32': userDataDir = process.env.APPDATA; break; default: throw Error("Unrecognizable operative system"); } return path.join(userDataDir, 'Code', 'User', 'FileTemplates'); }; /** * Creates the templates dir if not exists * @throw Error */ TemplatesManager.prototype.createTemplatesDirIfNotExists = function () { var templatesDir = this.getTemplatesDir(); fs.mkdir(templatesDir, '0755', function (err) { if (err && err.code != 'EEXIST') { throw Error("Failed to created templates directory " + templatesDir); } }); }; return TemplatesManager; }()); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TemplatesManager; //# sourceMappingURL=templatesManager.js.map
解决方法
1. 修改代码直接输出路径到VS Code控制台/弹窗
在getTemplates方法中添加输出逻辑,触发模板加载时即可看到路径:
TemplatesManager.prototype.getTemplates = function () { var templateDir = this.getTemplatesDir(); // 输出到VS Code调试控制台 console.log('当前模板目录:', templateDir); // 或者用弹窗直接显示路径 const vscode = require('vscode'); vscode.window.showInformationMessage(`模板目录路径: ${templateDir}`); var templateFiles = fs.readdirSync(templateDir).map(function (item) { if (!/^\./.exec(item)) { return fs.statSync(path.join(templateDir, item)).isFile() ? item : null; } return null; }).filter(function (filename) { return filename !== null; }); return templateFiles; };
修改后按F1执行Reload Window重新加载扩展,触发模板相关操作即可看到输出。
2. 在扩展入口直接调用getTemplatesDir
找到扩展入口文件(通常为extension.js/extension.ts),实例化TemplatesManager后直接调用方法获取路径:
const vscode = require('vscode'); const TemplatesManager = require('./templatesManager'); function activate(context) { const config = vscode.workspace.getConfiguration('你的扩展ID'); // 替换为实际扩展的配置ID const templateManager = new TemplatesManager(config); console.log('模板目录:', templateManager.getTemplatesDir()); }
3. 从代码逻辑推导默认路径
如果未配置自定义模板路径,默认路径根据系统不同分别是:
- Windows:
%APPDATA%\Code\User\FileTemplates - macOS:
~/Library/Application Support/Code/User/FileTemplates - Linux:
~/.config/Code/User/FileTemplates
内容的提问来源于stack exchange,提问作者user19551915
相关产品推荐
相关产品推荐

