Skip to content

Commit 73ecf2d

Browse files
committed
Updates directory check to see if it is a non-empty dir
1 parent c9c5c0f commit 73ecf2d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/commands/kickstart-install.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fs from 'node:fs'
88
import path from "node:path";
99
import { dirname } from 'node:path';
1010
import { fileURLToPath } from 'node:url';
11-
import { betaWarning, isDockerInstalled } from "../utils.js";
11+
import { betaWarning, isDirEmpty, isDockerInstalled } from "../utils.js";
1212

1313
const __dirname = dirname(fileURLToPath(import.meta.url));
1414

@@ -39,14 +39,15 @@ const action = async function (dir: string) {
3939
throw (chalk.red("Error: You don't have Docker installed. It's the easiest way to get everything you need\n") + chalk.cyan("Please install Docker. For developers new to Docker, we suggest Orbstack: https://docs.orbstack.dev/quick-start"))
4040
}
4141

42-
if (fs.existsSync(directory)) {
43-
throw (chalk.red(`Target directory (${directory}) already exists`))
42+
if (fs.existsSync(directory) && !isDirEmpty(directory)) {
43+
throw (chalk.redBright(`Error: `) + `Target directory (${chalk.yellow(directory)}) has files.\n\nPlease choose an empty or non-existent directory\n`)
4444
}
45+
4546
const parentDir = path.dirname(directory)
4647

4748
try {
4849
fs.accessSync(parentDir, fs.constants.W_OK)
49-
} catch(err) {
50+
} catch (err) {
5051
console.error(chalk.red(`Can't write to ${parentDir}. Please check permissions on the directory`))
5152
}
5253

@@ -68,7 +69,7 @@ const action = async function (dir: string) {
6869
validate: (text) => {
6970
if (text.length == 0) {
7071
return 'Custom password is required'
71-
} else if(text.length < 8) {
72+
} else if (text.length < 8) {
7273
return 'Password must be at least 8 characters (You can change this requirement later in your tenant password settings)'
7374
} else {
7475
return true
@@ -91,7 +92,7 @@ const action = async function (dir: string) {
9192
}, 500)
9293
setTimeout(() => {
9394
console.log(chalk.green(`Creating Kickstart file`))
94-
if (!fs.existsSync(directory)) throw(chalk.red(`Something went wrong. ${directory} does not exists.`))
95+
if (!fs.existsSync(directory)) throw (chalk.red(`Something went wrong. ${directory} does not exists.`))
9596
createKickstart(__dirname + '/resources/kickstart/kickstart.json', answers, directory)
9697
}, 1500)
9798

src/utils.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import ClientResponse from '@fusionauth/typescript-client/build/src/ClientResponse.js';
22
import {Errors} from '@fusionauth/typescript-client';
3-
import chalk from 'chalk';
3+
import fs from 'node:fs'
44

5+
import chalk from 'chalk';
56
import boxen from 'boxen';
67
import { execSync } from 'node:child_process';
78
/**
@@ -172,4 +173,19 @@ export function isDockerInstalled() {
172173
} catch (e) {
173174
return false;
174175
}
176+
}
177+
178+
/**
179+
* Returns true if path directory contains no files
180+
* Path must exist
181+
* @param path
182+
*/
183+
export function isDirEmpty(path: string) {
184+
const data = fs.readdirSync(path)
185+
186+
if (data.length > 0) {
187+
return false
188+
} else {
189+
return true
190+
}
175191
}

0 commit comments

Comments
 (0)