@@ -148,6 +148,18 @@ aeq.file = aeq.extend({}, {
148148 return filePathStr . substr ( 0 , filePathStr . lastIndexOf ( '.' ) ) ;
149149 } ,
150150
151+ /**
152+ * Takes a file path or a file object, and returns a file object
153+ * allows functions to be flexible in whether they take a path vs file
154+ * @method
155+ * @memberof aeq
156+ * @param {File|string } filePath String path to a file, or file object
157+ * @return {File } Resolved file object
158+ */
159+ getFileObject : function ( filePath ) {
160+ return aeq . isFile ( filePath ) ? filePath : new File ( filePath ) ;
161+ } ,
162+
151163 /**
152164 * Gets target file by path or file object, or null if doesn't exist
153165 * @method
@@ -185,6 +197,18 @@ aeq.file = aeq.extend({}, {
185197 return aeq . arrayEx ( files ) ;
186198 } ,
187199
200+ /**
201+ * Takes a folder path or a folder object, and returns a folder object
202+ * allows functions to be flexible in whether they take a path vs folder
203+ * @method
204+ * @memberof aeq
205+ * @param {Folder|string } folderPath String path to a folder, or folder object
206+ * @return {Folder } Resolved folder object
207+ */
208+ getFolderObject : function ( folderPath ) {
209+ return aeq . isFolder ( folderPath ) ? folderPath : new Folder ( folderPath ) ;
210+ } ,
211+
188212 /**
189213 * Returns a folder, or null if it doesn't exist
190214 * @method
@@ -216,13 +240,81 @@ aeq.file = aeq.extend({}, {
216240
217241 return folder ;
218242 } ,
243+
244+ /**
245+ * Returns the contents of a specified file
246+ * @method
247+ * @memberof aeq
248+ * @param {File|string } filePath Path or file to read
249+ * @param {string } [encoding=UTF-8] Encoding method
250+ * @return {string|null } Contents of the file, or null if file doesn't exist
251+ */
252+ readFile : function ( filePath , encoding ) {
253+ var file = aeq . getFileObject ( filePath ) ,
254+ contents ;
255+
256+ encoding = setDefault ( encoding , "UTF-8" ) ;
257+
258+ if ( file . exists ) {
259+ if ( File . isEncodingAvailable ( encoding ) )
260+ file . encoding = encoding ;
261+
262+ file . open ( ) ;
263+ contents = file . read ( ) ;
264+ file . close ( ) ;
265+ return contents ;
266+ }
267+ return null ;
268+ } ,
269+
270+ /**
271+ * Writes data to a file, returns file
272+ * @method
273+ * @memberof aeq
274+ * @param {File|string } filePath Path or file to write to
275+ * @param {string } contents Data to write to the file
276+ * @param {object } [options] Options for writing file.
277+ * @param {boolean } [options.overwrite=false] `true` if file should be overwritten if exists.
278+ * @param {string } [options.encoding="UTF-8"] Encoding method.
279+ * @return {File|null } New file, or null if file was not written
280+ * correctly or file exits and overwrite = false
281+ */
282+ writeFile : function ( filePath , contents , options ) {
283+ var file = aeq . getFileObject ( filePath ) ;
284+ options = aeq . setDefault ( options , { } ) ;
285+
286+ if ( file . exists && options . overwrite === false ) {
287+ return null
288+ }
289+
290+ if ( ! file . exists ) {
291+ aeq . file . ensureFolderExists ( file . path ) ;
292+ }
293+
294+ if ( ! aeq . isNullOrUndefined ( options . encoding ) && File . isEncodingAvailable ( options . encoding ) ) {
295+ file . encoding = options . encoding ;
296+ }
297+
298+ file . open ( "w" ) ;
299+ var success = file . write ( contents ) ;
300+ file . close ( ) ;
301+
302+ if ( success )
303+ return file ;
304+
305+ return null ;
306+ }
219307} ) ;
220308
221309// Function aliases
222310aeq . pathSeparatorSymbol = aeq . file . pathSeparatorSymbol ;
311+ aeq . getFileObject = aeq . file . getFileObject ;
312+ aeq . getFolderObject = aeq . file . getFolderObject ;
223313aeq . getFile = aeq . file . get = aeq . file . getFile ;
224314aeq . getFiles = aeq . file . getFiles ;
225315aeq . getFolder = aeq . file . getFolder ;
316+ aeq . readFile = aeq . file . readFile ;
317+ aeq . writeFile = aeq . file . writeFile ;
226318
227319return aeq ;
228320} ( aeq || { } ) ) ;
0 commit comments