Skip to content

Commit c1403e2

Browse files
committed
feat(process): process
1 parent 254efb5 commit c1403e2

File tree

3 files changed

+209
-2
lines changed

3 files changed

+209
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
### Features
55

6+
* **changelog:** chagnelog ([254efb5](https://github.com/836334258/node_practice/commit/254efb50725ed7cc04fd28b2c1c0cb39459284f4))
67
* **path:** path ([237dca2](https://github.com/836334258/node_practice/commit/237dca247ff4f1b938fd529c8717295d8217142d))
78

89

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"scripts": {
1010
"test": "echo \"Error: no test specified\" && exit 1",
11-
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
11+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
12+
"cz": "cz"
1213
},
1314
"keywords": [],
1415
"author": "",
@@ -27,4 +28,4 @@
2728
"path": "./node_modules/cz-conventional-changelog"
2829
}
2930
}
30-
}
31+
}

process/index.ts

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import path from 'path'
2+
import process from 'process'
3+
4+
// 由于process是EventEmitter的实例,on事件相当于addListener事件
5+
6+
// 0 node正常结束后会调用beforeExit,如果显式调用process.exit()则不会调用
7+
process.addListener('beforeExit', (code) => {
8+
console.log('Process beforeExit event with code: ', code)
9+
})
10+
11+
// 目前看起来只要退出就会调用
12+
process.addListener('exit', (code) => {
13+
console.log(`About to exit with code: ${code}`)
14+
})
15+
16+
// promise里面不止拒绝或解决过一次,就会调用 reject Promise { 'ok' } no ok
17+
process.on('multipleResolves', (type, promise, value) => {
18+
console.error(type, promise, value)
19+
})
20+
new Promise((resolve, reject) => {
21+
resolve('ok')
22+
reject('no ok')
23+
})
24+
.then(console.log)
25+
.catch(console.error)
26+
27+
// 捕获promise.reject 方法
28+
process.addListener('unhandledRejection', (reason, promise) => {
29+
console.log('unhandledRejection', reason, promise)
30+
})
31+
32+
Promise.reject('haha reject')
33+
34+
// 捕获异常,不推荐使用
35+
process.addListener('uncaughtException', (error, origin) => {
36+
console.log('uncaughtException', error, origin)
37+
})
38+
39+
// 好像和uncaughtException差不多
40+
process.addListener('uncaughtExceptionMonitor', (error, origin) => {
41+
console.log('uncaughtExceptionMonitor', error, origin)
42+
})
43+
44+
// 会触发uncaughtException事件和uncaughtExceptionMonitor事件
45+
// os.setPriority(0,-10)
46+
47+
// 每当 Node.js 触发进程警告时,则会触发 'warning' 事件
48+
process.addListener('warning', (waring) => {
49+
console.log('waring', waring)
50+
})
51+
52+
// 创建新的 <Worker> 线程后会触发 'worker' 事件。
53+
process.addListener('worker', (worker) => {
54+
console.log('worker', worker)
55+
})
56+
57+
// 一种信号,暂时没发现用处
58+
process.addListener('SIGINT',(signal)=>{
59+
console.log('signal',signal)
60+
})
61+
62+
// 导致node立即退出,同时addListener里面的事件也失效
63+
// process.abort()
64+
65+
// cpu架构 x64
66+
console.log(process.arch)
67+
68+
// 输入参数 ts-node index.ts a b
69+
/**
70+
* [
71+
'/mnt/c/Program Files/nodejs/node_modules/ts-node/dist/bin.js',
72+
'/mnt/e/code/test_code/node_practice/process/index.ts',
73+
'a',
74+
'b'
75+
]
76+
*/
77+
console.log(process.argv)
78+
79+
// node的只读副本
80+
console.log(process.argv0)
81+
82+
// 改变目录
83+
process.chdir(path.dirname(__dirname))
84+
85+
// 当前目录
86+
console.log(process.cwd())
87+
88+
// 会触发waring事件 waring Warning: emitWaring....
89+
process.emitWarning('emitWaring....')
90+
91+
// 用户环境对象
92+
console.log(process.env)
93+
94+
// 返回 Node.js 进程启动时传入的一组特定于 Node.js 的命令行选项
95+
console.log(process.execArgv)
96+
97+
// node可执行文件的绝对路径名
98+
console.log(process.execPath)
99+
100+
// 设置终止进程code码,默认为0
101+
process.exitCode=100
102+
// 终止进程
103+
// process.exit()
104+
105+
// console.log(process.getActiveResourcesInfo())
106+
107+
// 群组id
108+
console.log(process.getgid())
109+
110+
// 用户id
111+
console.log(process.getuid())
112+
113+
console.log(process.hrtime())
114+
115+
// 返回纳秒
116+
console.log(process.hrtime.bigint())
117+
118+
// 将sigint发送到由pid标识的进程
119+
process.kill(process.pid,'SIGINT')
120+
121+
122+
// 内存使用量
123+
console.log(process.memoryUsage())
124+
125+
// 会先执行nextTick,然后执行queueMicrotask,除非需要 process.nextTick() 的特定功能,否则请使用 queueMicrotask()。
126+
queueMicrotask(()=>{
127+
console.log('queueMicrotask')
128+
})
129+
130+
// 将 callback 添加到"下一个滴答队列"
131+
process.nextTick(()=>{
132+
console.log('nextTick')
133+
})
134+
135+
// pid 434
136+
console.log('pid',process.pid)
137+
138+
// 平台 linux
139+
console.log(process.platform)
140+
141+
// 父进程id
142+
console.log(process.ppid)
143+
144+
// 当前版本相关的元数据
145+
/**
146+
* {
147+
name: 'node',
148+
sourceUrl: 'https://nodejs.org/download/release/v17.5.0/node-v17.5.0.tar.gz',
149+
headersUrl: 'https://nodejs.org/download/release/v17.5.0/node-v17.5.0-headers.tar.gz'
150+
}
151+
*/
152+
console.log(process.release)
153+
154+
/**
155+
* 诊断报告
156+
* {
157+
writeReport: [Function: writeReport],
158+
getReport: [Function: getReport],
159+
directory: [Getter/Setter],
160+
filename: [Getter/Setter],
161+
compact: [Getter/Setter],
162+
signal: [Getter/Setter],
163+
reportOnFatalError: [Getter/Setter],
164+
reportOnSignal: [Getter/Setter],
165+
reportOnUncaughtException: [Getter/Setter]
166+
}
167+
*/
168+
console.log(process.report)
169+
170+
/**
171+
* ReadStream
172+
*/
173+
console.log(process.stdin)
174+
175+
// 运行了几秒 3.124805684
176+
console.log(process.uptime())
177+
178+
// node版本 v17.5.0
179+
console.log(process.version)
180+
181+
/**
182+
* 列出依赖项
183+
{
184+
node: '17.5.0',
185+
v8: '9.6.180.15-node.13',
186+
uv: '1.43.0',
187+
zlib: '1.2.11',
188+
brotli: '1.0.9',
189+
ares: '1.18.1',
190+
modules: '102',
191+
nghttp2: '1.45.1',
192+
napi: '8',
193+
llhttp: '6.0.4',
194+
openssl: '3.0.1+quic',
195+
cldr: '40.0',
196+
icu: '70.1',
197+
tz: '2021a3',
198+
unicode: '14.0',
199+
ngtcp2: '0.1.0-DEV',
200+
nghttp3: '0.1.0-DEV'
201+
}
202+
*/
203+
console.log(process.versions)
204+
205+

0 commit comments

Comments
 (0)