Skip to content

Commit 5c43a4c

Browse files
author
姚观寿
committed
修改配置
1 parent 54e3409 commit 5c43a4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+460
-787
lines changed

bin/cmd.js

+107-108
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,86 @@
22
* @Date: 2022-08-09 11:24:59
33
* @Author: Yao guan shou
44
* @LastEditors: Yao guan shou
5-
* @LastEditTime: 2022-08-09 13:22:27
5+
* @LastEditTime: 2022-08-10 14:16:33
66
* @FilePath: /react-loading-ssr/bin/cmd.js
77
* @Description:
88
*/
9-
import os from 'os';
10-
import { spawn, SpawnOptions, exec, execSync } from 'child_process';
11-
import moment from 'moment';
9+
import os from 'os'
10+
import { spawn, SpawnOptions, exec, execSync } from 'child_process'
11+
import moment from 'moment'
1212

1313
export default class Cmd {
14-
text = '';
14+
text = ''
1515

16-
runNodeModule(moduleName, params, options) {
17-
if (os.type() == 'Windows_NT' && !moduleName.match(/\.cmd$/)) {
18-
moduleName += '.cmd';
19-
}
20-
return this.run(moduleName, params, options);
16+
runNodeModule(moduleName, params, options) {
17+
if (os.type() == 'Windows_NT' && !moduleName.match(/\.cmd$/)) {
18+
moduleName += '.cmd'
2119
}
20+
return this.run(moduleName, params, options)
21+
}
22+
23+
run(command, params, options) {
24+
this.text = ''
25+
// options = Object.assign(options || {}, { cwd: this.cfg.cwd });
26+
return new Promise((resolve, reject) => {
27+
console.log(`run command: ${command}, params:`, params, options)
28+
29+
if (!options) {
30+
options = {
31+
stdio: 'inherit',
32+
}
33+
}
34+
if (!params) params = []
35+
options.stdio = 'pipe'
2236

23-
run(command, params, options) {
24-
this.text = '';
25-
// options = Object.assign(options || {}, { cwd: this.cfg.cwd });
26-
return new Promise((resolve, reject) => {
27-
console.log(`run command: ${command}, params:`, params, options);
28-
29-
if (!options) {
30-
options = {
31-
stdio: 'inherit',
32-
};
33-
}
34-
if (!params) params = [];
35-
options.stdio = 'pipe';
36-
37-
let proc = spawn(command, params, options);
38-
// console.log('proc===', proc)
39-
40-
proc.stdout.on('data', (data) => {
41-
let dataStr = String(data);
42-
if (options.logPrefix) {
43-
dataStr = options.logPrefix + dataStr;
44-
}
45-
this.text += dataStr;
46-
if (!options?.silent)
47-
process.stdout.write(
48-
moment().format('HH:mm:ss:SSS ') + dataStr
49-
);
50-
});
51-
52-
proc.stderr.on('data', (data) => {
53-
// 不一定代表进程exitcode != 0,可能只是进程调用了console.error
54-
let dataStr = String(data);
55-
if (options?.logPrefix) {
56-
dataStr = options.logPrefix + dataStr;
57-
}
58-
if (!options?.silent)
59-
process.stderr.write(
60-
moment().format('HH:mm:ss:SSS ') + dataStr
61-
);
62-
});
63-
64-
// 进程错误
65-
proc.on('error', (error) => {
66-
if (!options?.silent) console.error(error);
67-
reject(error);
68-
});
69-
70-
// 进程关闭
71-
proc.on('close', (code) => {
72-
console.log(`process closed with exit code: ${code}`);
73-
if (code == 0) {
74-
resolve(this.text || '');
75-
} else {
76-
let errMsg = `process closed with exit code: ${code}`;
77-
if (options?.logPrefix) {
78-
errMsg = options.logPrefix + errMsg;
79-
}
80-
reject(new Error(errMsg));
81-
}
82-
});
83-
84-
proc.on('exit', (code, signal) => {
85-
console.log(`process exits`);
86-
});
87-
});
88-
}
37+
let proc = spawn(command, params, options)
38+
// console.log('proc===', proc)
39+
40+
proc.stdout.on('data', (data) => {
41+
let dataStr = String(data)
42+
if (options.logPrefix) {
43+
dataStr = options.logPrefix + dataStr
44+
}
45+
this.text += dataStr
46+
if (!options?.silent)
47+
process.stdout.write(moment().format('HH:mm:ss:SSS ') + dataStr)
48+
})
49+
50+
proc.stderr.on('data', (data) => {
51+
// 不一定代表进程exitcode != 0,可能只是进程调用了console.error
52+
let dataStr = String(data)
53+
if (options?.logPrefix) {
54+
dataStr = options.logPrefix + dataStr
55+
}
56+
if (!options?.silent)
57+
process.stderr.write(moment().format('HH:mm:ss:SSS ') + dataStr)
58+
})
59+
60+
// 进程错误
61+
proc.on('error', (error) => {
62+
if (!options?.silent) console.error(error)
63+
reject(error)
64+
})
65+
66+
// 进程关闭
67+
proc.on('close', (code) => {
68+
console.log(`process closed with exit code: ${code}`)
69+
if (code == 0) {
70+
resolve(this.text || '')
71+
} else {
72+
let errMsg = `process closed with exit code: ${code}`
73+
if (options?.logPrefix) {
74+
errMsg = options.logPrefix + errMsg
75+
}
76+
reject(new Error(errMsg))
77+
}
78+
})
79+
80+
proc.on('exit', (code, signal) => {
81+
console.log(`process exits`)
82+
})
83+
})
84+
}
8985
}
9086

9187
// let cmd = new Cmd().runNodeModule(
@@ -94,48 +90,51 @@ export default class Cmd {
9490
// )
9591

9692
export const execute = (command, options = { stdio: 'inherit' }) => {
97-
command = command.split(' ').filter((item) => item);
93+
command = command.split(' ').filter((item) => item)
9894

99-
if (os.type() === 'Windows_NT' && !command[0].match(/\.cmd$/)) {
100-
command[0] += '.cmd';
101-
}
95+
if (os.type() === 'Windows_NT' && !command[0].match(/\.cmd$/)) {
96+
command[0] += '.cmd'
97+
}
10298

103-
const proc = spawn(command[0], command.slice(1), options);
99+
const proc = spawn(command[0], command.slice(1), options)
104100

105-
// 进程错误
106-
proc.on('error', (error) => {
107-
if (error) console.error(error);
108-
});
101+
// 进程错误
102+
proc.on('error', (error) => {
103+
if (error) {
104+
console.error('process error:', error)
105+
}
106+
})
109107

110-
// 进程关闭
111-
proc.on('close', (code) => {
112-
console.log(`process closed with exit code: ${code}`);
113-
});
108+
// 进程关闭
109+
proc.on('close', (code) => {
110+
console.log(`process closed with exit code: ${code}`)
111+
// process.exit(code);
112+
})
114113

115-
// 退出
116-
proc.on('exit', (code, signal) => {
117-
console.log(`process exits`);
118-
});
114+
// 退出
115+
proc.on('exit', (code, signal) => {
116+
console.log(`process exits`)
117+
// process.exit(code);
118+
})
119119

120-
return proc;
121-
};
120+
return proc
121+
}
122122

123123
/**
124124
* 判断端口是否被占用
125125
* @param port 端口号
126126
* @returns 该端口是否被占用
127127
*/
128128
export const iSportTake = (port) => {
129-
const cmd =
130-
process.platform === 'win32'
131-
? `netstat -aon|findstr ${port}`
132-
: `lsof -i:${port}`;
133-
try {
134-
const res = execSync(cmd);
135-
return true;
136-
} catch (error) {
137-
// console.log('error:', error);
138-
return false;
139-
}
140-
141-
};
129+
const cmd =
130+
process.platform === 'win32'
131+
? `netstat -aon|findstr ${port}`
132+
: `lsof -i:${port}`
133+
try {
134+
const res = execSync(cmd)
135+
return true
136+
} catch (error) {
137+
// console.log('error:', error);
138+
return false
139+
}
140+
}

bin/index.js

+63-65
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,83 @@
22
* @Date: 2022-08-09 09:35:04
33
* @Author: Yao guan shou
44
* @LastEditors: Yao guan shou
5-
* @LastEditTime: 2022-08-09 13:52:05
5+
* @LastEditTime: 2022-08-10 16:18:28
66
* @FilePath: /react-loading-ssr/bin/index.js
77
* @Description:
88
*/
9-
import cluster from 'cluster';
10-
import path from 'path';
11-
import os from 'os';
12-
import { Worker } from 'worker_threads';
13-
import { copyFile, watchFile, readWriteFiles } from '../webpack/utils';
14-
import { spawn, SpawnOptions } from 'child_process';
15-
import kill from 'kill-port'; // 杀死端口包
16-
import { execute, iSportTake } from './cmd'; // 杀死端口包
17-
import { stabilization } from '../client/utils';
18-
import ResolveAlias from '../webpack/definePlugin/webpack-plugin-resolve-alias';
9+
import cluster from 'cluster'
10+
import path from 'path'
11+
import os from 'os'
12+
import { Worker } from 'worker_threads'
13+
import { copyFile, watchFile, readWriteFiles } from '../webpack/utils'
14+
import { spawn, SpawnOptions } from 'child_process'
15+
import kill from 'kill-port' // 杀死端口包
16+
import { execute, iSportTake } from './cmd' // 杀死端口包
17+
import { stabilization } from '../client/utils'
18+
import ResolveAlias from '../webpack/definePlugin/webpack-plugin-resolve-alias'
1919

20-
import * as dotenv from 'dotenv';
21-
dotenv.config({ path: '.env' });
20+
import * as dotenv from 'dotenv'
21+
dotenv.config({ path: '.env' })
2222

2323
// 如果是开发环境 先拷贝 服务器文件到 dist
2424
let {
25-
NODE_ENV, // 环境参数
26-
WEB_ENV, // 环境参数
27-
target, // 环境参数
28-
htmlWebpackPluginOptions = '',
29-
port,
30-
} = process.env; // 环境参数
25+
NODE_ENV, // 环境参数
26+
WEB_ENV, // 环境参数
27+
target, // 环境参数
28+
htmlWebpackPluginOptions = '',
29+
port,
30+
} = process.env // 环境参数
3131

32-
const isSsr = target === 'ssr';
32+
const isSsr = target === 'ssr'
3333
// 是否是生产环境
34-
const isEnvProduction = NODE_ENV === 'production';
34+
const isEnvProduction = NODE_ENV === 'production'
3535
// 是否是测试开发环境
36-
const isEnvDevelopment = NODE_ENV === 'development';
36+
const isEnvDevelopment = NODE_ENV === 'development'
3737

38-
console.log('NODE_ENV======', NODE_ENV);
38+
console.log('NODE_ENV======', NODE_ENV)
3939

40-
let counter = 0;
41-
let child = null;
40+
let counter = 0
41+
let child = null
4242
if (isEnvDevelopment && isSsr) {
43-
let $ResolveAlias = new ResolveAlias({
44-
resolve: {
45-
// 路径配置
46-
alias: {
47-
'@/': path.join(process.cwd(), '/client'),
48-
},
49-
},
50-
});
43+
let $ResolveAlias = new ResolveAlias({
44+
resolve: {
45+
// 路径配置
46+
alias: {
47+
'@/': path.join(process.cwd(), '/client'),
48+
},
49+
},
50+
})
5151

52-
readWriteFiles({
53-
from: path.join(process.cwd(), '/server/**/*').replace(/\\/gi, '/'),
54-
to: path.join(process.cwd(), '/dist/server').replace(/\\/gi, '/'),
55-
transform(content, absoluteFrom) {
56-
let reg = /.jsx|.js$/g;
57-
if (reg.test(absoluteFrom)) {
58-
return $ResolveAlias.alias(content.toString(), '');
59-
}
60-
return content;
61-
},
62-
async callback() {
63-
if (child) {
64-
// spawn 杀死正在执行的子进程
65-
spawn('kill', [child.pid]);
66-
}
67-
68-
if (iSportTake(port)) {
69-
await kill(port, 'tcp');
70-
}
71-
stabilization(500, async () => {
72-
counter += 1;
73-
if (counter === 1) {
74-
child = execute(
75-
"cross-env target='ssr' npx babel-node -r @babel/register ./server/index.js -r dotenv/config dotenv_config_path=.env.development"
76-
);
77-
} else {
78-
child = execute('npm run bin');
79-
counter = 0;
80-
}
81-
});
82-
},
83-
});
52+
readWriteFiles({
53+
from: path.join(process.cwd(), '/server/**/*').replace(/\\/gi, '/'),
54+
to: path.join(process.cwd(), '/dist/server').replace(/\\/gi, '/'),
55+
transform(content, absoluteFrom) {
56+
let reg = /.jsx|.js$/g
57+
if (reg.test(absoluteFrom)) {
58+
return $ResolveAlias.alias(content.toString(), '')
59+
}
60+
return content
61+
},
62+
async callback() {
63+
if (child && child.kill) {
64+
child.kill()
65+
}
66+
if (iSportTake(port)) {
67+
await kill(port, 'tcp')
68+
}
69+
stabilization(1500, async () => {
70+
counter = counter >= 10 ? 2 : counter + 1
71+
if (counter === 1) {
72+
child = execute(
73+
"cross-env target='ssr' npx babel-node -r @babel/register ./server/index.js -r dotenv/config dotenv_config_path=.env.development",
74+
)
75+
} else {
76+
child = execute('npm run bin')
77+
counter = 0
78+
}
79+
})
80+
},
81+
})
8482
}
8583

8684
// if (isEnvDevelopment && !isSsr) {

0 commit comments

Comments
 (0)