@aiot-toolkit/packager
Version:
@aiot-toolkit/packager
3 lines (2 loc) • 4.09 kB
JavaScript
;var _http=_interopRequireDefault(require("http")),_fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_shelljs=_interopRequireDefault(require("shelljs")),_compilationConfig=require("@aiot-toolkit/shared-utils/compilation-config"),_sharedUtils=require("@aiot-toolkit/shared-utils"),_recordClient=require("@aiot-toolkit/shared-utils/lib/record-client"),_config=_interopRequireDefault(require("@aiot-toolkit/shared-utils/config")),_wsserver=require("../router/wsserver"),_child_process=require("child_process");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const{getManifestInfo:getManifestInfo}=require("../common/info");let clientExists=!1,isFirstUpdate=!0,isStartupVela=!0;function sendUpdateReq(e){const t=`http://${e.ip}:${e.port}/update`,o={host:e.ip,port:e.port,path:"/update",timeout:3e3},i=_http.default.request(o,(()=>{_sharedUtils.colorconsole.log(`### App Server ### Notify the mobile phone to update the rpk file successfully: ${t} `)})).on("error",(e=>{_sharedUtils.colorconsole.log(`### App Server ### Notify the phone to update the rpk file failed(can be ignored): ${t} error message: ${e.message}`)})).on("timeout",(function(){_sharedUtils.colorconsole.log(`### App Server ### Notify the phone to update the rpk file timeout(can be ignored): ${t}`),i.abort()}));i.end()}function getTargetNuttx(e,t="default"){try{const o=_path.default.resolve(e,"./version.json");let i=require(o),r=i[t];r||_sharedUtils.colorconsole.error(`'${t}' is not supported, the supported simulationVersions are ${Object.keys(i).join(",")}`);let s=_path.default.resolve(e,r),l=_path.default.resolve(s,"nuttx");if(r&&_fs.default.existsSync(l))return s}catch(e){"default"!==t&&_sharedUtils.colorconsole.error(`### App Server ### You need to update @aiot-toolkit/velasim if you want use simulationVersion ${e.message}`)}return e}function handleVelaUpdate(){try{const{enableCustomComponent:e}=_compilationConfig.compileOptionsObject,{package:t,simulationVersion:o}=getManifestInfo(),i=_path.default.resolve(_config.default.projectPath,_config.default.outputPath,"./*"),r=getTargetNuttx(_path.default.resolve(_config.default.projectPath,"./node_modules/@aiot-toolkit/velasim/"),o),s=_path.default.resolve(r,"./app/",t);if(_shelljs.default.find(s).toString()||_shelljs.default.mkdir("-p",s),_shelljs.default.cp("-rf",i,s),isStartupVela){const o=`${(0,_sharedUtils.getIPv4IPAddress)()}:${_wsserver.velaSocket&&_wsserver.velaSocket.serverPort||_config.default.server.port}`;_compilationConfig.compileOptionsObject.openNuttx?(_shelljs.default.cd(`${r}`),(0,_child_process.spawn)("./nuttx",[`--debug=${o}`,""+(e?"--component":""),`app/${t}`],{stdio:"inherit"})):_sharedUtils.colorconsole.warn(`### App Server ### If you are debugging vela application, please try execute the following command:\n 1、cd ${r} \n 2、./nuttx --debug=${o} app/${t}`)}else _wsserver.velaSocket&&_wsserver.velaSocket.send("update")}catch(e){_sharedUtils.colorconsole.error("### App Server ### handleVelaUpdate error:",e.message)}}function notify(){const e=_config.default.clientRecordPath;if(_fs.default.existsSync(e)){const t=(0,_recordClient.getRecords)(e),o=(0,_recordClient.getProjectClients)(t);o.length>0&&(_sharedUtils.colorconsole.log("### App Loader ### Notify the phone to update the rpk file"),o.forEach((function(e){"127.0.0.1"!==e.ip?sendUpdateReq(e):(0,_sharedUtils.getDeviceInfo)(e,(t=>{t||sendUpdateReq(e)}))})),clientExists=!0)}clientExists||_sharedUtils.colorconsole.log("### App Server ### The phone address is not recorded, and the phone will not be notified to update the rpk file")}function NotifyPlugin(e){this.options=e,e.doNotNotifyAtFirst&&(isFirstUpdate=!1)}module.exports=notify,NotifyPlugin.prototype.apply=function(e){e.hooks.done.tapAsync("NotifyPlugin",(function(t,o){!t.compilation.errors.length&&isFirstUpdate&&(_compilationConfig.compileOptionsObject.enableMirtos?e.watchMode&&handleVelaUpdate():notify()),isFirstUpdate=!0,isStartupVela=!1,o()}))},module.exports=NotifyPlugin;
//# sourceMappingURL=notify-plugin.js.map