@@ -154,6 +154,111 @@ function _setupVFS(fsLib, pathLib){
154154 } ) ;
155155 } ) ;
156156 } ,
157+ /**
158+ * Deletes a file/dir asynchronously. resolves on success or rejects on error.
159+ *
160+ * @function
161+ * @param {string } filePath - The path of the file/dir to be deleted.
162+ * @returns {Promise<Object> } A promise that resolves on success or rejects on error.
163+ */
164+ unlinkAsync : async function ( filePath ) {
165+ return new Promise ( ( resolve , reject ) => {
166+ fs . unlink ( filePath , ( err ) => {
167+ if ( err ) {
168+ reject ( err ) ;
169+ } else {
170+ resolve ( ) ;
171+ }
172+ } ) ;
173+ } ) ;
174+ } ,
175+ /**
176+ * deletes a file/dir asynchronously, always resolves, never rejects.
177+ *
178+ * @function
179+ * @param {string } filePath - The path of the file/dir to be deleted.
180+ * @returns {Promise<Object> } A promise that resolves to an object containing either
181+ * an `error` property if there is an error, or just {} on success.
182+ */
183+ unlinkResolves : async function ( filePath ) {
184+ return new Promise ( ( resolve ) => {
185+ fs . unlink ( filePath , ( error ) => {
186+ if ( error ) {
187+ resolve ( { error : error } ) ;
188+ return ;
189+ }
190+ resolve ( { } ) ;
191+ } ) ;
192+ } ) ;
193+ } ,
194+ /**
195+ * Reads the contents of a file asynchronously, always resolves, never rejects.
196+ * Mainly use to read config and other files.
197+ * This should not be used for reading project files that are being edited, for that use file system APIs
198+ * as that apis will be able to deal with files being edited in the editor.
199+ *
200+ * @function
201+ * @param {string } filePath - The path of the file to be read.
202+ * @param {string } encoding - The encoding to use for reading the file.
203+ * @returns {Promise<Object> } A promise that resolves to an object containing either
204+ * an `error` property if there is an error, or a `data` property with the file contents.
205+ */
206+ readFileResolves : function ( filePath , encoding ) {
207+ return new Promise ( ( resolve ) => {
208+ fs . readFile ( filePath , encoding , function ( error , data ) {
209+ if ( error ) {
210+ resolve ( { error : error } ) ;
211+ return ;
212+ }
213+ resolve ( { data : data } ) ;
214+ } ) ;
215+ } ) ;
216+ } ,
217+ /**
218+ * Reads the contents of a file asynchronously, resolves with content or rejects with error.
219+ * Mainly use to read config and other files.
220+ * This should not be used for reading project files that are being edited, for that use file system APIs
221+ * as that apis will be able to deal with files being edited in the editor.
222+ *
223+ * @param {string } filePath - The path to the file to be read.
224+ * @param {string } encoding - The encoding format to use when reading the file.
225+ * @returns {Promise<string> } A promise that resolves with the file data when the read is successful,
226+ * or rejects with an error if the read operation fails.
227+ */
228+ readFileAsync : function ( filePath , encoding ) {
229+ return new Promise ( ( resolve , reject ) => {
230+ fs . readFile ( filePath , encoding , function ( error , data ) {
231+ if ( error ) {
232+ reject ( error ) ;
233+ return ;
234+ }
235+ resolve ( data ) ;
236+ } ) ;
237+ } ) ;
238+ } ,
239+ /**
240+ * Asynchronously writes data to a file, replacing the file if it already exists.
241+ * Mainly use to write config and other files.
242+ * This should not be used for write project files that are being edited, for that use file system APIs
243+ * as that apis will be able to deal with files being edited in the editor.
244+ *
245+ * @param {string } filePath - The path of the file where the data should be written.
246+ * @param {string } content - The data to write into the file.
247+ * @param {string } encoding - The character encoding to use when writing the file.
248+ * @returns {Promise<void> } A promise that resolves when the file has been successfully written,
249+ * or rejects with an error if the operation fails.
250+ */
251+ writeFileAsync : function ( filePath , content , encoding ) {
252+ return new Promise ( ( resolve , reject ) => {
253+ fs . writeFile ( filePath , content , encoding , ( err ) => {
254+ if ( err ) {
255+ reject ( err ) ;
256+ } else {
257+ resolve ( ) ;
258+ }
259+ } ) ;
260+ } ) ;
261+ } ,
157262 fs : fsLib ,
158263 path : pathLib
159264 } ;
0 commit comments