@@ -52,11 +52,25 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
52
52
}
53
53
} ;
54
54
55
- const cpFile = ( sourcePath , destinationPath , options ) => {
55
+ const resolvePath = ( cwd , sourcePath , destinationPath ) => {
56
+ sourcePath = path . resolve ( cwd , sourcePath ) ;
57
+ destinationPath = path . resolve ( cwd , destinationPath ) ;
58
+
59
+ return {
60
+ sourcePath,
61
+ destinationPath
62
+ } ;
63
+ } ;
64
+
65
+ const cpFile = ( sourcePath , destinationPath , options = { } ) => {
56
66
if ( ! sourcePath || ! destinationPath ) {
57
67
return Promise . reject ( new CpFileError ( '`source` and `destination` required' ) ) ;
58
68
}
59
69
70
+ if ( options . cwd ) {
71
+ ( { sourcePath, destinationPath} = resolvePath ( options . cwd , sourcePath , destinationPath ) ) ;
72
+ }
73
+
60
74
options = {
61
75
overwrite : true ,
62
76
...options
@@ -85,23 +99,27 @@ const checkSourceIsFile = (stat, source) => {
85
99
}
86
100
} ;
87
101
88
- module . exports . sync = ( source , destination , options ) => {
89
- if ( ! source || ! destination ) {
102
+ module . exports . sync = ( sourcePath , destinationPath , options = { } ) => {
103
+ if ( ! sourcePath || ! destinationPath ) {
90
104
throw new CpFileError ( '`source` and `destination` required' ) ;
91
105
}
92
106
107
+ if ( options . cwd ) {
108
+ ( { sourcePath, destinationPath} = resolvePath ( options . cwd , sourcePath , destinationPath ) ) ;
109
+ }
110
+
93
111
options = {
94
112
overwrite : true ,
95
113
...options
96
114
} ;
97
115
98
- const stat = fs . statSync ( source ) ;
99
- checkSourceIsFile ( stat , source ) ;
100
- fs . makeDirSync ( path . dirname ( destination ) , { mode : options . directoryMode } ) ;
116
+ const stat = fs . statSync ( sourcePath ) ;
117
+ checkSourceIsFile ( stat , sourcePath ) ;
118
+ fs . makeDirSync ( path . dirname ( destinationPath ) , { mode : options . directoryMode } ) ;
101
119
102
120
const flags = options . overwrite ? null : fsConstants . COPYFILE_EXCL ;
103
121
try {
104
- fs . copyFileSync ( source , destination , flags ) ;
122
+ fs . copyFileSync ( sourcePath , destinationPath , flags ) ;
105
123
} catch ( error ) {
106
124
if ( ! options . overwrite && error . code === 'EEXIST' ) {
107
125
return ;
@@ -110,5 +128,5 @@ module.exports.sync = (source, destination, options) => {
110
128
throw error ;
111
129
}
112
130
113
- fs . utimesSync ( destination , stat . atime , stat . mtime ) ;
131
+ fs . utimesSync ( destinationPath , stat . atime , stat . mtime ) ;
114
132
} ;
0 commit comments