-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·54 lines (46 loc) · 1.47 KB
/
index.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
#!/usr/bin/env node
const path = require('path')
const fs = require('fs')
const readPackageJson = require('./read-package-json')
const isTestMode = process.env.NODE_ENV === 'test'
const projectDir = isTestMode
? path.join(__dirname, 'fixtures')
: projectDir = process.cwd()
const projectPackageJson = readPackageJson(projectDir)
const getCaretDeps = (deps) => {
return Object.keys(deps)
.map(key => {
return {
name: key,
version: deps[key],
}
})
.filter(dep => {
return dep.version[0] === '^'
})
}
const changes = []
const updateDeps = (packageJson, depenencyType) => {
const caretDeps = getCaretDeps(packageJson[depenencyType] || {})
caretDeps.forEach(dep => {
const depDir = path.join(projectDir, 'node_modules', dep.name)
const depPackageJson = readPackageJson(depDir)
const actualVersion = depPackageJson.version
if (actualVersion !== dep.version) {
changes.push(`${dep.name}: ${dep.version} -> ${actualVersion}`)
}
packageJson[depenencyType][dep.name] = actualVersion
})
return packageJson
}
// side effects!
updateDeps(projectPackageJson, 'dependencies')
updateDeps(projectPackageJson, 'devDependencies')
if (changes.length) {
changes.sort().forEach(x => console.log(x))
const prettyJson = JSON.stringify(projectPackageJson, null, 2)
const outputFileName = isTestMode
? 'result.package.json'
: 'package.json'
fs.writeFileSync(path.join(projectDir, outputFileName), prettyJson)
}