15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ 'use strict' ;
19
+
18
20
var fs = require ( 'fs' ) ,
19
21
path = require ( 'path' ) ,
20
22
rimraf = require ( 'rimraf' ) ,
21
23
tmp = require ( 'tmp' ) ;
22
24
23
- var promise = require ( '..' ) . promise ;
24
-
25
25
26
26
27
27
// PUBLIC API
@@ -32,11 +32,11 @@ var promise = require('..').promise;
32
32
* Recursively removes a directory and all of its contents. This is equivalent
33
33
* to {@code rm -rf} on a POSIX system.
34
34
* @param {string } path Path to the directory to remove.
35
- * @return {!promise. Promise } A promise to be resolved when the operation has
35
+ * @return {!Promise } A promise to be resolved when the operation has
36
36
* completed.
37
37
*/
38
38
exports . rmDir = function ( path ) {
39
- return new promise . Promise ( function ( fulfill , reject ) {
39
+ return new Promise ( function ( fulfill , reject ) {
40
40
var numAttempts = 0 ;
41
41
attemptRm ( ) ;
42
42
function attemptRm ( ) {
@@ -61,23 +61,19 @@ exports.rmDir = function(path) {
61
61
* Copies one file to another.
62
62
* @param {string } src The source file.
63
63
* @param {string } dst The destination file.
64
- * @return {!promise. Promise. <string> } A promise for the copied file's path.
64
+ * @return {!Promise<string> } A promise for the copied file's path.
65
65
*/
66
66
exports . copy = function ( src , dst ) {
67
- var copied = promise . defer ( ) ;
68
-
69
- var rs = fs . createReadStream ( src ) ;
70
- rs . on ( 'error' , copied . reject ) ;
71
- rs . on ( 'end' , function ( ) {
72
- copied . fulfill ( dst ) ;
73
- } ) ;
67
+ return new Promise ( function ( fulfill , reject ) {
68
+ var rs = fs . createReadStream ( src ) ;
69
+ rs . on ( 'error' , reject ) ;
70
+ rs . on ( 'end' , ( ) => fulfill ( dst ) ) ;
74
71
75
- var ws = fs . createWriteStream ( dst ) ;
76
- ws . on ( 'error' , copied . reject ) ;
72
+ var ws = fs . createWriteStream ( dst ) ;
73
+ ws . on ( 'error' , reject ) ;
77
74
78
- rs . pipe ( ws ) ;
79
-
80
- return copied . promise ;
75
+ rs . pipe ( ws ) ;
76
+ } ) ;
81
77
} ;
82
78
83
79
@@ -88,7 +84,7 @@ exports.copy = function(src, dst) {
88
84
* @param {(RegEx|function(string): boolean)= } opt_exclude An exclusion filter
89
85
* as either a regex or predicate function. All files matching this filter
90
86
* will not be copied.
91
- * @return {!promise. Promise. <string> } A promise for the destination
87
+ * @return {!Promise<string> } A promise for the destination
92
88
* directory's path once all files have been copied.
93
89
*/
94
90
exports . copyDir = function ( src , dst , opt_exclude ) {
@@ -128,32 +124,30 @@ exports.copyDir = function(src, dst, opt_exclude) {
128
124
}
129
125
} ) ;
130
126
131
- return promise . all ( results ) . then ( function ( ) {
132
- return dst ;
133
- } ) ;
127
+ return Promise . all ( results ) . then ( ( ) => dst ) ;
134
128
} ;
135
129
136
130
137
131
/**
138
132
* Tests if a file path exists.
139
133
* @param {string } path The path to test.
140
- * @return {!promise. Promise.<boolean> } A promise for whether the file exists.
134
+ * @return {!Promise.<boolean> } A promise for whether the file exists.
141
135
*/
142
136
exports . exists = function ( path ) {
143
- var result = promise . defer ( ) ;
144
- fs . exists ( path , result . fulfill ) ;
145
- return result . promise ;
137
+ return new Promise ( function ( fulfill ) {
138
+ fs . exists ( path , fulfill ) ;
139
+ } ) ;
146
140
} ;
147
141
148
142
149
143
/**
150
144
* Deletes a name from the filesystem and possibly the file it refers to. Has
151
145
* no effect if the file does not exist.
152
146
* @param {string } path The path to remove.
153
- * @return {!promise. Promise } A promise for when the file has been removed.
147
+ * @return {!Promise } A promise for when the file has been removed.
154
148
*/
155
149
exports . unlink = function ( path ) {
156
- return new promise . Promise ( function ( fulfill , reject ) {
150
+ return new Promise ( function ( fulfill , reject ) {
157
151
fs . exists ( path , function ( exists ) {
158
152
if ( exists ) {
159
153
fs . unlink ( path , function ( err ) {
@@ -168,27 +162,37 @@ exports.unlink = function(path) {
168
162
169
163
170
164
/**
171
- * @return {!promise.Promise.<string> } A promise for the path to a temporary
172
- * directory.
165
+ * @return {!Promise<string> } A promise for the path to a temporary directory.
173
166
* @see https://www.npmjs.org/package/tmp
174
167
*/
175
168
exports . tmpDir = function ( ) {
176
- return promise . checkedNodeCall ( tmp . dir ) ;
169
+ return new Promise ( function ( fulfill , reject ) {
170
+ tmp . dir ( function ( error , value ) {
171
+ error ? reject ( error ) : fulfill ( value ) ;
172
+ } ) ;
173
+ } ) ;
177
174
} ;
178
175
179
176
180
177
/**
181
178
* @param {{postfix: string}= } opt_options Temporary file options.
182
- * @return {!promise.Promise.<string> } A promise for the path to a temporary
183
- * file.
179
+ * @return {!Promise<string> } A promise for the path to a temporary file.
184
180
* @see https://www.npmjs.org/package/tmp
185
181
*/
186
182
exports . tmpFile = function ( opt_options ) {
187
- // |tmp.file| checks arguments length to detect options rather than doing a
188
- // truthy check, so we must only pass options if there are some to pass.
189
- return opt_options ?
190
- promise . checkedNodeCall ( tmp . file , opt_options ) :
191
- promise . checkedNodeCall ( tmp . file ) ;
183
+ return new Promise ( function ( fulfill , reject ) {
184
+ let callback = function ( err , value ) {
185
+ err ? reject ( err ) : fulfill ( value ) ;
186
+ } ;
187
+
188
+ // |tmp.file| checks arguments length to detect options rather than doing a
189
+ // truthy check, so we must only pass options if there are some to pass.
190
+ if ( opt_options ) {
191
+ tmp . file ( opt_options , callback ) ;
192
+ } else {
193
+ tmp . file ( callback ) ;
194
+ }
195
+ } ) ;
192
196
} ;
193
197
194
198
0 commit comments