Skip to content

Commit 024e534

Browse files
fix: "Create project" should prompt before overwriting existing files
1 parent 87c07c7 commit 024e534

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/commands/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { mkdirSync, readFileSync, writeFileSync } = require("fs");
1+
const { mkdirSync, readFileSync, writeFileSync, readdirSync } = require("fs");
22
const vscode = require("vscode");
33
const { msgs } = require("../utils/msgs");
4-
const { mapEnumsToQuickPick, getTemplates, copyTemplateByName, waitFor } = require("../utils/misc");
4+
const { mapEnumsToQuickPick, getTemplates, copyTemplateByName, waitFor, hasExistingFiles } = require("../utils/misc");
55
const { relative } = require("path");
66
const { Project } = require("../Project");
77
const { DeviceManager } = require("../Watcher/DeviceManager");
@@ -292,7 +292,16 @@ class Commands {
292292
const device = this.pymakr.devicesStore.get().find((d) => d.name === match[1]);
293293
const project = /** @type {Project} */ ({ folder: projectUri.fsPath });
294294
this.commands.downloadProject({ device, project });
295-
} else copyTemplateByName(templateName, projectUri.fsPath);
295+
} else {
296+
const hasFiles = hasExistingFiles(projectUri.fsPath, ["pymakr.conf"]);
297+
const choice =
298+
hasFiles &&
299+
(await vscode.window.showQuickPick(["Keep", "Overwrite"], {
300+
title: "Folder has existing files. How do you wish to proceed?",
301+
}));
302+
const overwrite = choice === "Overwrite";
303+
copyTemplateByName(templateName, projectUri.fsPath, overwrite);
304+
}
296305
},
297306

298307
/**

src/utils/misc.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const once = (fn, context) => {
2020
};
2121
};
2222

23+
/**
24+
* Checks if a dir has existing files.
25+
* @param {string} path
26+
* @param {string[]} ignore
27+
* @returns {Boolean}
28+
*/
29+
const hasExistingFiles = (path, ignore = []) => !!readdirSync(path).filter((file) => !ignore.includes(file)).length;
30+
2331
/**
2432
* if the input isn't an array, an array will be returned containing the input
2533
* if the input is an array, the input will be returned
@@ -277,8 +285,10 @@ const createThrottledFunction = (fn, time) => {
277285
};
278286

279287
const getTemplates = () => readdirSync(TEMPLATES_PATH).map((name) => ({ name, path: resolve(TEMPLATES_PATH, name) }));
280-
const copyTemplateByName = (name, destination) => copyTemplateByPath(resolve(TEMPLATES_PATH, name), destination);
281-
const copyTemplateByPath = (path, destination) => cpSync(path, destination, { recursive: true });
288+
const copyTemplateByName = (name, destination, overwrite) =>
289+
copyTemplateByPath(resolve(TEMPLATES_PATH, name), destination, overwrite);
290+
const copyTemplateByPath = (path, destination, overwrite) =>
291+
cpSync(path, destination, { recursive: true, force: overwrite });
282292

283293
/**
284294
* converts {foo:FOO, bar:BAR} to ['foo=FOO', 'bar=BAR']
@@ -380,6 +390,7 @@ const createQueue = () => {
380390
*/
381391

382392
module.exports = {
393+
hasExistingFiles,
383394
dynamicPromiseAll,
384395
onResolveAll,
385396
objToSerializedEntries,

0 commit comments

Comments
 (0)