ee-bin 4.1.8 → 4.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js CHANGED
@@ -1,224 +1,255 @@
1
- 'use strict';
2
-
3
- const debug = require('debug')('ee-bin:lib:utils');
4
- const path = require('path');
5
- const fs = require('fs');
6
- const chalk = require('chalk');
7
- const is = require('is-type-of');
8
- const { loadTsConfig } = require('config-file-ts');
9
- const JsonLib = require('json5');
10
- const mkdirp = require('mkdirp');
11
- const OS = require('os');
12
- const defaultConfig = require('../config/bin_default');
13
- const { extend } = require('./extend');
14
-
15
- const _basePath = process.cwd();
16
- const userBin = './cmd/bin.js';
17
-
18
- function loadConfig(binFile) {
19
- const binPath = binFile ? binFile : userBin;
20
- const userConfig = loadFile(binPath);
21
- const result = extend(true, defaultConfig, userConfig);
22
- debug('[loadConfig] bin:%j', result)
23
-
24
- return result
25
- };
26
-
27
- function loadFile(filepath) {
28
- const configFile = path.join(_basePath, filepath);
29
- if (!fs.existsSync(configFile)) {
30
- const errorTips = 'file ' + chalk.blue(`${configFile}`) + ' does not exist !';
31
- throw new Error(errorTips)
32
- }
33
-
34
- let result;
35
- if (configFile.endsWith(".json5") || configFile.endsWith(".json")) {
36
- const data = fs.readFileSync(configFile, 'utf8');
37
- return JsonLib.parse(data);
38
- }
39
- if (configFile.endsWith(".js") || configFile.endsWith(".cjs")) {
40
- result = require(configFile);
41
- if (result.default != null) {
42
- result = result.default;
43
- }
44
- } else if (configFile.endsWith(".ts")) {
45
- result = loadTsConfig(configFile);
46
- }
47
- if (is.function(result) && !is.class(result)) {
48
- result = result();
49
- }
50
- return result || {}
51
- };
52
-
53
- /**
54
- * get electron program
55
- */
56
- function getElectronProgram() {
57
- let electronPath
58
- const electronModulePath = path.dirname(require.resolve('electron'))
59
- const pathFile = path.join(electronModulePath, 'path.txt')
60
- const executablePath = fs.readFileSync(pathFile, 'utf-8')
61
- if (executablePath) {
62
- electronPath = path.join(electronModulePath, 'dist', executablePath)
63
- } else {
64
- throw new Error('Check that electron is installed!')
65
- }
66
- return electronPath;
67
- };
68
-
69
- /**
70
- * 版本号比较
71
- */
72
- function compareVersion(v1, v2) {
73
- v1 = v1.split('.')
74
- v2 = v2.split('.')
75
- const len = Math.max(v1.length, v2.length)
76
-
77
- while (v1.length < len) {
78
- v1.push('0')
79
- }
80
- while (v2.length < len) {
81
- v2.push('0')
82
- }
83
-
84
- for (let i = 0; i < len; i++) {
85
- const num1 = parseInt(v1[i])
86
- const num2 = parseInt(v2[i])
87
-
88
- if (num1 > num2) {
89
- return 1
90
- } else if (num1 < num2) {
91
- return -1
92
- }
93
- }
94
-
95
- return 0
96
- }
97
-
98
- function isWindows() {
99
- return process.platform === 'win32'
100
- }
101
-
102
- function isOSX() {
103
- return process.platform === 'darwin'
104
- }
105
-
106
- function isMacOS() {
107
- return isOSX()
108
- }
109
-
110
- function isLinux() {
111
- return process.platform === 'linux'
112
- }
113
-
114
- function isx86() {
115
- return process.arch === 'ia32'
116
- }
117
-
118
- function isx64() {
119
- return process.arch === 'x64'
120
- }
121
-
122
- /**
123
- * Delete a file or folder
124
- */
125
- function rm(name) {
126
- // check
127
- if (!fs.existsSync(name)) {
128
- return
129
- }
130
-
131
- const nodeVersion = (process.versions && process.versions.node) || null;
132
- if (nodeVersion && compareVersion(nodeVersion, '14.14.0') == 1) {
133
- fs.rmSync(name, {recursive: true});
134
- } else {
135
- fs.rmdirSync(name, {recursive: true});
136
- }
137
- }
138
-
139
- /**
140
- * 获取项目根目录package.json
141
- */
142
- function getPackage () {
143
- const content = readJsonSync(path.join(_basePath, 'package.json'));
144
- return content;
145
- }
146
-
147
- function readJsonSync (filepath, encoding = 'utf8') {
148
- if (!fs.existsSync(filepath)) {
149
- throw new Error(filepath + ' is not found');
150
- }
151
- return JSON.parse(fs.readFileSync(filepath, { encoding }));
152
- }
153
-
154
- function writeJsonSync (filepath, str, options) {
155
- options = options || {};
156
- if (!('space' in options)) {
157
- options.space = 2;
158
- }
159
-
160
- mkdirp.sync(path.dirname(filepath));
161
- if (typeof str === 'object') {
162
- str = JSON.stringify(str, options.replacer, options.space) + '\n';
163
- }
164
-
165
- fs.writeFileSync(filepath, str);
166
- };
167
-
168
- function getPlatform(delimiter = "_", isDiffArch = false) {
169
- let os = "";
170
- if (isWindows()) {
171
- os = "windows";
172
- if (isDiffArch) {
173
- const arch = isx64() ? "64" : "32";
174
- os += delimiter + arch;
175
- }
176
- } else if (isMacOS()) {
177
- let isAppleSilicon = false;
178
- const cpus = OS.cpus();
179
- for (let cpu of cpus) {
180
- if (cpu.model.includes('Apple')) {
181
- isAppleSilicon = true;
182
- break;
183
- }
184
- }
185
- const core = isAppleSilicon? "apple" : "intel";
186
- os = "macos" + delimiter + core;
187
- } else if (isLinux()) {
188
- os = "linux";
189
- }
190
-
191
- return os;
192
- }
193
-
194
- // Get cmd parameter by name
195
- function getArgumentByName(name, args) {
196
- if (!args) {
197
- args = process.argv;
198
- }
199
- for (let i = 0; i < args.length; i++) {
200
- const item = args[i];
201
- const prefixKey = `--${name}=`;
202
- if (item.indexOf(prefixKey) !== -1) {
203
- return item.substring(prefixKey.length);
204
- }
205
- }
206
- }
207
-
208
- module.exports = {
209
- loadConfig,
210
- getElectronProgram,
211
- compareVersion,
212
- isWindows,
213
- isOSX,
214
- isMacOS,
215
- isLinux,
216
- isx86,
217
- isx64,
218
- getPlatform,
219
- rm,
220
- getPackage,
221
- readJsonSync,
222
- writeJsonSync,
223
- getArgumentByName
224
- }
1
+ 'use strict';
2
+
3
+ const debug = require('debug')('ee-bin:lib:utils');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const chalk = require('chalk');
7
+ const is = require('is-type-of');
8
+ const { loadTsConfig } = require('config-file-ts');
9
+ const JsonLib = require('json5');
10
+ const mkdirp = require('mkdirp');
11
+ const OS = require('os');
12
+ const defaultConfig = require('../config/bin_default');
13
+ const { extend } = require('./extend');
14
+
15
+ const _basePath = process.cwd();
16
+ const userBin = './cmd/bin.js';
17
+
18
+ function loadConfig(binFile) {
19
+ const binPath = binFile ? binFile : userBin;
20
+ const userConfig = loadFile(binPath);
21
+ const result = extend(true, defaultConfig, userConfig);
22
+ debug('[loadConfig] bin:%j', result)
23
+
24
+ return result
25
+ };
26
+
27
+ function loadFile(filepath) {
28
+ const configFile = path.join(_basePath, filepath);
29
+ if (!fs.existsSync(configFile)) {
30
+ const errorTips = 'file ' + chalk.blue(`${configFile}`) + ' does not exist !';
31
+ throw new Error(errorTips)
32
+ }
33
+
34
+ let result;
35
+ if (configFile.endsWith(".json5") || configFile.endsWith(".json")) {
36
+ const data = fs.readFileSync(configFile, 'utf8');
37
+ return JsonLib.parse(data);
38
+ }
39
+ if (configFile.endsWith(".js") || configFile.endsWith(".cjs")) {
40
+ result = require(configFile);
41
+ if (result.default != null) {
42
+ result = result.default;
43
+ }
44
+ } else if (configFile.endsWith(".ts")) {
45
+ result = loadTsConfig(configFile);
46
+ }
47
+ if (is.function(result) && !is.class(result)) {
48
+ result = result();
49
+ }
50
+ return result || {}
51
+ };
52
+
53
+ /**
54
+ * get electron program
55
+ */
56
+ function getElectronProgram() {
57
+ let electronPath
58
+ const electronModulePath = path.dirname(require.resolve('electron'))
59
+ const pathFile = path.join(electronModulePath, 'path.txt')
60
+ const executablePath = fs.readFileSync(pathFile, 'utf-8')
61
+ if (executablePath) {
62
+ electronPath = path.join(electronModulePath, 'dist', executablePath)
63
+ } else {
64
+ throw new Error('Check that electron is installed!')
65
+ }
66
+ return electronPath;
67
+ };
68
+
69
+ /**
70
+ * 版本号比较
71
+ */
72
+ function compareVersion(v1, v2) {
73
+ v1 = v1.split('.')
74
+ v2 = v2.split('.')
75
+ const len = Math.max(v1.length, v2.length)
76
+
77
+ while (v1.length < len) {
78
+ v1.push('0')
79
+ }
80
+ while (v2.length < len) {
81
+ v2.push('0')
82
+ }
83
+
84
+ for (let i = 0; i < len; i++) {
85
+ const num1 = parseInt(v1[i])
86
+ const num2 = parseInt(v2[i])
87
+
88
+ if (num1 > num2) {
89
+ return 1
90
+ } else if (num1 < num2) {
91
+ return -1
92
+ }
93
+ }
94
+
95
+ return 0
96
+ }
97
+
98
+ function isWindows() {
99
+ return process.platform === 'win32'
100
+ }
101
+
102
+ function isOSX() {
103
+ return process.platform === 'darwin'
104
+ }
105
+
106
+ function isMacOS() {
107
+ return isOSX()
108
+ }
109
+
110
+ function isLinux() {
111
+ return process.platform === 'linux'
112
+ }
113
+
114
+ function isx86() {
115
+ return process.arch === 'ia32'
116
+ }
117
+
118
+ function isx64() {
119
+ return process.arch === 'x64'
120
+ }
121
+
122
+ /**
123
+ * Delete a file or folder
124
+ */
125
+ function rm(name) {
126
+ // check
127
+ if (!fs.existsSync(name)) {
128
+ return
129
+ }
130
+
131
+ const nodeVersion = (process.versions && process.versions.node) || null;
132
+ if (nodeVersion && compareVersion(nodeVersion, '14.14.0') == 1) {
133
+ fs.rmSync(name, {recursive: true});
134
+ } else {
135
+ fs.rmdirSync(name, {recursive: true});
136
+ }
137
+ }
138
+
139
+ /**
140
+ * 获取项目根目录package.json
141
+ */
142
+ function getPackage () {
143
+ const content = readJsonSync(path.join(_basePath, 'package.json'));
144
+ return content;
145
+ }
146
+
147
+ function readJsonSync (filepath, encoding = 'utf8') {
148
+ if (!fs.existsSync(filepath)) {
149
+ throw new Error(filepath + ' is not found');
150
+ }
151
+ return JSON.parse(fs.readFileSync(filepath, { encoding }));
152
+ }
153
+
154
+ function writeJsonSync (filepath, str, options) {
155
+ options = options || {};
156
+ if (!('space' in options)) {
157
+ options.space = 2;
158
+ }
159
+
160
+ mkdirp.sync(path.dirname(filepath));
161
+ if (typeof str === 'object') {
162
+ str = JSON.stringify(str, options.replacer, options.space) + '\n';
163
+ }
164
+
165
+ fs.writeFileSync(filepath, str);
166
+ };
167
+
168
+ function getPlatform(delimiter = "_", isDiffArch = false) {
169
+ let os = "";
170
+ if (isWindows()) {
171
+ os = "windows";
172
+ if (isDiffArch) {
173
+ const arch = isx64() ? "64" : "32";
174
+ os += delimiter + arch;
175
+ }
176
+ } else if (isMacOS()) {
177
+ let isAppleSilicon = false;
178
+ const cpus = OS.cpus();
179
+ for (let cpu of cpus) {
180
+ if (cpu.model.includes('Apple')) {
181
+ isAppleSilicon = true;
182
+ break;
183
+ }
184
+ }
185
+ const core = isAppleSilicon? "apple" : "intel";
186
+ os = "macos" + delimiter + core;
187
+ } else if (isLinux()) {
188
+ os = "linux";
189
+ }
190
+
191
+ return os;
192
+ }
193
+
194
+ // Get cmd parameter by name
195
+ function getArgumentByName(name, args) {
196
+ if (!args) {
197
+ args = process.argv;
198
+ }
199
+ for (let i = 0; i < args.length; i++) {
200
+ const item = args[i];
201
+ const prefixKey = `--${name}=`;
202
+ if (item.indexOf(prefixKey) !== -1) {
203
+ return item.substring(prefixKey.length);
204
+ }
205
+ }
206
+ }
207
+
208
+ function getExtraResourcesDir() {
209
+ const dir = path.join(_basePath, "build", "extraResources")
210
+ return dir;
211
+ }
212
+
213
+ function getModuleNameFromPath(modulePath) {
214
+ // 分割路径段(处理不同系统的分隔符)
215
+ const segments = path.normalize(modulePath).split(path.sep);
216
+
217
+ // 从后往前查找 node_modules
218
+ for (let i = segments.length - 1; i >= 0; i--) {
219
+ if (segments[i] === 'node_modules') {
220
+ // 普通模块:node_modules/dayjs
221
+ if (i + 1 < segments.length) {
222
+ return segments[i + 1];
223
+ }
224
+
225
+ // 作用域模块:node_modules/@scope/module
226
+ if (i + 2 < segments.length && segments[i + 1].startsWith('@')) {
227
+ return `${segments[i + 1]}/${segments[i + 2]}`;
228
+ }
229
+
230
+ break; // 找到 node_modules 但后面没有模块名
231
+ }
232
+ }
233
+
234
+ return null;
235
+ }
236
+
237
+ module.exports = {
238
+ loadConfig,
239
+ getElectronProgram,
240
+ compareVersion,
241
+ isWindows,
242
+ isOSX,
243
+ isMacOS,
244
+ isLinux,
245
+ isx86,
246
+ isx64,
247
+ getPlatform,
248
+ rm,
249
+ getPackage,
250
+ readJsonSync,
251
+ writeJsonSync,
252
+ getArgumentByName,
253
+ getExtraResourcesDir,
254
+ getModuleNameFromPath
255
+ }
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "ee-bin",
3
- "version": "4.1.8",
4
- "description": "ee bin",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "author": "",
10
- "license": "ISC",
11
- "bin": {
12
- "ee-bin": "index.js"
13
- },
14
- "dependencies": {
15
- "adm-zip": "^0.4.11",
16
- "bytenode": "^1.3.6",
17
- "chalk": "^4.1.2",
18
- "chokidar": "^4.0.3",
19
- "commander": "^11.0.0",
20
- "config-file-ts": "^0.2.8-rc1",
21
- "cross-spawn": "^7.0.3",
22
- "debug": "^4.4.0",
23
- "esbuild": "^0.21.5",
24
- "fs-extra": "^10.0.0",
25
- "globby": "^10.0.0",
26
- "is-type-of": "^1.2.1",
27
- "javascript-obfuscator": "^4.0.2",
28
- "json5": "^2.2.3",
29
- "mkdirp": "^2.1.3",
30
- "tree-kill": "^1.2.2"
31
- }
32
- }
1
+ {
2
+ "name": "ee-bin",
3
+ "version": "4.1.10",
4
+ "description": "ee bin",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC",
11
+ "bin": {
12
+ "ee-bin": "index.js"
13
+ },
14
+ "dependencies": {
15
+ "adm-zip": "^0.4.11",
16
+ "bytenode": "^1.3.6",
17
+ "chalk": "^4.1.2",
18
+ "chokidar": "^4.0.3",
19
+ "commander": "^11.0.0",
20
+ "config-file-ts": "^0.2.8-rc1",
21
+ "cross-spawn": "^7.0.3",
22
+ "debug": "^4.4.0",
23
+ "esbuild": "^0.21.5",
24
+ "fs-extra": "^10.0.0",
25
+ "globby": "^10.0.0",
26
+ "is-type-of": "^1.2.1",
27
+ "javascript-obfuscator": "^4.0.2",
28
+ "json5": "^2.2.3",
29
+ "mkdirp": "^2.1.3",
30
+ "tree-kill": "^1.2.2"
31
+ }
32
+ }