"use strict";var e=this&&this.__awaiter||function(e,r,t,i){return new(t||(t=Promise))((function(n,o){function a(e){try{d(i.next(e))}catch(e){o(e)}}function s(e){try{d(i.throw(e))}catch(e){o(e)}}function d(e){var r;e.done?n(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(a,s)}d((i=i.apply(e,r||[])).next())}))},r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.FsUtil=void 0;const t=r(require("fs-extra")),i=require("../tools/symlink/SymlinkDir"),n=r(require("../log")),o=require("graceful-fs"),a=require("../tools/json5"),s=require("./FsBlockingUtil"),d=require("../tools/ohfs"),u=require("./is-windows"),l=require("../tools/safe-rename"),c=require("./handleReadJsonError"),f=require("../common/message"),m=r(require("tar"));function h(r){return e(this,void 0,void 0,(function*(){try{yield t.default.mkdirp(r)}catch(e){if("EEXIST"===e.code)return;throw e}}))}function y(r){return e(this,void 0,void 0,(function*(){let e=[];try{e=yield o.promises.readdir(r)}catch(e){if("ENOENT"===e.code)return[];throw e}return e}))}exports.FsUtil={remove:e=>d.remove.async(e),realpath:t.default.realpath,slash:e=>function(e){return e.startsWith("\\\\?\\")?e:e.replace(/\\/g,"/")}(e),rm:o.promises.rm,symLinkDir:function(r,t){return e(this,void 0,void 0,(function*(){try{const e=yield i.Symlink.symlink(r,t,{overwrite:!0});e.reused||n.default.debug("symlink",`${t} to ${r}`),e.warn&&n.default.warn("symlink",e.warn)}catch(e){throw new Error((0,f.format)(f.Messages.File.SymLinkDirFailed,{linkDirPath:t,realDirPath:r,msg:(null==e?void 0:e.message)||e}))}}))},createFile:t.default.createFile,writeFile:function(r,t,i){return e(this,void 0,void 0,(function*(){r&&t&&(t=t.replace(/[\r\n]+/gm,u.WindowsUtil.isWindows?"\r\n":"\n"),yield o.promises.writeFile(r,t,i))}))},createDirIfNotExists:function(r){return e(this,void 0,void 0,(function*(){if(!(yield t.default.pathExists(r)))try{yield h(r)}catch(e){n.default.warn("",`createDir exception: ${e}, start remove dirPath: ${r}`),yield d.remove.async(r),yield h(r)}}))},renameIfNotExist:function(r,i){return e(this,void 0,void 0,(function*(){if(!(yield t.default.pathExists(i)))try{yield l.safeRename.async(r,i)}catch(e){if(yield t.default.pathExists(i))return n.default.debug("ERENAME",`the targetPath: ${i} already exists`),void s.FsBlockingUtil.rmDirNoThrowError(r);throw n.default.error("ERENAME",`unknown error occurred when rename ${r} -> ${i}, error: ${e}`),e}}))},rmEmptyDir:function(r){return e(this,void 0,void 0,(function*(){if(!(yield t.default.pathExists(r)))return;if(0===(yield y(r)).length){try{yield o.promises.rmdir(r)}catch(e){if("ENOENT"===e.code)return;throw e}n.default.debug("",`Deleted empty directory ${r}`)}}))},rmDir:o.promises.rmdir,readdir:o.promises.readdir,readdirSilently:y,mkdirIfNotExists:h,exists:t.default.pathExists,extractAndIgnoreTargetFolder:function(r,t,i,o=1){return e(this,void 0,void 0,(function*(){try{yield m.default.x({file:r,cwd:t,filter:(e,r)=>!e.startsWith(`${i}/`),strip:o}),n.default.debug("",`Extraction ${r} complete.`)}catch(e){throw n.default.error("",`An error occurred during extraction ${r}, detail: ${e}`),e}}))},stat:o.promises.stat,readModifyTime:function(r){return e(this,void 0,void 0,(function*(){try{return(yield t.default.stat(r)).mtimeMs.toString()}catch(e){throw n.default.error("",`can not read modify time from "${r}", detail: ${e}`),e}}))},readFile:o.promises.readFile,copyFile:o.promises.copyFile,copy:t.default.copy,readlink:o.promises.readlink,unlink:o.promises.unlink,rename:l.safeRename.async,readJSON:function(e,r){try{return t.default.readJSON(e,r)}catch(r){const t=`${r.message} - check ${e} format.`;throw n.default.error("",t),new Error(t)}},readJSON5:function(r,t=""){return e(this,void 0,void 0,(function*(){try{const e=yield o.promises.readFile(r,"utf8");return a.JSON5.parse(e)}catch(e){(0,c.handleReadJsonError)(e,r)}}))}};