service.js 2.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: !0
  }), exports.getProjectName = getProjectName, exports.getServerAddress = getServerAddress, exports.getClientFromRequest =
  getClientFromRequest, exports.getDistFilePath = getDistFilePath, exports.serverConf = serverConf, exports.LINK_MODE =
  void 0;
var _path = _interopRequireDefault(require("path")),
  _fs = _interopRequireDefault(require("fs")),
  _sharedUtils = require("@hap-toolkit/shared-utils");

function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}
const LINK_MODE = {
  NULL: 0,
  WIFI: 1,
  ADB: 2
};

function getProjectName(e, t) {
  try {
fxy060608's avatar
fxy060608 已提交
24 25 26
    const stripJsonComments = require('strip-json-comments');
    const r = _path.default.join(e, t, "manifest.json");
    const s = JSON.parse(stripJsonComments(_fs.default.readFileSync(r).toString()))
fxy060608's avatar
fxy060608 已提交
27
    return s && s['quickapp-vue'] && s['quickapp-vue']['package'] || s.name || "Bundle"
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  } catch (e) {
    _sharedUtils.colorconsole.error(`### App Server ### 获取项目名称出错:${e.message}`)
  }
}

function getServerAddress(e) {
  return `http://${(0,_sharedUtils.getIPv4IPAddress)()}${80===e?"":":"+e}`
}

function getClientFromRequest(e) {
  const t = (0, _sharedUtils.getClientIPAddress)(e),
    r = (0, _sharedUtils.getIPv4IPAddress)(),
    s = e.header["device-serial-number"];
  let i = LINK_MODE.NULL;
  return "127.0.0.1" === t && s ? i = LINK_MODE.ADB : "127.0.0.1" !== t && t !== r && (i = LINK_MODE.WIFI), {
    clientIp: t,
    sn: s,
    linkMode: i
  }
}

function getDistFilePath(e, t, r) {
  let s;
  if (!process.env.UNI_OUTPUT_DIR) {
    const mode = process.env.NODE_ENV === 'production' ? 'build' : 'dev'
fxy060608's avatar
fxy060608 已提交
53
    process.env.UNI_OUTPUT_DIR = _path.default.join(process.cwd(), 'dist/' + mode + '/quickapp-vue')
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63
  }
  return s = _path.default.join(process.env.UNI_OUTPUT_DIR, `${t}.debug.${r}`), _fs.default.existsSync(s) ? s : (s =
    _path.default.join(e,
      `${t}.release.${r}`), _fs.default.existsSync(s) ? s : void 0)
}

function serverConf(e) {
  return e.conf
}
exports.LINK_MODE = LINK_MODE;