Skip to content

Commit

Permalink
Merge pull request #239 from pizzafroide/fix/different-things
Browse files Browse the repository at this point in the history
Fix: different things
  • Loading branch information
streamich authored Nov 6, 2018
2 parents 2d43c7e + 1c9c57f commit 39694fa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/relative-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ on-disk filesystem and you will probably not have that directory available in yo
`memfs` volume.

The best solution is to always use absolute paths. Alternatively, you can use
`mkdirp` method to recursively create the current working directory in your
`mkdir` method to recursively create the current working directory in your
volume:

```js
vol.mkdirpSync(process.cwd());
vol.mkdirSync(process.cwd(), { recursive: true });
```

Or, you can set the current working directory to `/`, which
Expand Down
6 changes: 5 additions & 1 deletion src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {URL} from 'url';
import {Link, Node, Stats, Dirent} from "../node";
import {Volume, filenameToSteps, StatWatcher} from "../volume";


describe('volume', () => {
describe('filenameToSteps(filename): string[]', () => {
it('/ -> []', () => {
Expand Down Expand Up @@ -432,6 +432,10 @@ describe('volume', () => {
expect(buf).toBeInstanceOf(Buffer);
expect(str).toBe(data);
});
it('Read file with path passed as URL', () => {
const str = vol.readFileSync(new URL('file:///text.txt')).toString();
expect(str).toBe(data);
});
it('Specify encoding as string', () => {
const str = vol.readFileSync('/text.txt', 'utf8');
expect(str).toBe(data);
Expand Down
13 changes: 7 additions & 6 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,14 @@ export class Volume {
private readFileBase(id: TFileId, flagsNum: number, encoding: TEncoding): Buffer | string {
let result: Buffer | string;

const userOwnsFd = isFd(id);
const isUserFd = typeof id === 'number';
let userOwnsFd: boolean = isUserFd && isFd(id);
let fd: number;

if(userOwnsFd) {
fd = id as number;
} else {
const steps = filenameToSteps(id as string);
if(userOwnsFd) fd = id as number;
else {
const filename = pathToFilename(id as TFilePath);
const steps = filenameToSteps(filename);
const link: Link = this.getResolvedLink(steps);

if(link) {
Expand Down Expand Up @@ -1552,7 +1553,7 @@ export class Volume {
}

appendFile(id: TFileId, data: TData, callback: TCallback<void>);
appendFile(id: TFileId, data: TData, options: IAppendFileOptions, callback: TCallback<void>);
appendFile(id: TFileId, data: TData, options: IAppendFileOptions | string, callback: TCallback<void>);
appendFile(id: TFileId, data: TData, a, b?) {
const [opts, callback] = getAppendFileOptsAndCb(a, b);

Expand Down

0 comments on commit 39694fa

Please sign in to comment.