File size: 1,254 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as fs from 'graceful-fs';
import { dirname } from 'path';
import mkdirp from 'mkdirp';
import resolvePathAndOptions from '../utils/resolvePathAndOptions';

export function copyFile () {
	const { resolvedPath: src, options: readOptions } = resolvePathAndOptions( arguments );

	return {
		to () {
			const { resolvedPath: dest, options: writeOptions } = resolvePathAndOptions( arguments );

			return new Promise( ( fulfil, reject ) => {
				mkdirp( dirname( dest ), err => {
					if ( err ) {
						reject( err );
					} else {
						const readStream = fs.createReadStream( src, readOptions );
						const writeStream = fs.createWriteStream( dest, writeOptions );

						readStream.on( 'error', reject );
						writeStream.on( 'error', reject );

						writeStream.on( 'close', fulfil );

						readStream.pipe( writeStream );
					}
				});
			});
		}
	};
}

export function copyFileSync () {
	const { resolvedPath: src, options: readOptions } = resolvePathAndOptions( arguments );

	return {
		to () {
			const { resolvedPath: dest, options: writeOptions } = resolvePathAndOptions( arguments );

			const data = fs.readFileSync( src, readOptions );

			mkdirp.sync( dirname( dest ) );
			fs.writeFileSync( dest, data, writeOptions );
		}
	};
}