In my VS Code extension development, I am currently exploring the correct usage of an async function with an await statement to fetch data from an input box presented to the user.
A part of my code that is causing unexpected behavior is:
function runGitConfigCheck() {
console.log('\nChecking for .gitconfig file');
let requiredgitTags = ['user', 'http', 'https', 'core'];
let requiredgitConfigItems =
[
'http.sslbackend=thebestSSLofcourse',
'http.proxy=http://myproxy.website.example:1234',
'https.proxy=http://myproxy.website.example:1234',
'http.sslcainfo=C:\\Users\\myusername\\path\\to\\folder'
];
/**
TODO: other things here
*/
let gitConfigExists: boolean = checkFileExistsInTargetFolder(userProfile, '\\.gitconfig');
if (gitConfigExists === false) {
// create new empty git config
fs.appendFileSync(userProfile + '\\.gitconfig', '');
requiredgitConfigItems.forEach(function (value) {
console.log('Writing value to config: ' + value);
fs.appendFileSync(userProfile + '\\.git', '\n' + value);
});
}
else if (gitConfigExists === true) {
console.log('.gitconfig file found');
var gitconfig = ini.parse(fs.readFileSync(userProfile+"\\.gitconfig",'utf-8'));
let attributes = getGitConfigAttributeNames(gitconfig);
// check for the [user], [http], [https], and [core] attributes
let tagsNotFound = new Array();
let tagsFound = new Array();
for (var tag in requiredgitTags) {
let tagValue = requiredgitTags[tag];
console.log('searching for tag '+tagValue);
let search = searchForGitTag(tagValue, attributes);
if(search === true) {
tagsFound.push(tagValue);
}
else {
tagsNotFound.push(tagValue);
}
}
addGitTagsNotFound(tagsNotFound, userProfile+'\\.gitconfig');
console.log('Finished doing all the things!');
}
}
function appendItemsToConfigFile(file: fs.PathLike, configItems: string[], firstItemStartsNewLine?: boolean)
{
let counter: number = 0;
configItems.forEach(function (item) {
if(firstItemStartsNewLine === true && counter === 0) {
fs.writeFileSync(file, `\n${item}\n`, {encoding: 'utf-8', flag: 'as'});
}
else {
fs.writeFileSync(file, `${item}\n`, {encoding: 'utf-8', flag: 'as'});
}
counter++;
});
return;
}
async function getUserInfo(myplaceholder: string) {
let userInputWindow = vscode.window.showInputBox({ placeHolder: myplaceholder, prompt: 'Here is the prompt' });
return userInputWindow;
}
function addGitTagsNotFound(tags: string[], configFile: fs.PathLike) {
tags.forEach(function (tag) {
switch(tag) {
case 'user':
let currentName = getUserInfo('Message1')
.then(function (result) {
return result;
});
let currentEmail = getUserInfo('Message2')
.then(function (result) {
return result;
});
console.log(currentEmail + ' ' currentEmail);
break;
case 'http':
console.log('Adding config items for [http] tag');
appendItemsToConfigFile(configFile, [`[${tag}]`,
'\tsslBackend=myconfig',
`\tsslCAInfo=${userProfile}\\path\\to\\folder`,
'\tproxy=http://myproxy.website.example:1234'], true);
break;
case 'https':
console.log('Adding config items for [https] tag');
appendItemsToConfigFile(configFile, [`[${tag}]`,
`\tsslCAInfo=${userProfile}\\path\\to\\folder`,
'\tproxy=proxy=http://myproxy.website.example:1234'], true);
break;
case 'core':
console.log('Adding config items for [core] tag');
appendItemsToConfigFile(configFile, [`[${tag}]`,
`\teditor=${userProfile}\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe -w`], true);
break;
}
});
}
When I call addGitTagsNotFound()
with an array and a file, the input box only pops up after the rest of the runGitConfigCheck()
function finishes executing in the parent activate()
function of the extension.
I believe my confusion with async/await is causing this issue, and it seems that the synchronous append operation to the config file might be blocking the input box from being displayed.
I would greatly appreciate any help in explaining this to me!