Skip to content

Commit 689cbc2

Browse files
committed
builtin completion
1 parent 83eb1cf commit 689cbc2

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

app/EchoCommand.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ class EchoCommand extends Command {
88

99
execute(args) {
1010
const output = args.join(' ');
11-
// Only write to stdout, never to stderr
12-
if (!this.outputHandler.isStderr) {
11+
// write to stdout if there are arguments, otherwise write to stderr
12+
if (args.length > 0) {
1313
this.outputHandler.write(output, true);
14-
} else if (this.outputHandler.isStderr && args.length > 0) {
15-
// If stderr and redirection file and there is an error, write to ouput file
16-
this.outputHandler.initializeFile();
17-
process.stdout.write(output + '\n');
1814
} else {
1915
this.outputHandler.writeError(output, true);
2016
}

app/OutputHandler.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const fs = require('fs');
22
const path = require('path');
33

44
class OutputHandler {
5-
constructor(outputFile = null, isStderr = false, append = false, writeToFile = false) {
5+
constructor(outputFile = null, isStderr = false, append = false, writeToFile = false, redirectToStdout = false) {
6+
this.redirectToStdout = redirectToStdout;
67
this.outputFile = outputFile;
78
this.isStderr = isStderr;
89
this.append = append;
@@ -40,15 +41,15 @@ class OutputHandler {
4041
if (this.outputFile) {
4142
// Ensure file is initialized first
4243
this.initializeFile();
43-
44-
// Only write if initialization succeeded
44+
4545
if (this.fileInitialized && this.append) {
4646
fs.appendFileSync(this.outputFile, content);
4747
} else if (this.fileInitialized && this.writeToFile) {
4848
// If not appending, clear the file
4949
fs.writeFileSync(this.outputFile, content);
5050
}
51-
process.stdout.write(content);
51+
if (!this.isStderr) return;
52+
process.stdout.write(content)
5253
} else {
5354
const stream = this.isStderr ? process.stderr : process.stdout;
5455
if (this.outputFile) {
@@ -71,9 +72,6 @@ class OutputHandler {
7172
} else if (this.fileInitialized && this.writeToFile) {
7273
// If not appending, clear the file
7374
fs.writeFileSync(this.outputFile, content);
74-
} else {
75-
// Fall back to stderr console
76-
process.stderr.write(content);
7775
}
7876
} else {
7977
if(this.outputFile) {

foo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
jdfl
1+
cat: fjffj: No such file or directory

foo2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
README.md
2+
app
3+
codecrafters.yml
4+
foo.md
5+
foo2.md
6+
package-lock.json
7+
package.json
8+
your_program.sh

0 commit comments

Comments
 (0)