Skip to content

Commit 399eb2e

Browse files
committed
fix: improve directory-conditionals, add isEmptyDirOrHasGit method
1 parent 92b4557 commit 399eb2e

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/finda.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,18 @@ export class Finda {
208208
}
209209

210210
/**
211-
* Finda package destination
211+
* Is current directory empty, or does it have a .git directory?
212+
*
213+
* @param {string} gitPath Path to .git directory
214+
* @returns {boolean}
215+
* @memberof Finda
216+
*/
217+
isEmptyDirOrHasGit(gitPath: string = `${this.gitPath}`): boolean {
218+
return (emptyDirSync(process.cwd()) || existsSync(pathResolve(gitPath)));
219+
}
220+
221+
/**
222+
* Find package destination
212223
*
213224
* Try to find package destination (directory or path) via;
214225
* - resolve path via parameter
@@ -219,10 +230,7 @@ export class Finda {
219230
* @memberof Finda
220231
*/
221232
packageDestination(directoryOrPath: string): string {
222-
if (emptyDirSync(process.cwd()) || existsSync(pathResolve('.git'))) {
223-
return process.cwd();
224-
}
225-
return pathResolve(directoryOrPath);
233+
return (this.isEmptyDirOrHasGit()) ? process.cwd() : pathResolve(directoryOrPath);
226234
}
227235

228236
/**
@@ -254,10 +262,8 @@ export class Finda {
254262
*/
255263
packageName(defaultValue = `${this.username()}-package`): string {
256264
let name = this._getFromPackage('name', defaultValue);
257-
if (!get(name, 'length')) {
258-
if (emptyDirSync(process.cwd()) || existsSync(pathResolve('.git'))) {
259-
name = (process.cwd().match(/[^\/]+$/g) || [defaultValue]).join('');
260-
}
265+
if (!get(name, 'length') && this.isEmptyDirOrHasGit()) {
266+
name = (process.cwd().match(/[^\/]+$/g) || [defaultValue]).join('');
261267
}
262268
return name;
263269
}

0 commit comments

Comments
 (0)