Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/__tests__/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ describe('union', () => {
expect(() => ufs.readdirSync('/bar')).toThrow();
});
});

describe('copyFileSync()', () => {
it('can copy from one filesystem to another', async () => {
const vol1 = Volume.fromJSON({
'/foo/bar': 'bar',
});
const vol2 = Volume.fromJSON({
'/bar/baz': 'baz',
});

const ufs = new Union();
ufs.use(vol1 as any).use(vol2 as any);

ufs.copyFileSync('/foo/bar', '/bar/barCopy');
expect(vol2.readFileSync('/bar/barCopy').toString()).toEqual('bar');
expect(vol1.existsSync('/bar/barCopy')).toBeFalsy();
});
});
});
describe('async methods', () => {
it('Basic one file system', done => {
Expand Down
1 change: 1 addition & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type FSMethods =
| 'readFileSync'
| 'writeFileSync'
| 'appendFileSync'
| 'copyFileSync'
| 'existsSync'
| 'accessSync'
| 'createReadStream'
Expand Down
39 changes: 39 additions & 0 deletions src/union.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FSWatcher, Dirent } from 'fs';
import { IFS } from './fs';
import { Readable, Writable } from 'stream';
import { dirname } from 'path';
import { ENOENT } from 'constants';
const { fsAsyncMethods, fsSyncMethods } = require('fs-monkey/lib/util/lists');

export interface IUnionFsError extends Error {
Expand All @@ -13,6 +15,7 @@ const SPECIAL_METHODS = new Set([
'existsSync',
'readdir',
'readdirSync',
'copyFileSync',
'createReadStream',
'createWriteStream',
'watch',
Expand Down Expand Up @@ -245,6 +248,42 @@ export class Union {
return this.sortedArrayFromReaddirResult(result);
};

public copyFileSync(src: string, dest: string, flags?: number) {
const destDir = dirname(dest);

// CASE 1: dest is a filename. Don't treat, standard copy (opinionated)
if (destDir.length == 0) {
return this.syncMethod('copyFileSync', [src, dest, flags]);
}

// CASE 2: src doesn't exist in any fs.
const srcFS = this.fss.find(fs => {
try { return fs.existsSync(src) }
catch (error) { return false };
})

if (!srcFS) {
const error = new Error(`no such source file: ${src}`) as NodeJS.ErrnoException;
error.errno = ENOENT;
throw error;
}

// CASE 3: dest dir does not exist or is in the same fs.
// To protect ourselves, simply launch standard copy, whatever the policies
// about destination dir doesn't exist.
const destFS = this.fss.find(fs => {
try { return fs.existsSync(destDir) }
catch (error) { return false };
});

if (!destFS || srcFS == destFS) {
return this.syncMethod('copyFileSync', [src, dest, flags]);
}

// CASE 4: different FS. Workaround: read src, write dest.
destFS.writeFileSync(dest, srcFS.readFileSync(src));
}

public readdirPromise = async (...args): Promise<Array<readdirEntry>> => {
let lastError: IUnionFsError | null = null;
let result = new Map<string, readdirEntry>();
Expand Down