Skip to content

Commit 8bf144d

Browse files
authored
Merge pull request #62 from amalej/path-fr
Added feature to list folders/files
2 parents 4fcecc9 + 7e104d0 commit 8bf144d

4 files changed

Lines changed: 95 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<!-- ADD CHANGES HERE -->
22

3+
v1.3.2
4+
5+
- Added feature to list folders/files in current path to make it easier to use
6+
`cd` and `git add <file>` commands
7+
38
v1.3.1
49

510
- Fixed issue where spawn processes cannot be exitted(#56)

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "onfire-cli",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "A CLI that extends the Firebase CLI",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/onfire-cli.ts

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ import {
66
FirebaseCommands,
77
} from "./firebase-cmd";
88
import { CommandLineInterface } from "./cli";
9-
import {
10-
ChildProcess,
11-
SpawnSyncReturns,
12-
exec,
13-
execSync,
14-
spawnSync,
15-
} from "child_process";
9+
import { execSync } from "child_process";
1610
import { CliCache, MAX_CACHE_COUNT } from "./cli-cache";
11+
import { existsSync, readdirSync, statSync } from "fs";
12+
import path from "path";
1713

1814
interface SavedConfig {
1915
pastCommands?: string[] | undefined;
@@ -425,6 +421,81 @@ export class OnFireCLI extends CommandLineInterface {
425421
}
426422
}
427423

424+
private async renderPaths() {
425+
const { base } = this.getCommandParams(this.input);
426+
if (this.firebaseCommands[base] !== undefined) {
427+
// TODO: We should add better handling when running a Firebase command
428+
// For now, do nothing since this method is kinda wonky
429+
return;
430+
}
431+
432+
this.clearTerminalDownward();
433+
console.log("");
434+
// const date = new Date();
435+
// console.log(date);
436+
const typedWord = this.getTypedWord();
437+
const modifiedPath = path.join(process.cwd(), typedWord.word);
438+
const modifiedPathExists = existsSync(modifiedPath);
439+
const currentPath = modifiedPathExists
440+
? modifiedPath
441+
: modifiedPath.substring(0, modifiedPath.lastIndexOf(path.sep));
442+
const paths: string[] = [];
443+
const pathToRead = modifiedPathExists ? modifiedPath : currentPath;
444+
const pathString = modifiedPathExists
445+
? typedWord.word
446+
: typedWord.word.substring(0, typedWord.word.lastIndexOf("/")) ||
447+
typedWord.word.substring(0, typedWord.word.lastIndexOf("\\"));
448+
449+
// console.log("modifiedPathExists:", modifiedPathExists);
450+
// console.log("pathString:", pathString);
451+
try {
452+
const pathStat = statSync(pathToRead);
453+
if (!pathStat.isDirectory()) return;
454+
const _paths = readdirSync(pathToRead);
455+
paths.push(..._paths);
456+
const list = paths.map((_path) => {
457+
const stat = statSync(path.join(pathToRead, _path));
458+
const joined =
459+
pathString === ""
460+
? _path
461+
: path.normalize([pathString, _path].join(path.sep));
462+
if (stat.isDirectory()) {
463+
return `${joined}${path.sep}`;
464+
}
465+
return `${joined}`;
466+
});
467+
468+
// console.log("list:", list.join(" | "));
469+
const pathInput =
470+
typedWord.word === "" ? typedWord.word : path.normalize(typedWord.word);
471+
const filteredList = list.filter((argVal: string) =>
472+
argVal.startsWith(pathInput)
473+
);
474+
this.itemList = filteredList;
475+
const slicedList = this.getItemsToDisplay(filteredList);
476+
// console.log("pathInput:", pathInput);
477+
// console.log("filteredList:", filteredList.join(" | "));
478+
// console.log("listItemIndex:", this.listItemIndex);
479+
for (let i = 0; i < this.itemList.length; i++) {
480+
const pathVal = this.itemList[i];
481+
if (!slicedList.includes(pathVal)) continue;
482+
if (pathVal !== undefined) {
483+
const msg =
484+
i === this.listItemIndex
485+
? `${this.textCyan(this.textBold(">"))} ${this.textGreen(
486+
this.textBold(pathVal)
487+
)}`
488+
: ` ${this.textBold(pathVal)}`;
489+
console.log(this.displaceText(`${msg}\x1b[K`));
490+
} else {
491+
console.log(`-\x1b[K`);
492+
}
493+
}
494+
} catch (_) {
495+
// Do nothing. Path and last safest path does not exists
496+
}
497+
}
498+
428499
private renderIO(newChar: string = "", tabCompletion: boolean = false) {
429500
if (tabCompletion) {
430501
this.handleTabCompletion();
@@ -467,6 +538,7 @@ export class OnFireCLI extends CommandLineInterface {
467538
} else {
468539
this.itemList = [];
469540
this.clearTerminalDownward();
541+
this.renderPaths();
470542
}
471543
this.moveCursorToSavedCurrentPos();
472544
}
@@ -655,8 +727,10 @@ export class OnFireCLI extends CommandLineInterface {
655727
if (path === undefined) {
656728
console.log(`${process.cwd()}`);
657729
} else {
658-
execSync(this.input, { stdio: "pipe" });
730+
// execSync(this.input, { stdio: "pipe" });
731+
// if (debugging === false) {
659732
process.chdir(path);
733+
// }
660734
}
661735
} else if (base === "clear" || base === "cls") {
662736
console.clear();
@@ -667,7 +741,7 @@ export class OnFireCLI extends CommandLineInterface {
667741
} else {
668742
// TODO: Might be a good idea to use spawn instead?
669743
await this.firebaseCli.killLoadModuleChildProcess();
670-
if (this.isChildProcessRunning === false) {
744+
if (this.isChildProcessRunning === false && debugging === false) {
671745
this.isChildProcessRunning = true;
672746
execSync(this.input, { stdio: "inherit" });
673747
this.isChildProcessRunning = false;
@@ -912,6 +986,10 @@ export class OnFireCLI extends CommandLineInterface {
912986
} else if (key.name === "tab") {
913987
render = true;
914988
tabCompletion = true;
989+
this.displayBuffer = {
990+
start: 0,
991+
end: this.maxItemShown,
992+
};
915993
} else if (str !== undefined && str.length === 1) {
916994
render = true;
917995
newChar = str;

0 commit comments

Comments
 (0)