This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
install.js
73 lines (65 loc) · 2.06 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path')
const fs = require('fs')
const child_process = require('child_process')
const root = process.cwd()
npm_install_recursive(root)
// Since this script is intended to be run as a "preinstall" command,
// it will do `npm install` automatically inside the root folder in the end.
console.log(
'==================================================================='
)
console.log(`Performing "npm install" inside root folder`)
console.log(
'==================================================================='
)
npm_install(root)
// Recurses into a folder
function npm_install_recursive(folder) {
const has_package_json = fs.existsSync(path.join(folder, 'package.json'))
// Abort if there's no `package.json` in this folder and it's not a "packages" folder
if (!has_package_json && path.basename(folder) !== 'packages') {
return
}
// If there is `package.json` in this folder then perform `npm install`.
//
// Since this script is intended to be run as a "preinstall" command,
// skip the root folder, because it will be `npm install`ed in the end.
// Hence the `folder !== root` condition.
//
if (has_package_json && folder !== root) {
console.log(
'==================================================================='
)
console.log(
`Performing "npm install" inside ${
folder === root ? 'root folder' : './' + path.relative(root, folder)
}`
)
console.log(
'==================================================================='
)
npm_install(folder)
}
// Recurse into subfolders
for (let subfolder of subfolders(folder)) {
npm_install_recursive(subfolder)
}
}
// Performs `npm install`
function npm_install(where) {
child_process.execSync('npm install', {
cwd: where,
env: process.env,
stdio: 'inherit',
})
}
// Lists subfolders in a folder
function subfolders(folder) {
return fs
.readdirSync(folder)
.filter(subfolder =>
fs.statSync(path.join(folder, subfolder)).isDirectory()
)
.filter(subfolder => subfolder !== 'node_modules' && subfolder[0] !== '.')
.map(subfolder => path.join(folder, subfolder))
}