forked from vercel/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-dep.mjs
79 lines (64 loc) · 2.18 KB
/
update-dep.mjs
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
74
75
76
77
78
79
import path from 'path'
import fs from 'fs/promises'
import { constants } from 'fs'
import { promisify } from 'util'
import child from 'child_process'
const exec = promisify(child.exec)
const log = (...args) => {
if (process.env.DEBUG !== '0') {
console.log(...args)
}
}
const DIRS = ['edge-functions', 'solutions', 'packages']
const packageName = process.argv[2]
const hasPackageName = (name) => {
const str = packageName.replace(new RegExp(`^${name}`), '')
return (
str.length !== packageName.length && (str.length ? str[0] === '@' : true)
)
}
log('Package name:', packageName)
async function updateDependency() {
if (!packageName?.length) {
throw new Error('No dependency to install specified')
}
const promises = DIRS.map(async (dirPath) => {
log(`Updating all packages in ${dirPath}...`)
const files = await fs.readdir(dirPath)
await Promise.all(
files.map(async (fileName) => {
const filePath = path.join(dirPath, fileName)
const isDir = (await fs.lstat(filePath)).isDirectory()
if (!isDir) return
const packageJson = await fs
.readFile(`./${filePath}/package.json`, 'utf8')
.then((str) => JSON.parse(str))
.catch((err) => {
if (err.code !== 'ENOENT') throw err
})
if (!packageJson) return
if (
!Object.keys(packageJson.dependencies || {}).some(hasPackageName) &&
!Object.keys(packageJson.devDependencies || {}).some(hasPackageName)
) {
return
}
const hasPackageLock = await fs
.access(`./${filePath}/package-lock.json`, constants.F_OK)
.catch((err) => (err ? false : true))
const hasYarnLock = await fs
.access(`./${filePath}/yarn.lock`, constants.F_OK)
.catch((err) => (err ? false : true))
return hasPackageLock !== false || !hasYarnLock
? exec(`npm install ${packageName}`, { cwd: filePath })
: exec(`yarn add ${packageName}`, { cwd: filePath })
})
)
log(`Finished updating all packages in ${dirPath}`)
})
await Promise.all(promises)
}
updateDependency().catch((err) => {
console.error(err)
process.exit(1)
})