1 | 'use strict'
|
2 |
|
3 | const cwd = process.cwd()
|
4 | const path = require('path')
|
5 | const fs = require('fs-extra')
|
6 | const { error, info } = require('./utils/log')
|
7 | const npmVersions = require('./utils/check-npm-version')
|
8 | const { getNodeModulesDir } = require('./env')
|
9 | const { installFromScripts } = require('./index')
|
10 |
|
11 | const nodeModulesDir = getNodeModulesDir()
|
12 | const scriptPath = path.join(__dirname, '../scripts/postinstall')
|
13 | const destDirPath = path.join(nodeModulesDir, '.hooks')
|
14 | const destScriptPath = path.join(destDirPath, 'postinstall')
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function checkNpm () {
|
27 | |
28 |
|
29 |
|
30 | if (process.env.CI) {
|
31 | return
|
32 | }
|
33 |
|
34 | const { npmVersion, npmLifecycleVersion, pass } = npmVersions
|
35 |
|
36 | info(
|
37 | `npm version: ${npmVersion}`,
|
38 | `npm-lifecycle version: ${npmLifecycleVersion || '-'}`
|
39 | )
|
40 |
|
41 | if (!pass) {
|
42 | error(
|
43 | 'elint 在当前的 npm 版本下无法正常运行,请升级 npm 后再安装',
|
44 | '更多信息请访问:http://t.cn/Rg7xvP0'
|
45 | )
|
46 |
|
47 | process.exit(1)
|
48 | }
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function installHooks () {
|
57 |
|
58 | if (!cwd.includes('node_modules')) {
|
59 | return
|
60 | }
|
61 |
|
62 |
|
63 | fs.ensureDirSync(destDirPath)
|
64 |
|
65 |
|
66 | fs.copySync(scriptPath, destScriptPath)
|
67 |
|
68 |
|
69 | if (process.platform === 'win32') {
|
70 | const cmdScriptPath = path.join(__dirname, '../scripts/postinstall.cmd')
|
71 | const destCmdScriptPath = path.join(destDirPath, 'postinstall.cmd')
|
72 |
|
73 | fs.copySync(cmdScriptPath, destCmdScriptPath)
|
74 | }
|
75 |
|
76 |
|
77 | fs.chmodSync(destScriptPath, 0o755)
|
78 | }
|
79 |
|
80 | checkNpm()
|
81 |
|
82 | installHooks()
|
83 |
|
84 |
|
85 | installFromScripts()
|