15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
 
const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const is = require('is-type-of');
const { loadTsConfig } = require('config-file-ts');
const JsonLib = require('json5');
 
const _basePath = process.cwd();
 
function checkConfig(prop) {
  const filepath = path.join(_basePath, prop);
  if (fs.existsSync(filepath)) {
    return true;
  }
 
  return false;
}
 
function loadConfig(prop) {
  const configFile = path.join(_basePath, prop);
  if (!fs.existsSync(configFile)) {
    const errorTips = 'config file ' + chalk.blue(`${configFile}`) + ' does not exist !';
    throw new Error(errorTips)
  }
 
  let result;
  if (configFile.endsWith(".json5") || configFile.endsWith(".json")) {
    const data = fs.readFileSync(configFile, 'utf8');
    return JsonLib.parse(data);
  }
  if (configFile.endsWith(".js") || configFile.endsWith(".cjs")) {
    result = require(configFile);
    if (result.default != null) {
      result = result.default;
    }
  } else if (configFile.endsWith(".ts")) {
    result = loadTsConfig(configFile);
  }
  if (is.function(result) && !is.class(result)) {
    result = result();
  }
  return result || {}
};
 
function loadEncryptConfig() {
  const configFile = './electron/config/encrypt.js';
  const filepath = path.join(_basePath, configFile);
  if (!fs.existsSync(filepath)) {
    const errorTips = 'config file ' + chalk.blue(`${filepath}`) + ' does not exist !';
    throw new Error(errorTips)
  }
  const obj = require(filepath);
  if (!obj) return obj;
 
  let ret = obj;
  if (is.function(obj) && !is.class(obj)) {
    ret = obj();
  }
 
  return ret || {};
};
 
/**
 * get electron program
 */
function getElectronProgram() {
  let electronPath
  const electronModulePath = path.dirname(require.resolve('electron'))
  const pathFile = path.join(electronModulePath, 'path.txt')
  const executablePath = fs.readFileSync(pathFile, 'utf-8')
  if (executablePath) {
    electronPath = path.join(electronModulePath, 'dist', executablePath)
  } else {
    throw new Error('Check that electron is installed!')
  }
  return electronPath;
};
 
/**
 * 版本号比较
 */
function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  const len = Math.max(v1.length, v2.length)
 
  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }
 
  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i])
    const num2 = parseInt(v2[i])
 
    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }
 
  return 0
}
 
function isWindows(prop) {
  return process.platform === 'win32'
}
 
module.exports = {
  loadConfig,
  checkConfig,
  loadEncryptConfig,
  getElectronProgram,
  compareVersion,
  isWindows
}