-
Notifications
You must be signed in to change notification settings - Fork 50
/
version.js
executable file
·111 lines (94 loc) · 3.35 KB
/
version.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import fs from 'fs'
import path from 'path'
import semver from 'semver'
import format from 'format-json'
import {execSync} from 'child_process'
let changeModules = {}
let moduleMaps = {}
const hasChanges = (path)=> {
// 先看看status对不对
const gitStatus = execSync(`cd ${path};git status`)
return gitStatus.indexOf('nothing to commit, working directory clean') <= -1
}
const getModulePath = (info)=> {
return path.join(__dirname, '../../..', `lib/${info.categoryName}/${info.module.path}`)
}
const updateModuleVirtual = (modulePath, name, version) => {
if (!changeModules[modulePath]) {
changeModules[modulePath] = {
modulePath: modulePath,
name : name,
version : semver.inc(version, 'patch')
}
}
}
const buildModuleMap = (modules) => {
modules.map((module)=> {
const modulePath = getModulePath(module)
let moduleObj = getModuleObj(modulePath)
moduleMaps[moduleObj.name] = {
version : moduleObj.version,
name : moduleObj.name,
modulePath : modulePath,
dependencies: []
}
for (let dependence in moduleObj.dependencies) {
moduleMaps[moduleObj.name].dependencies.push({
[dependence]: moduleObj.dependencies[dependence]
})
}
})
}
const getModuleObj = (module) => {
if (fs.existsSync(module, 'package.json')) {
return JSON.parse(fs.readFileSync(path.resolve(module, 'package.json')).toString())
}
return {}
}
const whoIsNeedMe = (moduleObj)=> {
let modules = []
let moduleName = moduleObj.name
for (let module in moduleMaps) {
for (let dep of moduleMaps[module].dependencies) {
if (dep[moduleName]) {
modules.push({
modulePath : moduleMaps[module].modulePath,
moduleName : moduleMaps[module].name,
moduleVersion: moduleMaps[module].version
})
}
}
}
return modules
}
const updateModule = (modulePath) => {
let moduleObj = changeModules[modulePath] || getModuleObj(modulePath)
updateModuleVirtual(modulePath, moduleObj.name, moduleObj.version)
let dependences = whoIsNeedMe(moduleObj)
for (let dep of dependences) {
updateModule(dep.modulePath)
}
}
const writeChanges = () => {
for (let change in changeModules) {
let moduleObj = getModuleObj(changeModules[change].modulePath)
console.log(`Update ${changeModules[change].name} version ${moduleObj.version} ==> ${changeModules[change].version}`)
moduleObj.version = changeModules[change].version
for (let dep in moduleObj.dependencies) {
if (moduleMaps[dep] && changeModules[moduleMaps[dep].modulePath] && (changeModules[moduleMaps[dep].modulePath].name === dep)) {
moduleObj.dependencies[dep] = '^' + changeModules[moduleMaps[dep].modulePath].version
}
}
fs.writeFileSync(path.join(changeModules[change].modulePath, 'package.json'), format.plain(moduleObj), 'utf-8')
}
}
export default (modules) => {
buildModuleMap(modules)
for (let module of modules) {
const modulePath = getModulePath(module)
if (hasChanges(modulePath)) {
updateModule(modulePath)
}
}
writeChanges()
}