@pixui-dev/pxw 0.1.24 → 0.1.25

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/bin/pxw.js CHANGED
@@ -1,9 +1,6 @@
1
1
  #!/usr/bin/env node
2
- let fs = require('fs');
3
- let cp = require('child_process');
4
2
  let path = require('path');
5
3
  let http = require('http');
6
- let os = require('os');
7
4
  let util = require('../config/util');
8
5
  let pfbs = require('../config/pfbs');
9
6
  let webpack = require('../config/webpack');
@@ -11,16 +8,11 @@ let argv = require('yargs').argv;
11
8
 
12
9
  let runInWindows = process.platform == 'win32';
13
10
  let pr = path.resolve;
14
- let log = console.log;
15
- log = () => {};
16
11
 
17
12
  let rootDir = pr(__dirname, '../');
18
- let testHtmlDir = pr(__dirname, '../pxtest/html');
19
13
  let projectConfig = util.loadProjectConfig();
20
14
  let appPort = parseInt(process.env['PXW_APP_PORT']) || projectConfig.port || 8001;
21
15
 
22
- let isDaemonMode = argv.d;
23
-
24
16
  process
25
17
  .on('unhandledRejection', (err) => {
26
18
  console.log('unhandledRejection', err);
@@ -30,19 +22,17 @@ process
30
22
  });
31
23
 
32
24
  async function main() {
33
- let fetch = require('node-fetch');
34
25
  let express = require('express');
35
- let serveIndex = require('serve-index');
36
26
 
37
27
  let app = express();
38
28
  let server = http.createServer(app);
39
29
 
40
- let { notifyAppReload } = await require('../config/devops').setupDevops(server, app);
41
-
42
30
  app.use(express.json());
43
31
  app.use(pfbs.getMiddleware());
44
- app.use(webpack.devServer.getMiddleware()(appPort, notifyAppReload));
45
- app.use('/pxtest', serveIndex(testHtmlDir), express.static(testHtmlDir));
32
+
33
+ let { notifyAppReload } = await require('../config/devops').setupDevops(server, app);
34
+
35
+ if (!argv['no-webpack']) app.use(webpack.devServer.getMiddleware()(appPort, notifyAppReload));
46
36
 
47
37
  app.use(function (err, req, res, next) {
48
38
  console.error(err);
@@ -57,136 +47,10 @@ async function main() {
57
47
  });
58
48
  }
59
49
 
60
- //以下处理自动重启和单例
61
- function restart() {
62
- const scriptPath = process.argv[1];
63
- let removeArgs = { '--fg': 1 };
64
- let args = process.argv.slice(2);
65
- args = args.filter((v) => !removeArgs[v]);
66
- const subprocess = cp.spawn(process.execPath, [scriptPath, ...args], {
67
- detached: true,
68
- windowsHide: true,
69
- });
70
- subprocess.unref();
71
- process.exit();
72
- }
73
-
74
- function watchSelf() {
75
- //可以通过环境变量设置是否要监视重启
76
- let restartWatchFile = process.env['PXW_WATCHFILE'];
77
- if (restartWatchFile == 'none') return;
78
- if (!restartWatchFile) restartWatchFile = __filename;
79
- restartWatchFile = pr(restartWatchFile);
80
- console.log('watchfile:', restartWatchFile);
81
- let chokidar = require('chokidar');
82
- let watcher = chokidar.watch(restartWatchFile, { ignored: /^\./, persistent: true });
83
- watcher
84
- .on('change', function (path) {
85
- console.log(`pid[${process.pid}] observing '${path}' changed, will restart!`);
86
- restart();
87
- })
88
- .on('error', function (error) {
89
- console.error('watchSelf error:', error);
90
- });
91
- }
92
-
50
+ let isDaemonMode = argv.d;
93
51
  if (isDaemonMode) {
94
- //默认将log重定向到文件,不然在restart后会丢失
95
- let logFile = pr(`.cache/app${runInWindows ? '.win' : ''}.log`);
96
- if (!fs.existsSync(path.dirname(logFile))) fs.mkdirSync(path.dirname(logFile));
97
- let oldWrite = process.stdout.write.bind(process.stdout);
98
- var access = fs.createWriteStream(logFile, { flags: 'a' });
99
- if (!process.env['PXW_NOREDIRECT_LOG']) {
100
- process.stdout.write = process.stderr.write = access.write.bind(access);
101
- }
102
-
103
- let checkBinString = 'pxw/bin/pxw.js';
104
- let checkCmd =
105
- process.platform == 'win32' //
106
- ? 'wmic /OUTPUT:STDOUT process get processid,commandline'
107
- : `ps -ef|grep "node"|grep "${checkBinString.replace(/(.)/, '[$1]')}"`; //第一个字符包在[]里可以避免grep命令自身出现在结果里
108
-
109
- console.log(`\n*** pxw run: ${new Date().toLocaleString()} ***\npid[${process.pid}] check singleton:`, checkCmd);
110
- cp.exec(checkCmd, { windowsHide: true, maxBuffer: 1024 * 1024 * 10 }, (error, stdout, stderr) => {
111
- if (error) {
112
- console.log('pgrep error', error);
113
- // return;
114
- }
115
-
116
- let cpid = process.pid.toString();
117
- if (process.platform == 'win32') {
118
- let psl = stdout.trim().split('\n');
119
- // console.log(psl.splice(300));
120
-
121
- for (let cmd of psl) {
122
- cmd = cmd.replace(/\\/g, '/');
123
- if ((cmd.indexOf('/node.exe') > 0 || cmd.indexOf('node') == 0) && cmd.indexOf(checkBinString) > 0) {
124
- let info = cmd.trim().replace(/\s+/g, ' ').split(' ');
125
- console.log('find', info, process.pid);
126
- let tpid = info[info.length - 1];
127
- let isSelf = tpid == cpid;
128
- console.log(`tpid[${tpid}],isSelf:${isSelf}`);
129
- if (!isSelf) {
130
- if (argv.me) {
131
- cp.execSync('taskkill /F /PID ' + tpid);
132
- } else {
133
- return;
134
- }
135
- }
136
- }
137
- }
138
- } else {
139
- console.log('pgrep stdout\n', stdout);
140
- let lines = stdout.trim().split('\n');
141
- for (let line of lines) {
142
- line = line
143
- .trim()
144
- .split(' ')
145
- .filter((x) => !!x);
146
- // console.log(line);
147
- if (line.length > 5) {
148
- let tpid = line[1];
149
- let isSelf = tpid == cpid;
150
- console.log(`tpid[${tpid}],isSelf:${isSelf}`);
151
- if (tpid && !isSelf) {
152
- console.log(`found exist process:[${tpid}], cur[${process.pid}]`);
153
- if ((argv.me || argv.kill) && !error) {
154
- console.log(`force kill other process:[${tpid}]`);
155
- try {
156
- process.stdout.write('', () => {
157
- cp.execSync('kill ' + tpid);
158
- });
159
- } catch (e) {
160
- console.log(e);
161
- }
162
- } else {
163
- // cp.execSync('ps -ef|grep test/html/app.js', { stdio: 'inherit' });
164
- // cp.execSync(`ps -ef|grep ${stdout}`, { stdio: 'inherit' });
165
- return console.log('process already exist, pid:', tpid);
166
- }
167
- }
168
- }
169
- }
170
- }
171
- if (argv.kill) return;
172
-
173
- if (argv.fg || process.env.restart) {
174
- if (argv.fg) {
175
- oldWrite(`pid[${process.pid}] run in fg,log已重定向到 ${logFile} \n`);
176
- } else {
177
- let rc = parseInt(process.env.restart);
178
- process.env.restart = rc + 1;
179
- console.log(`pid[${process.pid}] run in restart: ${rc}`);
180
- }
181
- main();
182
- watchSelf();
183
- } else {
184
- console.log(`pid[${process.pid}] run in bg`);
185
- oldWrite('pxtest-admin 默认后台运行,加--fg参数可前台运行,log已重定向到' + logFile + '\n');
186
- process.env.restart = 1;
187
- restart();
188
- }
189
- });
190
- } else {
52
+ require('../config/daemon').start(main);
53
+ }
54
+ else {
191
55
  main();
192
56
  }
@@ -0,0 +1,157 @@
1
+ let fs = require('fs');
2
+ let cp = require('child_process');
3
+ let path = require('path');
4
+ let http = require('http');
5
+ let os = require('os');
6
+ let util = require('./util');
7
+ let pfbs = require('./pfbs');
8
+ let webpack = require('./webpack');
9
+ let argv = require('yargs').argv;
10
+
11
+ let runInWindows = process.platform == 'win32';
12
+ let pr = path.resolve;
13
+ let log = console.log;
14
+ log = () => {};
15
+
16
+ let rootDir = pr(__dirname, '../');
17
+ let projectConfig = util.loadProjectConfig();
18
+
19
+ let isDaemonMode = argv.d;
20
+
21
+ function restart() {
22
+ const scriptPath = process.argv[1];
23
+ let removeArgs = { '--fg': 1 };
24
+ let args = process.argv.slice(2);
25
+ args = args.filter((v) => !removeArgs[v]);
26
+ const subprocess = cp.spawn(process.execPath, [scriptPath, ...args], {
27
+ detached: true,
28
+ windowsHide: true,
29
+ });
30
+ subprocess.unref();
31
+ process.exit();
32
+ }
33
+
34
+ function watchSelf() {
35
+ //可以通过环境变量设置是否要监视重启
36
+ let restartWatchFile = process.env['PXW_WATCHFILE'];
37
+ if (restartWatchFile == 'none') return;
38
+ if (!restartWatchFile) restartWatchFile = __filename;
39
+ restartWatchFile = pr(restartWatchFile);
40
+ console.log('watchfile:', restartWatchFile);
41
+ let chokidar = require('chokidar');
42
+ let watcher = chokidar.watch(restartWatchFile, { ignored: /^\./, persistent: true });
43
+ watcher
44
+ .on('change', function (path) {
45
+ console.log(`pid[${process.pid}] observing '${path}' changed, will restart!`);
46
+ restart();
47
+ })
48
+ .on('error', function (error) {
49
+ console.error('watchSelf error:', error);
50
+ });
51
+ }
52
+
53
+ module.exports.start = function (main) {
54
+ //默认将log重定向到文件,不然在restart后会丢失
55
+ let logFile = pr(`.cache/app${runInWindows ? '.win' : ''}.log`);
56
+ if (!fs.existsSync(path.dirname(logFile))) fs.mkdirSync(path.dirname(logFile));
57
+ let oldWrite = process.stdout.write.bind(process.stdout);
58
+ var access = fs.createWriteStream(logFile, { flags: 'a' });
59
+ if (!process.env['PXW_NOREDIRECT_LOG']) {
60
+ process.stdout.write = process.stderr.write = access.write.bind(access);
61
+ }
62
+
63
+ let checkBinString = 'pxw/bin/pxw.js';
64
+ let checkCmd =
65
+ process.platform == 'win32' //
66
+ ? 'wmic /OUTPUT:STDOUT process get processid,commandline'
67
+ : `ps -ef|grep "node"|grep "${checkBinString.replace(/(.)/, '[$1]')}"`; //第一个字符包在[]里可以避免grep命令自身出现在结果里
68
+
69
+ console.log(`\n*** pxw run: ${new Date().toLocaleString()} ***\npid[${process.pid}] check singleton:`, checkCmd);
70
+ cp.exec(checkCmd, { windowsHide: true, maxBuffer: 1024 * 1024 * 10 }, (error, stdout, stderr) => {
71
+ if (error) {
72
+ console.log('pgrep error', error);
73
+ // return;
74
+ }
75
+
76
+ let cpid = process.pid.toString();
77
+ if (process.platform == 'win32') {
78
+ let psl = stdout.trim().split('\n');
79
+ // console.log(psl.splice(300));
80
+
81
+ for (let cmd of psl) {
82
+ cmd = cmd.replace(/\\/g, '/');
83
+ if ((cmd.indexOf('/node.exe') > 0 || cmd.indexOf('node') == 0) && cmd.indexOf(checkBinString) > 0) {
84
+ let info = cmd.trim().replace(/\s+/g, ' ').split(' ');
85
+ console.log('find', info, process.pid);
86
+ let tpid = info[info.length - 1];
87
+ let isSelf = tpid == cpid;
88
+ console.log(`tpid[${tpid}],isSelf:${isSelf}`);
89
+ if (!isSelf) {
90
+ if (argv.me || argv.kill) {
91
+ cp.execSync('taskkill /F /PID ' + tpid);
92
+ }
93
+ else {
94
+ return;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ if (argv.kill) return;
100
+ }
101
+ else {
102
+ console.log('pgrep stdout\n', stdout);
103
+ let lines = stdout.trim().split('\n');
104
+ for (let line of lines) {
105
+ line = line
106
+ .trim()
107
+ .split(' ')
108
+ .filter((x) => !!x);
109
+ // console.log(line);
110
+ if (line.length > 5) {
111
+ let tpid = line[1];
112
+ let isSelf = tpid == cpid;
113
+ console.log(`tpid[${tpid}],isSelf:${isSelf}`);
114
+ if (tpid && !isSelf) {
115
+ console.log(`found exist process:[${tpid}], cur[${process.pid}]`);
116
+ if ((argv.me || argv.kill) && !error) {
117
+ console.log(`force kill other process:[${tpid}]`);
118
+ try {
119
+ process.stdout.write('', () => {
120
+ cp.execSync('kill ' + tpid);
121
+ });
122
+ }
123
+ catch (e) {
124
+ console.log(e);
125
+ }
126
+ }
127
+ else {
128
+ // cp.execSync('ps -ef|grep test/html/app.js', { stdio: 'inherit' });
129
+ // cp.execSync(`ps -ef|grep ${stdout}`, { stdio: 'inherit' });
130
+ return console.log('process already exist, pid:', tpid);
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ if (argv.kill) return;
137
+
138
+ if (argv.fg || process.env.restart) {
139
+ if (argv.fg) {
140
+ oldWrite(`pid[${process.pid}] run in fg,log已重定向到 ${logFile} \n`);
141
+ }
142
+ else {
143
+ let rc = parseInt(process.env.restart);
144
+ process.env.restart = rc + 1;
145
+ console.log(`pid[${process.pid}] run in restart: ${rc}`);
146
+ }
147
+ main();
148
+ watchSelf();
149
+ }
150
+ else {
151
+ console.log(`pid[${process.pid}] run in bg`);
152
+ oldWrite('pxtest-admin 默认后台运行,加--fg参数可前台运行,log已重定向到' + logFile + '\n');
153
+ process.env.restart = 1;
154
+ restart();
155
+ }
156
+ });
157
+ };