In a VSCode extension I have a function that removes certain directories from the current workspace. But the function does not wait for the directories to be removed.
The accepted answer for this Stackoverflow question makes sense, but still I can't figure out how to make my function work correctly. Where do I put await and return?
async function cleanUpWorkspace(workspace: any) {
const fs = require('fs');
const rimraf = require("rimraf");
var targetFolders = ['dirA', 'dirB', 'dirC', 'dirD'];
try {
await targetFolders.forEach(function (value) { //'await' has no effect on the type of this expression.
let targetPath = workspace + "\\\\" + value;
if (fs.existsSync(targetPath)) {
fs.promises.access(targetPath);
rimraf(targetPath, function (err: any) {
if (err) {
console.log(err);
} else {
console.log("Directory: " + value + " was deleted");
}
});
} else {
console.log(value + " doesn't exist.");
}
});
return Promise.resolve();
} catch (error) {
console.log(error);
}
}
async function prepare() {
await cleanUpWorkspace(workspace);
}
- Typescript 4.3.5
- node.js 14.17.0