Skip to content

Commit

Permalink
Merge pull request #11 from ColinFay/9-fail-gracefully-when-folder-al…
Browse files Browse the repository at this point in the history
…ready-exists

feat: init fails gracefully if the folder already exists
  • Loading branch information
ColinFay committed Jun 12, 2024
2 parents 6eb3355 + 2e63198 commit 4c9dad0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/download-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function download_and_untar(

// Delete the tarball
fs.unlinkSync(full_path)
console.log(`✅ {${pkgname}} downloaded and extracted ----\n`);
console.log(`✅ {${pkgname}} downloaded and extracted ----`);
} catch (error) {
console.error('Error:', error.message);
}
Expand Down
8 changes: 7 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const fs = require("fs");
const path = require("path");
const log = console.log;
const error = console.error;
const { execSync } = require("child_process");

const {installIt} = require("./install");

const init = async (destination_folder) => {

// check if destination_folder does not exists
if (fs.existsSync(destination_folder)){
error(`❌ A folder named ${destination_folder} already exists. \nPlease choose another name or remove the existing folder.`);
return false;
}

log("👉 Initializing project ----");
log("(This may take some time, please be patient)")

// Making destination_folder folder full path
destination_folder = path.resolve(destination_folder);
basename = path.basename(destination_folder);

// creating the folder if it doesn't exist
if (!fs.existsSync(destination_folder)){
Expand Down
2 changes: 1 addition & 1 deletion src/install-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const installOnePackage = async function (
) {
// check if the package is already installed
if (fs.existsSync(path.join(destination_folder, pkgname))) {
console.log(`❗️ Package {${pkgname}} is already installed`)
console.log(`❗️ {${pkgname}} is already installed`)
return
}

Expand Down
5 changes: 5 additions & 0 deletions tests/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ test('init works', async () => {
)
).toBe(true);

// testing that the function return undefined
// if the folder already exists
const folder_can_be_created = await init(temp_dir);
expect(folder_can_be_created).toBe(false);

}, 1000000);

0 comments on commit 4c9dad0

Please sign in to comment.