Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: showInformationMessage if file exists #104

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions templates/vscode-extension/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,48 @@ const getRemoteDirStructure = async ({
);
};

const buildDirStructure = async (structure) => {
structure.forEach((entity) => {
if (!entity) return;

const { fileType, fileName, fileContent, filePath } = entity;
const BuildEntity = async (entity) => {
if (!entity) return Promise.resolve(true);

if (fileType === vscode.FileType.Directory) {
buildDirStructure(fileContent);
} else {
vscode.workspace.fs.writeFile(
vscode.Uri.file(`${filePath}/${fileName}`),
const { fileType, fileName, fileContent, filePath } = entity;
let writeFlag = false;
if (fileType === vscode.FileType.Directory) {
await buildDirStructure(fileContent);
} else {
const fullPath = vscode.Uri.file(`${filePath}/${fileName}`)
try {
await vscode.workspace.fs.readFile(fullPath)
const answers = {
yes: "Rewrite",
no: "Keep it"
}
const answer = await vscode.window.showInformationMessage(
`File ${fullPath} already exists`,
...Object.values(answers)
)
writeFlag = answer === answers.yes
} catch (_err) {
// Do we know a better way to ensure file doesn't exist
// but raising exception while reading the file?
writeFlag = true
}
if(writeFlag) {
await vscode.workspace.fs.writeFile(
fullPath,
new TextEncoder().encode(fileContent)
);
}
});
return Promise.resolve(true)
}
}

const buildDirStructure = async (structure) => {
let it = 0
while(it < structure.length) {
await BuildEntity(structure[it])
it += 1
}
return Promise.resolve(true)
};

module.exports = {
Expand Down