Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make ProcessOutput iterable #1088

Closed
wants to merge 12 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: make resolved ProcessPromise iterable
Add a synchronous iterator to the `ProcessOutput` class to allow iterating over stdout lines.

* **src/core.ts**
  - Add a synchronous iterator method `[Symbol.iterator]()` to the `ProcessOutput` class.
  - Split the `stdout` string into lines using `\r?\n` to handle both Unix and Windows line endings.
  - Return the iterator of the split array.

* **test/core.test.js**
  - Add a test case to verify the synchronous iterator method in the `ProcessOutput` class.
  - Ensure the test case covers both Unix and Windows line endings.
Pulkit1822 committed Jan 29, 2025
commit 352ad01c1928a1540d2245ad2dc4a74667196098
7 changes: 7 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -802,6 +802,13 @@ export class ProcessOutput extends Error {
duration: ${this.duration}
}`
}

/**
* Synchronous iterator to allow iterating over stdout lines.
*/
[Symbol.iterator](): IterableIterator<string> {
return this.stdout.split(/\r?\n/)[Symbol.iterator]();
}
}

export function usePowerShell() {
6 changes: 6 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
@@ -1161,6 +1161,12 @@ describe('core', () => {
assert.match(ProcessOutput.getErrorMessage({}, ''), /Unknown error/)
})
})

test('synchronous iterator', () => {
const output = new ProcessOutput(0, null, 'line1\nline2\r\nline3\n', '', '', '');
const lines = Array.from(output);
assert.deepEqual(lines, ['line1', 'line2', 'line3']);
});
})

describe('cd()', () => {