Skip to content

Commit

Permalink
feat(fslib): add FileHandle.readLines (#5121)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Dec 12, 2022
1 parent 2e625d5 commit 1cf941c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .yarn/versions/f12a955a.yml
@@ -0,0 +1,39 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/fslib": minor
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/libzip"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ Yarn now accepts sponsorships! Please give a look at our [OpenCollective](https:
### Compatibility

- Updates the PnP compatibility layer for TypeScript v4.9.4.
- The patched filesystem now supports `FileHandle.readLines`.

## 3.3.0

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/worker-zip/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts
@@ -1,4 +1,5 @@
import type {BigIntStats, ReadStream, StatOptions, Stats, WriteStream, WriteVResult} from 'fs';
import {createInterface} from 'readline';

import type {CreateReadStreamOptions, CreateWriteStreamOptions, FakeFS} from '../FakeFS';
import type {Path} from '../path';
Expand Down Expand Up @@ -212,6 +213,13 @@ export class FileHandle<P extends Path> {
}
}

readLines(options?: CreateReadStreamOptions) {
return createInterface({
input: this.createReadStream(options),
crlfDelay: Infinity,
});
}

stat(
opts?: StatOptions & {
bigint?: false | undefined;
Expand Down
19 changes: 19 additions & 0 deletions packages/yarnpkg-fslib/tests/patchedFs.test.ts
Expand Up @@ -557,6 +557,25 @@ describe(`patchedFs`, () => {
});
});

it(`should support FileHandle.readLines`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

await xfs.mktempPromise(async dir => {
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
await patchedFs.promises.writeFile(filepath, `1\n\n2\n`);

// @ts-expect-error - Types are out of date
const fd = await patchedFs.promises.open(filepath);

const lines: Array<string> = [];
// @ts-expect-error - Types are out of date
for await (const line of fd.readLines())
lines.push(line);

expect(lines).toStrictEqual([`1`, ``, `2`]);
});
});

ifNotWin32It(`should support FileHandle.chmod`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

0 comments on commit 1cf941c

Please sign in to comment.