Skip to content

Instantly share code, notes, and snippets.

@avionbg
Created August 24, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avionbg/b268b764ddae16a1dcb7aaf352059989 to your computer and use it in GitHub Desktop.
Save avionbg/b268b764ddae16a1dcb7aaf352059989 to your computer and use it in GitHub Desktop.
Bugfix for vscode-file-templates-ext - making multiple variables work
"use strict";
var vscode = require("vscode");
var fs = require("fs");
var path = require("path");
var helpers = require("../helpers");
var moment = require("moment");
/**
* Main command to create a file from a template.
* This command can be invoked by the Command Palette or in a folder context menu on the explorer view.
* @export
* @param {TemplatesManager} templatesManager
* @param {*} args
* @returns
*/
function run(templatesManager, args) {
var templates = templatesManager.getTemplates();
// gets the target folder. if its invoked from a context menu,
// we use that reference, otherwise we use the file system path
var targetFolder = args ? args.fsPath : vscode.workspace.rootPath;
if (templates.length === 0) {
var optionGoToTemplates = {
title: "Open Templates Folder"
};
vscode.window.showInformationMessage("No templates found!", optionGoToTemplates).then(function (option) {
// nothing selected
if (!option) {
return;
}
helpers.openFolderInExplorer(templatesManager.getTemplatesDir());
});
return;
}
// show the list of available templates.
vscode.window.showQuickPick(templates).then(function (selection) {
// nothing selected. cancel
if (!selection) {
return;
}
// ask for filename
var inputOptions = {
prompt: "Please enter the desired file name",
value: selection,
};
vscode.window.showInputBox(inputOptions).then(function (filename) {
var workspaceSettings = vscode.workspace.getConfiguration("fileTemplates");
var fileContents = templatesManager.getTemplate(selection);
var className = filename.replace(/\.[^/.]+$/, "");
var resultsPromise = [];
var expression = /#{(\w+)}/g;
var placeholders = [];
var matches = expression.exec(fileContents);
while (matches) {
if (placeholders.indexOf(matches[0]) === -1) {
placeholders.push(matches[0]);
}
matches = expression.exec(fileContents);
}
placeholders.forEach(function (placeholder) {
var variableName = /#{(\w+)}/.exec(placeholder)[1];
var search = new RegExp(placeholder, "g");
switch (variableName) {
case "filename":
fileContents = fileContents.replace(search, className);
break;
case "filepath":
var workspaceRoot = vscode.workspace.rootPath;
fileContents = fileContents.replace(search, targetFolder.replace(workspaceRoot + "/", ""));
break;
case "year":
fileContents = fileContents.replace(search, moment().format("YYYY"));
break;
case "date":
fileContents = fileContents.replace(search, moment().format("D MMM YYYY"));
break;
default:
if (workspaceSettings && workspaceSettings[variableName]) {
fileContents = fileContents.replace(search, workspaceSettings[variableName]);
}
else {
resultsPromise.push([variableName, search]);
/*var variableInput_1 = {
prompt: "Please enter the desired value for \"" + variableName + "\""
};
var variablePromise = new Promise(function (resolve, reject) {
vscode.window.showInputBox(variableInput_1).then(function (value) {
var replacement;
if (!value) {
replacement = variableName.toUpperCase();
}
else {
replacement = value;
}
fileContents = fileContents.replace(search, replacement);
resolve(fileContents);
});
});
resultsPromise.push(variablePromise);*/
}
break;
}
});
vscode.window.showInputBox({prompt: `Enter values: ${resultsPromise.map(r=>r[0]).join(' ')}`})
.then(function(value) {
var subs = value.split(' ');
if (subs.length === resultsPromise.length) {
subs.forEach(function(sub, indx) {
fileContents = fileContents.replace(resultsPromise[indx][1], sub);
})
} else {
Promise.reject('Number of variables entered does not match!');
}
Promise.resolve(fileContents);
})
.then(function(content) {
var fullname = path.join(targetFolder, filename);
console.log(targetFolder);
fs.writeFile(path.join(targetFolder, filename), fileContents, function (err) {
if (err) {
vscode.window.showErrorMessage(err.message);
}
vscode.workspace.openTextDocument(fullname).then(function (doc) {
var editor = vscode.window.activeTextEditor;
vscode.window.showTextDocument(doc, editor.viewColumn);
});
});
});
//Promise.all(resultsPromise).then(function () {
// var fullname = path.join(targetFolder, filename);
// fs.writeFile(path.join(targetFolder, filename), fileContents, function (err) {
// if (err) {
// vscode.window.showErrorMessage(err.message);
// }
// vscode.workspace.openTextDocument(fullname).then(function (doc) {
// var editor = vscode.window.activeTextEditor;
// vscode.window.showTextDocument(doc, editor.viewColumn);
// });
// });
//});
});
});
}
exports.run = run;
//# sourceMappingURL=fileFromTemplateCommand.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment