"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.zipToBuffer=exports.zipFile=void 0,exports.initEnvironmentByProject=initEnvironmentByProject;const tslib_1=require("tslib"),fs_1=tslib_1.__importDefault(require("fs")),path_1=tslib_1.__importDefault(require("path")),jszip_1=tslib_1.__importDefault(require("jszip")),minimatch_1=tslib_1.__importDefault(require("minimatch")),cloudapi_1=require("./cloudapi"),log=tslib_1.__importStar(require("../../utils/log")),cloudAPI=tslib_1.__importStar(require("../../common/cloud-api")),minimatchWithList=(e,t)=>{if(!t)return!1;for(const i of t)if(i.startsWith("!")&&!(0,minimatch_1.default)(e,i))return!1;for(const i of t)if(!i.startsWith("!")&&(0,minimatch_1.default)(e,i))return!0;return!1},zipFile=(e,{ignore:t,name:i,zip:r=new jszip_1.default,includeRoot:n=!1}={})=>{if(!fs_1.default.existsSync(e))return r;const o=fs_1.default.lstatSync(e),s=i||path_1.default.basename(e);if(o.isSymbolicLink())r.file(s,fs_1.default.readlinkSync(e),{unixPermissions:parseInt("120"+parseInt((o.mode&parseInt("777",8)).toString(8),10),8)});else if(o.isDirectory()){n&&r.folder(s);const i=fs_1.default.readdirSync(e);for(let o=0,a=i.length;o<a;o++){const a=i[o],l=n?path_1.default.posix.join(s,a):a;t&&minimatchWithList(l,t)||(0,exports.zipFile)(path_1.default.join(e,a),{zip:r,ignore:t,name:l,includeRoot:!0})}}else r.file(s,fs_1.default.readFileSync(e),{binary:!0,unixPermissions:o.mode});return r};exports.zipFile=zipFile;const zipToBuffer=(e,t)=>e.generateAsync({type:"nodebuffer",platform:"darwin"===process.platform?"UNIX":"DOS",compression:"DEFLATE",compressionOptions:{level:9}},t);async function initEnvironmentByProject(e,t){const i=await e.getExtAppid();(0,cloudapi_1.initCloudAPI)(i||e.appid),log.info("[cloud] loading env info");const r={request:(0,cloudapi_1.boundTransactRequest)(e),transactType:cloudAPI.TransactType.IDE};cloudAPI.setRequest((0,cloudapi_1.boundTransactRequest)(e)),cloudAPI.setTransactType(cloudAPI.TransactType.IDE);const[n,o]=await Promise.all([cloudAPI.tcbGetEnvironments({},r),cloudAPI.tcbDescribeWxCloudBaseRunEnvs({})]),s=[...n.envList,...o.envList].find(e=>e.envId===t);if(!s)throw new Error("env not found");return{currentEnv:s,cloudAPI:cloudAPI}}exports.zipToBuffer=zipToBuffer;