Skip to content

Commit 1983931

Browse files
committed
feat: temp
1 parent b9101ce commit 1983931

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

.DS_Store

8 KB
Binary file not shown.

images/.DS_Store

6 KB
Binary file not shown.

images/techArticle/.DS_Store

6 KB
Binary file not shown.

techArticle/npm/bin/npm-cli.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s

techArticle/npm/[email protected]

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
## 源码解析
2+
```js
3+
#!/usr/bin/env node
4+
;(function () { // wrapper in case we're in module_context mode
5+
// windows: running "npm blah" in this folder will invoke WSH, not node.
6+
/*global WScript*/
7+
// WScript 是 windows 下的一个运行脚本的工具
8+
if (typeof WScript !== 'undefined') {
9+
WScript.echo(
10+
'npm does not work when run\n' +
11+
'with the Windows Scripting Host\n\n' +
12+
"'cd' to a different directory,\n" +
13+
"or type 'npm.cmd <args>',\n" +
14+
"or type 'node npm <args>'."
15+
)
16+
WScript.quit(1)
17+
return
18+
}
19+
20+
process.title = 'npm'
21+
22+
var unsupported = require('../lib/utils/unsupported.js')
23+
unsupported.checkForBrokenNode()
24+
25+
var log = require('npmlog')
26+
log.pause() // will be unpaused when config is loaded.
27+
log.info('it worked if it ends with', 'ok')
28+
29+
unsupported.checkForUnsupportedNode()
30+
31+
if (!unsupported.checkVersion(process.version).unsupported) {
32+
var updater = require('update-notifier')
33+
var pkg = require('../package.json')
34+
updater({pkg: pkg}).notify({defer: true})
35+
}
36+
37+
var path = require('path')
38+
var npm = require('../lib/npm.js')
39+
var npmconf = require('../lib/config/core.js')
40+
var errorHandler = require('../lib/utils/error-handler.js')
41+
var output = require('../lib/utils/output.js')
42+
43+
var configDefs = npmconf.defs
44+
var shorthands = configDefs.shorthands
45+
var types = configDefs.types
46+
var nopt = require('nopt')
47+
48+
// if npm is called as "npmg" or "npm_g", then
49+
// run in global mode.
50+
if (path.basename(process.argv[1]).slice(-1) === 'g') {
51+
process.argv.splice(1, 1, 'npm', '-g')
52+
}
53+
54+
log.verbose('cli', process.argv)
55+
56+
var conf = nopt(types, shorthands)
57+
npm.argv = conf.argv.remain
58+
if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()
59+
else conf.usage = true
60+
61+
if (conf.version) {
62+
console.log(npm.version)
63+
return errorHandler.exit(0)
64+
}
65+
66+
if (conf.versions) {
67+
npm.command = 'version'
68+
conf.usage = false
69+
npm.argv = []
70+
}
71+
72+
log.info('using', 'npm@%s', npm.version)
73+
log.info('using', 'node@%s', process.version)
74+
75+
process.on('uncaughtException', errorHandler)
76+
77+
if (conf.usage && npm.command !== 'help') {
78+
npm.argv.unshift(npm.command)
79+
npm.command = 'help'
80+
}
81+
82+
// now actually fire up npm and run the command.
83+
// this is how to use npm programmatically:
84+
conf._exit = true
85+
npm.load(conf, function (er) {
86+
if (er) return errorHandler(er)
87+
npm.commands[npm.command](npm.argv, function (err) {
88+
// https://www.youtube.com/watch?v=7nfPu8qTiQU
89+
if (!err && npm.config.get('ham-it-up') && !npm.config.get('json') && !npm.config.get('parseable') && npm.command !== 'completion') {
90+
output('\n 🎵 I Have the Honour to Be Your Obedient Servant,🎵 ~ npm 📜🖋\n')
91+
}
92+
errorHandler.apply(this, arguments)
93+
})
94+
})
95+
})()
96+
```

techArticle/npm/引子-npm-cli.js.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## 入口
2+
```
3+
每个 `npm` 包都有一个主入口。
4+
一般为 `index.js`, `main.js`, `package.json.main`。优先使用 `pkg.json` 中的 `main`。
5+
```
6+
7+
### npm
8+
npm 包中,`package.json``main` 指定了入口文件为:
9+
```json
10+
"main": "./lib/npm.js"
11+
```
12+
分析 `npm.js` 中,可以看到有
13+
```js
14+
if (require.main === module) {
15+
require('../bin/npm-cli.js')
16+
}
17+
```
18+
这样的一段代码。当运行的主程序为该文件时,引用 `/bin/npm-cli.js`。确保执行到 cli 文件。
19+
20+
```
21+
* 存疑 *: 主程序指的是什么,使用 node 运行的时候,并不能得到预期结果。
22+
什么时候会用到?
23+
```
24+
执行 `npm.js` 时,会被切换 `npm-cli.js`

techArticle/当 npm install 的时候发生了什么.md

+7
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,10 @@ process.emit('time', 'stage:' + name)
112112
```js
113113
var cmd = require(path.join(__dirname, a + '.js'))
114114
```
115+
116+
3. 相关概论
117+
deref: Dereference -- 间接引用
118+
119+
module.main -- 运行程序的主模块
120+
121+

0 commit comments

Comments
 (0)